001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.gnu.org/licenses/lgpl-3.0.txt
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package dev.tinyflow.core.chain.runtime;
017
018import java.util.concurrent.*;
019
020public class ChainRuntime {
021
022    private static final int NODE_POOL_CORE = 32;
023    private static final int NODE_POOL_MAX = 512;
024    private static final int CHAIN_POOL_CORE = 8;
025    private static final int CHAIN_POOL_MAX = 64;
026
027    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, r -> {
028        Thread t = new Thread(r, "tinyflow-trigger-scheduler");
029        t.setDaemon(true);
030        return t;
031    });
032
033    private static final ExecutorService ASYNC_NODE_EXECUTORS =
034            new ThreadPoolExecutor(
035                    NODE_POOL_CORE, NODE_POOL_MAX,
036                    60L, TimeUnit.SECONDS,
037                    new LinkedBlockingQueue<>(10000),
038                    r -> new Thread(r, "tinyflow-node-exec"));
039
040
041    private static final TriggerScheduler TRIGGER_SCHEDULER = new TriggerScheduler(
042            new InMemoryTriggerStore()
043            , scheduler
044            , ASYNC_NODE_EXECUTORS
045            , 10000L);
046
047
048    public static TriggerScheduler triggerScheduler() {
049        return TRIGGER_SCHEDULER;
050    }
051
052    static {
053        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
054            TRIGGER_SCHEDULER.shutdown();
055            ASYNC_NODE_EXECUTORS.shutdownNow();
056        }));
057    }
058}