Ejemplo n.º 1
0
        public void SimpleThreadPoolTestMethod()
        {
            SimpleThreadPool testThread = new SimpleThreadPool();
            bool             result     = testThread.RunThreadPool();

            Assert.IsTrue(result);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //Lockfree queue (bounded)
            //Lock queue(bounded)
            //Lock list(resizable)

            SimpleThreadPool threadPool = new SimpleThreadPool(4, 128);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates an in memory job store (<see cref="RAMJobStore" />)
        /// The thread priority is set to Thread.NORM_PRIORITY
        /// </summary>
        /// <param name="maxThreads">The number of threads in the thread pool</param>
        public virtual void CreateVolatileScheduler(int maxThreads)
        {
            SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads, ThreadPriority.Normal);

            threadPool.Initialize();
            IJobStore jobStore = new RAMJobStore();

            CreateScheduler(threadPool, jobStore);
        }
Ejemplo n.º 4
0
 public TestTaskScheduler(int threadCount, SimpleThreadPool threadPool = null)
 {
     threadPool = threadPool ?? CachingSimpleThreadPool.Instance;
     MaximumConcurrencyLevel = threadCount;
     lock (_lock)
     {
         _activeThreadCount = threadCount;
     }
     _events   = Enumerable.Range(0, threadCount).Select(_ => new AutoResetEvent(false)).ToArray();
     _runEvent = new AutoResetEvent(false);
     _threads  = Enumerable.Range(0, threadCount).Select(i => threadPool.Start(() => RunThread(_events[i]))).ToArray();
 }
Ejemplo n.º 5
0
        public Raft(StateMachine sm,
                    string RaftName     = null,
                    RaftConfig raftconf = null,
                    Zeze.Config config  = null,
                    string name         = "Zeze.Raft.Server")
        {
            if (null == raftconf)
            {
                raftconf = RaftConfig.Load();
            }
            raftconf.Verify();

            RaftConfig   = raftconf;
            sm.Raft      = this;
            StateMachine = sm;

            if (false == string.IsNullOrEmpty(RaftName))
            {
                raftconf.Name = RaftName;
            }

            if (null == config)
            {
                config = Zeze.Config.Load();
            }

            Server = new Server(this, name, config);
            if (Server.Config.AcceptorCount() != 0)
            {
                throw new Exception("Acceptor Found!");
            }
            if (Server.Config.ConnectorCount() != 0)
            {
                throw new Exception("Connector Found!");
            }
            if (RaftConfig.Nodes.Count < 3)
            {
                throw new Exception("Startup Nodes.Count Must >= 3.");
            }

            ImportantThreadPool = new SimpleThreadPool(5, $"Raft.{Name}");
            Server.CreateAcceptor(Server, raftconf);
            Server.CreateConnector(Server, raftconf);

            LogSequence = new LogSequence(this);

            RegisterInternalRpc();
            StartLeaderLostTimerTask();
            LogSequence.StartSnapshotPerDayTimer();
            AppDomain.CurrentDomain.ProcessExit += ProcessExit;
        }
Ejemplo n.º 6
0
        static OwinHttpWorker()
        {
            int threadSize = Environment.ProcessorCount + 1;

            if (threadSize < 2)
            {
                threadSize = 2;
            }
            if (threadSize > 64)
            {
                threadSize = 64;
            }
            _simpleThreadPool = new SimpleThreadPool(threadSize);
        }
Ejemplo n.º 7
0
        static OwinStream()
        {
            int num = checked (Environment.ProcessorCount + 1);

            if (num < 2)
            {
                num = 2;
            }
            if (num > 64)
            {
                num = 64;
            }
            _simpleThreadPool = new SimpleThreadPool(num);
        }
