Ejemplo n.º 1
0
        protected void Listen()
        {
            while (true)
            {
                try
                {
                    ConfigurationOptions config = ConfigurationOptions.Parse(_redisOptions.ConnectionString);
                    config.ConnectTimeout = 10000;
                    config.ConnectRetry   = Int32.MaxValue;
                    config.SyncTimeout    = 2500;
                    config.ResolveDns     = true;

                    _multiplexer = ConnectionMultiplexer.Connect(config);

                    _multiplexer.InternalError += (sender, e) => {
                        if (e.Exception != null)
                        {
                            _telemetry.Exception(e.Exception);
                        }
                    };

                    _multiplexer.ConnectionFailed += (sender, e) => {
                        if (e.Exception != null)
                        {
                            _telemetry.Exception(e.Exception);
                        }
                    };

                    _multiplexer.ConnectionRestored += (sender, e) =>
                    {
                        if (e.Exception != null)
                        {
                            _telemetry.Exception(e.Exception);
                        }
                    };

                    break;
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Failed to connect to Redis");
                }
            }

            _multiplexer.PreserveAsyncOrder = false;
            _logger.LogInformation($"Client connected to Redis instance: {_multiplexer.IsConnected}");


            try
            {
                _subscriber.Subscribe(new RedisChannel(_inputChannel, RedisChannel.PatternMode.Pattern), (c, v) => {
                    MessageReceived(c, v);
                });
                _logger.LogInformation($"Client listening on channel {_inputChannel}");
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Failed to start listener");
            }
        }
Ejemplo n.º 2
0
        public Task Publish(string channel, byte[] data)
        {
            try
            {
                _redisPolicy.Execute(() => {
                    _subscriber.Publish(channel, data);
                });
            }
            catch (Exception ex)
            {
                _telemetry.Exception(ex);
                throw;
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 3
0
        protected ActorTimer RegisterTimer(Func <Task> callback, TimeSpan interval, int?runCount = null, Boolean autoStart = true, Boolean sync = true)
        {
            Func <Task> innerCallback;

            if (sync)
            {
                innerCallback = () => { return(QueueExecuteInline(callback)); };
            }
            else
            {
                innerCallback = () => { return(ExecuteInline(callback)); };
            }

            var timer = new ActorTimer(innerCallback, interval, runCount, _timersCts.Token);

            _timers.Add(timer);

            timer.OnFinish(async() => { _timers.Remove(timer); });

            timer.OnError(async(ex) => { _telemetry.Exception(ex); });

            if (autoStart)
            {
                timer.Start();
            }

            return(timer);
        }
Ejemplo n.º 4
0
        public void ProcessServerInput()
        {
            _socketServer.DataReceived += (s, e) => {
                Task.Run(() =>
                {
                    try
                    {
                        var received = e.SocketData;

                        Interlocked.Increment(ref _inputProccessBacklog);

                        if (received.Data != null)
                        {
                            var remoteMessage = _serializer.Deserialize <RemoteStageMessage>(received.Data);

                            if (remoteMessage == null)
                            {
                                Interlocked.Decrement(ref _inputProccessBacklog);
                                return;
                            }

                            if (remoteMessage.MessageType == RemoteMessageType.ActorResponse)
                            {
                                //ReceivedActorResponse(remoteMessage.ActorResponse);

                                //should get responses in server, drop
                            }
                            if (remoteMessage.MessageType == RemoteMessageType.ActorRequest)
                            {
                                var processTask = ReceivedActorRequest(remoteMessage.ActorRequest, received.StageId).ConfigureAwait(false);
                            }
                            if (remoteMessage.MessageType == RemoteMessageType.PingRequest)
                            {
                                var processTask = ProcessPingRequest(received.StageId, remoteMessage.Ping).ConfigureAwait(false);
                            }

                            Interlocked.Decrement(ref _inputProccessBacklog);
                        }
                    }
                    catch (Exception ex)
                    {
                        _telemetry.Exception(ex);
                    }
                }).ConfigureAwait(false);
            };
        }
Ejemplo n.º 5
0
        public void DeactivateInstance(string key)
        {
            try
            {
                if (_actorInstances.TryRemove(key, out var instance))
                {
                    _logger.LogDebug("Deactivated instance for {0}. Actor Id : {1}. Requested", instance.ActorTypeName, instance.ActorId);
                    instance.Instance.Dispose();
                    instance = null;

                    _deactivatedInstancesMeter.Tick();
                }
            }
            catch (Exception ex)
            {
                _telemetry.Exception(ex);
            }
        }
Ejemplo n.º 6
0
        public void ProcessServerInput()
        {
            _socketClient.DataReceived += (s, e) =>
            {
                Task.Run(() =>
                {
                    try
                    {
                        var received = e.SocketData;

                        if (received.Data != null)
                        {
                            var remoteMessage = _serializer.Deserialize <RemoteStageMessage>(received.Data);

                            if (remoteMessage.MessageType == RemoteMessageType.ActorResponse)
                            {
                                var messageId = remoteMessage.ActorResponse.Id;
                                if (_serverResponseBuffer.TryGetValue(messageId, out var queueItem))
                                {
                                    var semaphore = queueItem.Item1;
                                    _serverResponseBuffer[messageId] = new Tuple <SemaphoreSlim, ActorResponse>(queueItem.Item1, remoteMessage.ActorResponse);
                                    semaphore.Release();
                                }
                            }
                            if (remoteMessage.MessageType == RemoteMessageType.PingResponse)
                            {
                                if (_pingResponseBuffer.TryGetValue(remoteMessage.Ping.Id, out var semaphore))
                                {
                                    semaphore.Release();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _telemetry.Exception(ex);
                    }
                }).ConfigureAwait(false);
            };
        }
Ejemplo n.º 7
0
        public async Task <Boolean> Listen()
        {
            while (true)
            {
                try
                {
                    ConfigurationOptions config = ConfigurationOptions.Parse(_redisOptions.ConnectionString);
                    config.ConnectTimeout = 10000;
                    config.ConnectRetry   = Int32.MaxValue;
                    config.SyncTimeout    = 2500;
                    config.ResolveDns     = true;

                    _multiplexer = ConnectionMultiplexer.Connect(config);

                    _multiplexer.InternalError += (sender, e) => {
                        if (e.Exception != null)
                        {
                            _telemetry.Exception(e.Exception);
                        }
                    };

                    _multiplexer.ConnectionFailed += (sender, e) => {
                        if (e.Exception != null)
                        {
                            _telemetry.Exception(e.Exception);
                        }
                    };

                    _multiplexer.ConnectionRestored += (sender, e) =>
                    {
                        if (e.Exception != null)
                        {
                            _telemetry.Exception(e.Exception);
                        }
                    };

                    break;
                }
                catch (Exception ex)
                {
                    _logger.LogCritical("Failed to connect to Redis");
                }
                await Task.Delay(1000);
            }

            _multiplexer.PreserveAsyncOrder = false;

            _guid = _localStage.StageGuid;


            try
            {
                await _subscriber.SubscribeAsync(new RedisChannel(_inputChannel, RedisChannel.PatternMode.Pattern), (c, v) => {
                    MessageReceived(c, v);
                });

                _logger.LogInformation($"Listening on channel {_inputChannel}");
            }
            catch (Exception ex)
            {
                _telemetry.Exception(ex);
                _logger.LogCritical("Failed to start listener");
            }

            RedisPing();

            return(true);
        }