public async Task <HelloResponse> SayHello(HelloRequest request, CancellationToken ct, GrainCallOptions options = null)
        {
            options = options ?? GrainCallOptions.Default;

            var gr = new GrainRequest
            {
                MethodIndex = 0,
                MessageData = request.ToByteString()
            };

            async Task <HelloResponse> Inner()
            {
                //resolve the grain
                var(pid, statusCode) = await _cluster.GetAsync(_id, "HelloGrain", ct);

                if (statusCode != ResponseStatusCode.OK)
                {
                    throw new Exception($"Get PID failed with StatusCode: {statusCode}");
                }

                //request the RPC method to be invoked
                var res = await _cluster.System.Root.RequestAsync <object>(pid, gr, ct);

                //did we get a response?
                if (res is GrainResponse grainResponse)
                {
                    return(HelloResponse.Parser.ParseFrom(grainResponse.MessageData));
                }

                //did we get an error response?
                if (res is GrainErrorResponse grainErrorResponse)
                {
                    throw new Exception(grainErrorResponse.Err);
                }
                throw new NotSupportedException();
            }

            for (int i = 0; i < options.RetryCount; i++)
            {
                try
                {
                    return(await Inner());
                }
                catch (Exception)
                {
                    if (options.RetryAction != null)
                    {
                        await options.RetryAction(i);
                    }
                }
            }
            return(await Inner());
        }
Exemple #2
0
        public async Task <HelloResponse> SayHello(HelloRequest request, CancellationToken ct)
        {
            var gr = new GrainRequest
            {
                MethodIndex = 0,
                MessageData = request.ToByteString()
            };

            async Task <HelloResponse> Inner()
            {
                //resolve the grain
                var(pid, statusCode) = await Cluster.GetAsync(_id, "HelloGrain", ct);

                if (statusCode != ResponseStatusCode.OK)
                {
                    throw new Exception($"Get PID failed with StatusCode: {statusCode}");
                }

                //request the RPC method to be invoked
                var res = await pid.RequestAsync <object>(gr, ct);

                //did we get a response?
                if (res is GrainResponse grainResponse)
                {
                    return(HelloResponse.Parser.ParseFrom(grainResponse.MessageData));
                }

                //did we get an error response?
                if (res is GrainErrorResponse grainErrorResponse)
                {
                    throw new Exception(grainErrorResponse.Err);
                }
                throw new NotSupportedException();
            }

            for (int i = 0; i < 4; i++)
            {
                try
                {
                    return(await Inner());
                }
                catch (Exception x)
                {
                    //ignore, TODO: exponential backoff?
                }
            }
            return(await Inner());
        }
        public async Task <HelloResponse> SayHello(HelloRequest request)
        {
            var pid = await Cluster.GetAsync(_id, "HelloGrain");

            var gr = new GrainRequest
            {
                Method      = "SayHello",
                MessageData = request.ToByteString()
            };
            var res = await pid.RequestAsync <object>(gr);

            if (res is GrainResponse grainResponse)
            {
                return(HelloResponse.Parser.ParseFrom(grainResponse.MessageData));
            }
            if (res is GrainErrorResponse grainErrorResponse)
            {
                throw new Exception(grainErrorResponse.Err);
            }
            throw new NotSupportedException();
        }