Ejemplo n.º 8
0
        public void Start()
        {
            var configFileNames = string.Join(",", Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, ConfigName));

            if (string.IsNullOrWhiteSpace(configFileNames))
            {
                return;
            }

            var instanceId = new SimpleInstanceIdGenerator().GenerateInstanceId();

            var threadPool = new SimpleThreadPool(threadCount: 2, threadPriority: ThreadPriority.Normal)
            {
                InstanceName = SchedulerName
            };

            threadPool.Initialize();

            var jobStore = new RAMJobStore
            {
                InstanceName = SchedulerName,
                InstanceId   = instanceId,
            };


            var jobInitializationPlugin = new ConfigFileProcessorPlugin
            {
                FileNames    = configFileNames,
                ScanInterval = QuartzConfigFileScanInterval.DisableScanning
            };

            DirectSchedulerFactory.Instance.CreateScheduler(
                SchedulerName,
                instanceId,
                threadPool,
                jobStore,
                new Dictionary <string, ISchedulerPlugin>
            {
                { SchedulerName, jobInitializationPlugin }
            },
                TimeSpan.Zero,
                TimeSpan.Zero);

            var scheduler = DirectSchedulerFactory.Instance.GetScheduler(SchedulerName);

            scheduler.JobFactory = _jobFactory;

            scheduler.Start();
        }
        public void Start()
        {
            var instanceId = new SimpleInstanceIdGenerator().GenerateInstanceId();

            var threadPool = new SimpleThreadPool(threadCount: 2, threadPriority: ThreadPriority.Normal)
            {
                InstanceName = SchedulerName
            };

            threadPool.Initialize();

            var jobstore = new RAMJobStore
            {
                InstanceName = SchedulerName,
                InstanceId   = instanceId,
            };

            var baseUri = new Uri(Assembly.GetExecutingAssembly().GetName().EscapedCodeBase);
            // ReSharper disable once AssignNullToNotNullAttribute
            var fileName = Path.Combine(Path.GetDirectoryName(baseUri.LocalPath), ConfigName);

            var jobInitializationPlugin = new ConfigFileProcessorPlugin
            {
                FileNames    = fileName,
                ScanInterval = QuartzConfigFileScanInterval.DisableScanning
            };

            DirectSchedulerFactory.Instance.CreateScheduler(
                SchedulerName,
                instanceId,
                threadPool,
                jobstore,
                new Dictionary <string, ISchedulerPlugin>
            {
                { SchedulerName, jobInitializationPlugin }
            },
                TimeSpan.Zero,
                TimeSpan.Zero);

            var scheduler = DirectSchedulerFactory.Instance.GetScheduler(SchedulerName);

            scheduler.JobFactory = _jobFactory;

            scheduler.Start();
        }
Ejemplo n.º 10
0
        public WebServer() : base(MainService.settings.http_port, MainService.settings.https_port, new ServerNameCertificateSelector(), IPAddress.Any)
        {
            // Set up the httpClient we will use for proxying requests
            WebRequestHandler webRequestHandler = new WebRequestHandler();

            webRequestHandler.AllowAutoRedirect      = false;                     // Do not hide redirects
            webRequestHandler.AllowPipelining        = false;                     // We would not want to pipeline a stream by accident, as it would delay other requests indefinitely.
            webRequestHandler.AutomaticDecompression = DecompressionMethods.None; // Use Blue Iris's compression for efficiency.
            httpClient = new HttpClient(webRequestHandler);

            pool = new SimpleThreadPool("Webserver Main Pool", 6, 256);
            if (!Debugger.IsAttached)
            {
                WindowsFirewallHelper.AuthorizeSelf(Globals.AssemblyName);
            }
            serverNameCertificateSelector = certificateSelector as ServerNameCertificateSelector;
            serverNameCertificateSelector.SetCertificate(null, HttpServer.GetSelfSignedCertificate());
        }
Ejemplo n.º 11
0
            private readonly LinkedList <Task> _taskQueue    = new LinkedList <Task>(); // All tasks that need executing.

            public TestTaskScheduler(int threadCount, SimpleThreadPool threadPool = null)
            {
                _disposeCts             = new CancellationTokenSource();
                _disposeToken           = _disposeCts.Token;
                MaximumConcurrencyLevel = threadCount;
                _threadPool             = threadPool ?? DefaultSimpleThreadPool.Instance;
                _threadEvents           = new AutoResetEvent[threadCount];
                _threadCompletionTasks  = new Task[threadCount];
                _threadExecutingTasks   = new Task[threadCount];
                _threadWaiting          = new Task[threadCount];
                for (int i = 0; i < threadCount; i++)
                {
                    int threadIndex = i;
                    _threadExecutingTasks[i] = null;
                    _threadWaiting[i]        = null;
                    var ev = new AutoResetEvent(false);
                    _threadEvents[i]          = ev;
                    _threadCompletionTasks[i] = _threadPool.Start(() => RunThread(threadIndex, ev));
                    _readyThreadIndexes.Enqueue(i);
                }
            }
