timeout/

directory
v1.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2021 License: Apache-2.0

README

Timeout interceptor

In this example, we will try to create unary grpc server timeout interceptor enabled.

Please make sure panic interceptor to be added at last in chain of interceptors.

Table of Contents generated with DocToc

Quick start

Get rk-grpc package from the remote repository.

go get -u github.com/rookie-ninja/rk-grpc
Code
import     "github.com/rookie-ninja/rk-grpc/interceptor/timeout"
    // *************************************
    // ********** Unary Server *************
    // *************************************
    opts := []grpc.ServerOption{
        grpc.ChainUnaryInterceptor(
            rkgrpctimeout.UnaryServerInterceptor(
                rkgrpctimeout.WithTimeoutAndResp(time.Second, nil),
            ),
        ),
    }

    // *************************************
    // ********** Stream Server ************
    // *************************************
    opts := []grpc.ServerOption {
        grpc.ChainStreamInterceptor(
            rkgrpctimeout.StreamServerInterceptor(
                rkgrpctimeout.WithTimeoutAndResp(time.Second, nil),
            ),
        ),
    }

Options

Name Default Description
WithEntryNameAndType(entryName, entryType string) entryName=grpc, entryType=grpc entryName and entryType will be used to distinguish options if there are multiple interceptors in single process.
WithTimeoutAndResp(time.Duration, error) 5*time.Second, codes.Canceled Set timeout interceptor with all methods.
WithTimeoutAndRespByPath(method string, time.Duration, error) "", 5*time.Second, codes.Canceled Set timeout interceptor with specified method.
Context Usage
Name Functionality
rkgrpcctx.GetLogger(context.Context) Get logger generated by log interceptor. If there are X-Request-Id or X-Trace-Id as headers in incoming and outgoing metadata, then loggers will has requestId and traceId attached by default.
rkgrpcctx.GetEvent(context.Context) Get event generated by log interceptor. Event would be printed as soon as RPC finished. ClientStream is a little bit tricky. Please refer rkgrpcctx.FinishClientStream() function for details.
rkgrpcctx.GetIncomingHeaders(context.Context) Get incoming header. ClientStream is a little bit tricky, please use stream.Header() instead.
rkgrpcctx.AddHeaderToClient(ctx, "k", "v") Add k/v to headers which would be sent to client.
rkgrpcctx.AddHeaderToServer(ctx, "k", "v") Add k/v to headers which would be sent to server.

Example

Unary

Create a simple unary server and client with bellow protocol buffer files. We will enable log interceptor at the same time.

Start server and client
$ go run greeter-server.go
$ go run greeter-client.go
Output
  • Server side (event)
------------------------------------------------------------------------
endTime=2021-10-30T00:15:14.386439+08:00
startTime=2021-10-30T00:15:13.383453+08:00
elapsedNano=1002992959
timezone=CST
ids={"eventId":"a607b956-b9e4-43fc-b707-74e343684855"}
app={"appName":"rk","appVersion":"","entryName":"grpc","entryType":"grpc"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"192.168.101.5","os":"darwin","realm":"*","region":"*"}
payloads={"grpcMethod":"SayHello","grpcService":"Greeter","grpcType":"unaryServer","gwMethod":"","gwPath":"","gwScheme":"","gwUserAgent":""}
error={"rpc error: code = Canceled desc = Request timed out!":1}
counters={"timeout":1}
pairs={}
timing={}
remoteAddr=127.0.0.1:61786
operation=/Greeter/SayHello
resCode=Canceled
eventStatus=Ended
EOE
  • Client side (zap & event)
2021-10-30T00:15:14.387+0800    FATAL   client/greeter-client.go:40     Failed to send request to server.       {"error": "rpc error: code = Canceled desc = Request timed out!"}
------------------------------------------------------------------------
endTime=2021-10-30T00:15:14.387264+08:00
startTime=2021-10-30T00:15:13.382787+08:00
elapsedNano=1004483920
timezone=CST
ids={"eventId":"1e176aa9-46a5-44a0-8507-9b69120cd600"}
app={"appName":"rk","appVersion":"","entryName":"grpc","entryType":"grpc"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"192.168.101.5","os":"darwin","realm":"*","region":"*"}
payloads={"grpcMethod":"SayHello","grpcService":"Greeter","grpcType":"unaryClient","remoteIp":"localhost","remotePort":"8080"}
error={"rpc error: code = Canceled desc = Request timed out!":1}
counters={}
pairs={}
timing={}
remoteAddr=localhost:8080
operation=/Greeter/SayHello
resCode=Canceled
eventStatus=Ended
EOE
Code

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL