Beispiel #1
0
        public void of_http_requests_routing()
        {
            const int iterations = 100000;

            IPublisher inputBus           = new NoopPublisher();
            var        bus                = InMemoryBus.CreateTest();
            var        queue              = new QueuedHandlerThreadPool(bus, "Test", true, TimeSpan.FromMilliseconds(50));
            var        multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            var        providers          = new HttpAuthenticationProvider[] { new AnonymousHttpAuthenticationProvider() };
            var        httpService        = new HttpService(ServiceAccessibility.Public, inputBus,
                                                            new TrieUriRouter(), multiQueuedHandler, false, "http://localhost:12345/");

            HttpService.CreateAndSubscribePipeline(bus, providers);

            var fakeController = new FakeController(iterations, null);

            httpService.SetupController(fakeController);

            httpService.Handle(new SystemMessage.SystemInit());

            var rnd = new Random();
            var sw  = Stopwatch.StartNew();

            var httpClient = new HttpAsyncClient();

            for (int i = 0; i < iterations; ++i)
            {
                var route = fakeController.BoundRoutes[rnd.Next(0, fakeController.BoundRoutes.Count)];

                switch (route.Item2)
                {
                case HttpMethod.Get:
                    httpClient.Get(route.Item1, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                case HttpMethod.Post:
                    httpClient.Post(route.Item1, "abracadabra", ContentType.Json, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                case HttpMethod.Delete:
                    httpClient.Delete(route.Item1, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                case HttpMethod.Put:
                    httpClient.Put(route.Item1, "abracadabra", ContentType.Json, TimeSpan.FromMilliseconds(10000), x => { }, x => { throw new Exception(); });
                    break;

                default:
                    throw new Exception();
                }
            }

            fakeController.CountdownEvent.Wait();
            sw.Stop();

            Console.WriteLine("{0} request done in {1} ({2:0.00} per sec)", iterations, sw.Elapsed, 1000.0 * iterations / sw.ElapsedMilliseconds);

            httpService.Shutdown();
            multiQueuedHandler.Stop();
        }
 private void Authenticate(IncomingHttpRequestMessage message)
 {
     try {
         foreach (var provider in _providers)
         {
             if (provider.Authenticate(message))
             {
                 break;
             }
         }
     } catch {
         HttpAuthenticationProvider.ReplyUnauthorized(message.Entity);
     }
 }
Beispiel #3
0
        public void SetUp()
        {
            _bus = new InMemoryBus(string.Format("bus_{0}", _serverEndPoint.Port));

            {
                var pipelineBus = InMemoryBus.CreateTest();
                var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", true, TimeSpan.FromMilliseconds(50));
                _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
                var httpAuthenticationProviders = new HttpAuthenticationProvider[] { new AnonymousHttpAuthenticationProvider() };

                _service = new HttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                           _multiQueuedHandler, false, _serverEndPoint.ToHttpUrl());
                HttpService.CreateAndSubscribePipeline(pipelineBus, httpAuthenticationProviders);
                _client = new HttpAsyncClient();
            }

            HttpBootstrap.Subscribe(_bus, _service);
        }
Beispiel #4
0
        public void SetUp()
        {
            _bus = new InMemoryBus($"bus_{_serverEndPoint.Port}");
            var pipelineBus = InMemoryBus.CreateTest();
            var queue       = new QueuedHandlerThreadPool(pipelineBus, "Test", new QueueStatsManager(), true, TimeSpan.FromMilliseconds(50));

            _multiQueuedHandler = new MultiQueuedHandler(new IQueuedHandler[] { queue }, null);
            _multiQueuedHandler.Start();
            var httpAuthenticationProviders = new HttpAuthenticationProvider[]
            { new AnonymousHttpAuthenticationProvider() };

            _service = new KestrelHttpService(ServiceAccessibility.Private, _bus, new NaiveUriRouter(),
                                              _multiQueuedHandler, false, null, 0, false, _serverEndPoint);
            KestrelHttpService.CreateAndSubscribePipeline(pipelineBus, httpAuthenticationProviders);
            _server = new TestServer(
                new WebHostBuilder()
                .UseStartup(new HttpServiceStartup(_service)));
            _httpMessageHandler = _server.CreateHandler();
            _client             = new HttpAsyncClient(_timeout, _httpMessageHandler);

            HttpBootstrap.Subscribe(_bus, _service);
        }
Beispiel #5
0
        public PlaygroundVNode(PlaygroundVNodeSettings vNodeSettings)
        {
            _tcpEndPoint  = vNodeSettings.ExternalTcpEndPoint;
            _httpEndPoint = vNodeSettings.ExternalHttpEndPoint;

            _mainBus    = new InMemoryBus("MainBus");
            _controller = new PlaygroundVNodeController(Bus, _httpEndPoint);
            _mainQueue  = new QueuedHandler(_controller, "MainQueue");
            _controller.SetMainQueue(MainQueue);

            // MONITORING
            var monitoringInnerBus = new InMemoryBus("MonitoringInnerBus", watchSlowMsg: false);
            var monitoringQueue    = new QueuedHandler(
                monitoringInnerBus, "MonitoringQueue", true, TimeSpan.FromMilliseconds(100));

            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.SystemInit, Message>());
            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.StateChangeMessage, Message>());
            Bus.Subscribe(monitoringQueue.WidenFrom <SystemMessage.BecomeShuttingDown, Message>());


            // MISC WORKERS
            _workerBuses = Enumerable.Range(0, vNodeSettings.WorkerThreads).Select(queueNum =>
                                                                                   new InMemoryBus(string.Format("Worker #{0} Bus", queueNum + 1),
                                                                                                   watchSlowMsg: true,
                                                                                                   slowMsgThreshold: TimeSpan.FromMilliseconds(50))).ToArray();
            _workersHandler = new MultiQueuedHandler(
                vNodeSettings.WorkerThreads,
                queueNum => new QueuedHandlerThreadPool(_workerBuses[queueNum],
                                                        string.Format("Worker #{0}", queueNum + 1),
                                                        groupName: "Workers",
                                                        watchSlowMsg: true,
                                                        slowMsgThreshold: TimeSpan.FromMilliseconds(50)));

            // AUTHENTICATION INFRASTRUCTURE
            var dispatcher                       = new IODispatcher(_mainBus, new PublishEnvelope(_workersHandler, crossThread: true));
            var passwordHashAlgorithm            = new Rfc2898PasswordHashAlgorithm();
            var internalAuthenticationProvider   = new InternalAuthenticationProvider(dispatcher, passwordHashAlgorithm, 1000);
            var passwordChangeNotificationReader = new PasswordChangeNotificationReader(_mainQueue, dispatcher);

            _mainBus.Subscribe <SystemMessage.SystemStart>(passwordChangeNotificationReader);
            _mainBus.Subscribe <SystemMessage.BecomeShutdown>(passwordChangeNotificationReader);
            _mainBus.Subscribe(internalAuthenticationProvider);
            _mainBus.Subscribe(dispatcher);

            SubscribeWorkers(bus =>
            {
                bus.Subscribe(dispatcher.ForwardReader);
                bus.Subscribe(dispatcher.BackwardReader);
                bus.Subscribe(dispatcher.Writer);
                bus.Subscribe(dispatcher.StreamDeleter);
            });

            // TCP
            var tcpService = new TcpService(
                MainQueue, _tcpEndPoint, _workersHandler, TcpServiceType.External, TcpSecurityType.Normal, new ClientTcpDispatcher(),
                ESConsts.ExternalHeartbeatInterval, ESConsts.ExternalHeartbeatTimeout, internalAuthenticationProvider, null);

            Bus.Subscribe <SystemMessage.SystemInit>(tcpService);
            Bus.Subscribe <SystemMessage.SystemStart>(tcpService);
            Bus.Subscribe <SystemMessage.BecomeShuttingDown>(tcpService);

            // HTTP
            {
                var httpAuthenticationProviders = new HttpAuthenticationProvider[]
                {
                    new BasicHttpAuthenticationProvider(internalAuthenticationProvider),
                    new TrustedHttpAuthenticationProvider(),
                    new AnonymousHttpAuthenticationProvider()
                };

                var httpPipe        = new HttpMessagePipe();
                var httpSendService = new HttpSendService(httpPipe, forwardRequests: false);
                _mainBus.Subscribe <SystemMessage.StateChangeMessage>(httpSendService);
                _mainBus.Subscribe(new WideningHandler <HttpMessage.SendOverHttp, Message>(_workersHandler));
                SubscribeWorkers(bus =>
                {
                    bus.Subscribe <HttpMessage.HttpSend>(httpSendService);
                    bus.Subscribe <HttpMessage.HttpSendPart>(httpSendService);
                    bus.Subscribe <HttpMessage.HttpBeginSend>(httpSendService);
                    bus.Subscribe <HttpMessage.HttpEndSend>(httpSendService);
                    bus.Subscribe <HttpMessage.SendOverHttp>(httpSendService);
                });

                _httpService = new HttpService(ServiceAccessibility.Private, _mainQueue, new TrieUriRouter(),
                                               _workersHandler, vNodeSettings.HttpPrefixes);

                _mainBus.Subscribe <SystemMessage.SystemInit>(_httpService);
                _mainBus.Subscribe <SystemMessage.BecomeShuttingDown>(_httpService);
                _mainBus.Subscribe <HttpMessage.PurgeTimedOutRequests>(_httpService);
                HttpService.SetupController(new AdminController(_mainQueue));
                HttpService.SetupController(new PingController());
                HttpService.SetupController(new StatController(monitoringQueue, _workersHandler));
                HttpService.SetupController(new AtomController(httpSendService, _mainQueue, _workersHandler));
                HttpService.SetupController(new GuidController(_mainQueue));
                HttpService.SetupController(new UsersController(httpSendService, _mainQueue, _workersHandler));

                SubscribeWorkers(bus => HttpService.CreateAndSubscribePipeline(bus, httpAuthenticationProviders));
            }


            // TIMER
            _timerService = new TimerService(new ThreadBasedScheduler(new RealTimeProvider()));
            Bus.Subscribe <TimerMessage.Schedule>(TimerService);

            monitoringQueue.Start();
            MainQueue.Start();
        }