Ejemplo n.º 12
0
 /// <summary>
 /// Performs an asynchronous, multi-threaded traceroute operation.
 /// </summary>
 /// <param name="threadPool">A thread pool upon which to execute the pings. It should contain as many threads as the <see cref="MaxHops"/> value.</param>
 /// <param name="token">An object which should be passed through to the OnHostResult method.</param>
 /// <param name="Target">The target host to ping.</param>
 /// <param name="MaxHops">The maximum number of hops to try.</param>
 /// <param name="OnHostResult">Callback method which will be called with the result of each individual ping.</param>
 /// <param name="PingTimeoutMs">Timeout in milliseconds after which the ping should be considered unsuccessful.</param>
 public static void TraceRoute(SimpleThreadPool threadPool, object token, IPAddress Target, byte MaxHops, Action <TraceRouteHostResult> OnHostResult, int PingTimeoutMs = 5000)
 {
     byte[] buffer = new byte[0];
     for (byte ttl = 1; ttl <= MaxHops; ttl++)
     {
         object state = new
         {
             token,
             ttl,
             Target,
             MaxHops,
             OnHostResult,
             PingTimeoutMs,
             buffer
         };
         threadPool.Enqueue(() =>
         {
             PingSync(state);
         });
     }
 }
Ejemplo n.º 13
0
        public void TestThreadPoolStopping()
        {
            SimpleThreadPool p = new SimpleThreadPool("Test Pool", 2, 6, 60000, true, (e, s) => Assert.Fail(e?.ToString() + " " + s));

            Thread.Sleep(250);

            Assert.AreEqual(2, p.MinThreads);
            Assert.AreEqual(6, p.MaxThreads);
            Assert.AreEqual(0, p.CurrentBusyThreads);
            Assert.AreEqual(2, p.CurrentIdleThreads);
            Assert.AreEqual(2, p.CurrentLiveThreads);

            p.Enqueue(() => { Thread.Sleep(1000); });

            Thread.Sleep(250);

            Assert.AreEqual(2, p.MinThreads);
            Assert.AreEqual(6, p.MaxThreads);
            Assert.AreEqual(1, p.CurrentBusyThreads);
            Assert.AreEqual(1, p.CurrentIdleThreads);
            Assert.AreEqual(2, p.CurrentLiveThreads);

            p.Stop();
            Thread.Sleep(250);

            Assert.AreEqual(2, p.MinThreads);
            Assert.AreEqual(6, p.MaxThreads);
            Assert.AreEqual(1, p.CurrentBusyThreads);
            Assert.AreEqual(0, p.CurrentIdleThreads);
            Assert.AreEqual(1, p.CurrentLiveThreads);

            Thread.Sleep(750);

            Assert.AreEqual(2, p.MinThreads);
            Assert.AreEqual(6, p.MaxThreads);
            Assert.AreEqual(0, p.CurrentBusyThreads);
            Assert.AreEqual(0, p.CurrentIdleThreads);
            Assert.AreEqual(0, p.CurrentLiveThreads);
        }
        public void TestPlugins()
        {
            StringBuilder result = new StringBuilder();

            IDictionary <string, ISchedulerPlugin> data = new Dictionary <string, ISchedulerPlugin>();

            data["TestPlugin"] = new TestPlugin(result);

            IThreadPool threadPool = new SimpleThreadPool(1, ThreadPriority.Normal);

            threadPool.Initialize();
            DirectSchedulerFactory.Instance.CreateScheduler(
                "MyScheduler", "Instance1", threadPool,
                new RAMJobStore(), data,
                TimeSpan.Zero, TimeSpan.Zero);


            IScheduler scheduler = DirectSchedulerFactory.Instance.GetScheduler("MyScheduler");

            scheduler.Start();
            scheduler.Shutdown();

            Assert.AreEqual("TestPlugin|MyScheduler|Start|Shutdown", result.ToString());
        }
