Exemple #1
0
        private async Task Pong(Cluster _cluster)
        {
            try
            {
                string kind = "pong";
                var    cmd  = new PongCmd {
                    Counter = pongCounter
                };
                var res = await _cluster.RequestAsync <PongResponse>("Worker2/101", kind, cmd, new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);

                if (true.Equals(res?.Success))
                {
                    pongCounter = res.Counter;
                    this.logger.LogInformation("Pong counter is {counter}", pongCounter);
                }
                else
                {
                    errors++;
                    this.logger.LogInformation("Failed to call {Kind} {errors}'", kind, errors);
                }
            }
            catch (Exception e)
            {
                this.logger.LogError(e.ToString());
            }
        }
        public override async Task ReceiveAsync(IContext context)
        {
            Task task = context.Message switch
            {
                PongCmd cmd => HandlePong(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 PongGrain");
                context.Respond(new DeadLetterResponse
                {
                    Target = context.Self
                });
            }
        }
Exemple #3
0
        public async Task PongAsync()
        {
            ThrowIfDisposed();

            var cmdPayload = PongCmd.Generate();

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

            var cmdPayload = PongCmd.Generate();

            _connection.WithWriteLock(writer =>
            {
                writer.Write(cmdPayload);
                writer.Flush();
            });
        }
        public async Task HandlePong(IContext context, PongCmd cmd)
        {
            logger.LogInformation($"PongGrain.HandlePong");

            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 PongResponse
                {
                    Success = true,
                    Counter = cmd.Counter + 1
                });
            }
            catch (Exception e)
            {
                this.logger.LogError(e, "Failed in PongGrain");
                context.Respond(new PongResponse()
                {
                    Success = false, ErrorMessage = "Internal server error"
                });
            }
        }
        public async Task Run(Cluster _cluster)
        {
            this.logger.LogInformation("MainWorker start");

            int    errors  = 0;
            int    counter = 1;
            string kind    = "pong";
            await Task.Delay(5000);

            while (true)
            {
                try
                {
                    var cmd = new PongCmd {
                        Counter = counter
                    };
                    var res = await _cluster.RequestAsync <PongResponse>("Worker2/101", kind, cmd, new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);

                    if (true.Equals(res?.Success))
                    {
                        counter = res.Counter;
                        this.logger.LogInformation("Pong counter is {counter}", counter);
                    }
                    else
                    {
                        errors++;
                        this.logger.LogInformation("Failed to call {Kind}'", kind);
                    }
                    await Task.Delay(1000);
                }
                catch (Exception e)
                {
                    this.logger.LogError(e.ToString());
                }
            }
        }