示例#1
0
        private async Task Ping(Cluster _cluster)
        {
            try
            {
                string kind = "ping";
                var    cmd  = new PingCmd {
                    Counter = pingCounter
                };
                var res = await _cluster.RequestAsync <PingResponse>("Worker1/101", kind, cmd, new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);

                if (res.Success)
                {
                    pingCounter = res.Counter;
                    this.logger.LogInformation("Ping counter is {counter}", pingCounter);
                }
                else
                {
                    errors++;
                    this.logger.LogInformation("Failed to call {Kind} {errors}'", kind, errors);
                }
            }
            catch (Exception e)
            {
                this.logger.LogError(e.ToString());
            }
        }
示例#2
0
        public override async Task ReceiveAsync(IContext context)
        {
            Task task = context.Message switch
            {
                PingCmd cmd => HandlePing(context, cmd),
                ExceptionPoison cmd => HandleExceptionPoison(context, cmd),
                NoAnswerPoison cmd => HandleNoAnswerPoison(context, cmd),
                SleepPoison cmd => HandleSleepPoison(context, cmd),

                Started _ => Started(context),
                _ => base.ReceiveAsync(context)
            };

            try
            {
                await task;
            }
            catch (Exception e)
            {
                this.logger.LogError(e, "Failed PingGrain");
                context.Respond(new DeadLetterResponse
                {
                    Target = context.Self
                });
            }
        }
示例#3
0
        public async Task PingAsync()
        {
            ThrowIfDisposed();

            var cmdPayload = PingCmd.Generate();

            await _connection.WithWriteLockAsync(async writer =>
            {
                await writer.WriteAsync(cmdPayload).ConfigureAwait(false);
                await writer.FlushAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
示例#4
0
        public void Ping()
        {
            ThrowIfDisposed();

            var cmdPayload = PingCmd.Generate();

            _connection.WithWriteLock(writer =>
            {
                writer.Write(cmdPayload);
                writer.Flush();
            });
        }
示例#5
0
        private static NatsServerInfo VerifyConnection(Host host, ConnectionInfo connectionInfo, Socket socket, Func <IOp> readOne)
        {
            if (!socket.Connected)
            {
                throw NatsException.FailedToConnectToHost(host, "No connection could be established.");
            }

            var op = readOne();

            if (op == null)
            {
                throw NatsException.FailedToConnectToHost(host, "Expected to get INFO after establishing connection. Got nothing.");
            }

            var infoOp = op as InfoOp;

            if (infoOp == null)
            {
                throw NatsException.FailedToConnectToHost(host, $"Expected to get INFO after establishing connection. Got {op.GetType().Name}.");
            }

            Logger.Debug($"Got INFO during connect. {infoOp.GetAsString()}");

            var serverInfo  = NatsServerInfo.Parse(infoOp.Message);
            var credentials = host.HasNonEmptyCredentials() ? host.Credentials : connectionInfo.Credentials;

            if (serverInfo.AuthRequired && (credentials == null || credentials == Credentials.Empty))
            {
                throw NatsException.MissingCredentials(host);
            }

            socket.Send(ConnectCmd.Generate(connectionInfo.Verbose, credentials));
            socket.Send(PingCmd.Generate());

            op = readOne();
            if (op == null)
            {
                throw NatsException.FailedToConnectToHost(host, "Expected to read something after CONNECT and PING. Got nothing.");
            }

            if (op is ErrOp)
            {
                throw NatsException.FailedToConnectToHost(host, $"Expected to get PONG after sending CONNECT and PING. Got {op.GetAsString()}.");
            }

            if (!socket.Connected)
            {
                throw NatsException.FailedToConnectToHost(host, "No connection could be established.");
            }

            return(serverInfo);
        }
示例#6
0
        public async Task HandlePing(IContext context, PingCmd cmd)
        {
            logger.LogInformation($"PingGrain.HandlePing");

            if (this.exceptionCounter > 0)
            {
                this.exceptionCounter--;
                throw new Exception("Exception poison");
            }


            try
            {
                if (this.noAnswerCounter > 0)
                {
                    this.noAnswerCounter--;
                    return;
                }

                if (this.sleepCounter > 0)
                {
                    this.sleepCounter--;
                    await Task.Delay(10 * 1000);
                }

                context.Respond(new PingResponse
                {
                    Success = true,
                    Counter = cmd.Counter + 1
                });
            }
            catch (Exception e)
            {
                this.logger.LogError(e, "Failed in HandlePing");
                context.Respond(new PingResponse()
                {
                    Success = false, ErrorMessage = "Internal server error"
                });
            }
        }