Ejemplo n.º 15
0
        public void TestThreadExpiration()
        {
            SimpleThreadPool p = new SimpleThreadPool("Test Pool", 0, 2, 200, true, (e, s) => Assert.Fail(e?.ToString() + " " + s));

            Thread.Sleep(250);

            Assert.AreEqual(0, p.MinThreads);
            Assert.AreEqual(2, p.MaxThreads);
            Assert.AreEqual(0, p.CurrentBusyThreads);
            Assert.AreEqual(0, p.CurrentIdleThreads);
            Assert.AreEqual(0, p.CurrentLiveThreads);

            p.Enqueue(() => { Thread.Sleep(200); });

            Assert.AreEqual(0, p.MinThreads);
            Assert.AreEqual(2, p.MaxThreads);
            Assert.AreEqual(1, p.CurrentBusyThreads);
            Assert.AreEqual(0, p.CurrentIdleThreads);
            Assert.AreEqual(1, p.CurrentLiveThreads);

            Thread.Sleep(250);

            Assert.AreEqual(0, p.MinThreads);
            Assert.AreEqual(2, p.MaxThreads);
            Assert.AreEqual(0, p.CurrentBusyThreads);
            Assert.AreEqual(1, p.CurrentIdleThreads);
            Assert.AreEqual(1, p.CurrentLiveThreads);

            Thread.Sleep(250);

            Assert.AreEqual(0, p.MinThreads);
            Assert.AreEqual(2, p.MaxThreads);
            Assert.AreEqual(0, p.CurrentBusyThreads);
            Assert.AreEqual(0, p.CurrentIdleThreads);
            Assert.AreEqual(0, p.CurrentLiveThreads);
        }
Ejemplo n.º 16
0
        private void YoutubeLiveCheckTimer(IEnumerable <VtuberEntity> vtubers)
        {
            var threadPool = new SimpleThreadPool()
            {
                MaxThread = 20
            };

            foreach (var vtuber in vtubers.Where(v => !string.IsNullOrEmpty(v.YoutubeChannelId)))
            {
                threadPool.Actions.Enqueue(() =>
                {
                    if (!LastCheckYoutubeLiveStatus.ContainsKey(vtuber))
                    {
                        LastCheckYoutubeLiveStatus.Add(vtuber, new YoutubeVideo());
                    }
                    var onLive = YoutubeApi.NowLive(vtuber.YoutubeChannelId).GetAwaiter().GetResult();
                    if (onLive)
                    {
                        var channelInfo = YoutubeApi.GetYoutubeChannelAsync(vtuber.YoutubeChannelId).GetAwaiter().GetResult();
                        if (!LastCheckYoutubeLiveStatus[vtuber].IsLive || channelInfo.LiveVideoId != LastCheckYoutubeLiveStatus[vtuber].VideoId)
                        {
                            var live = YoutubeApi.GetYoutubeVideoAsync(channelInfo.LiveVideoId).GetAwaiter().GetResult();
                            if (!live.IsLive)
                            {
                                return;
                            }
                            if (!LastCheckYoutubeLiveStatus[vtuber].IsLive)
                            {
                                LastCheckYoutubeLiveStatus[vtuber] = live;
                                var recorder = new YoutubeLiveChatRecorder(live.LiveDetails.LiveChatId, vtuber, live.VideoId)
                                {
                                    VtuberList = vtubers.ToList()
                                };
                                recorder.StartRecord();
                                recorder.LiveStoppedEvent += (id, sender) =>
                                {
                                    LogHelper.Info($"{vtuber.OriginalName} 已停止直播,正在保存评论数据");
                                    live     = YoutubeApi.GetYoutubeVideoAsync(channelInfo.LiveVideoId).Retry(5).GetAwaiter().GetResult() ?? live;
                                    var info = new YoutubeLiveInfo()
                                    {
                                        Title     = live.Title,
                                        Channel   = live.ChannelId,
                                        BeginTime = live.LiveDetails?.ActualStartTime ?? default(DateTime),
                                        EndTime   = live.LiveDetails?.ActualEndTime ?? DateTime.Now,
                                        VideoId   = live.VideoId
                                    };
                                    _youtubeLiveCollection.ReplaceOne(v => v.VideoId == live.VideoId, info,
                                                                      new UpdateOptions()
                                    {
                                        IsUpsert = true
                                    });
                                    LogHelper.Info("保存完毕");
                                    VtuberStoppedYoutubeLiveEvent?.Invoke(vtuber, live);
                                };
                                recorder.VtuberCommentedEvent += (author, message, sender) => VtuberCommentedYoutubeLiveEvent?.Invoke(author, sender.Vtuber, message, live);
                                VtuberBeginYoutubeLiveEvent?.Invoke(vtuber, live);
                            }
                        }
                    }
                    if (!onLive)
                    {
                        LastCheckYoutubeLiveStatus[vtuber] = new YoutubeVideo();
                    }
                });
            }
            threadPool.Run();
        }