Package list golang-github-go-kit-kit / e9965a6
adds gRPC ClientAfter handler and multiple calls to Client Before/After funcs Bas van Beek 4 years ago
4 changed file(s) with 35 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
2121 dec DecodeResponseFunc
2222 grpcReply reflect.Type
2323 before []RequestFunc
24 after []ClientResponseFunc
2425 }
2526
2627 // NewClient constructs a usable Client for a single remote endpoint.
5354 ).Interface(),
5455 ),
5556 before: []RequestFunc{},
57 after: []ClientResponseFunc{},
5658 }
5759 for _, option := range options {
5860 option(c)
6668 // ClientBefore sets the RequestFuncs that are applied to the outgoing gRPC
6769 // request before it's invoked.
6870 func ClientBefore(before ...RequestFunc) ClientOption {
69 return func(c *Client) { c.before = before }
71 return func(c *Client) { c.before = append(c.before, before...) }
72 }
73
74 // ClientAfter sets the ClientResponseFuncs that are applied to the incoming
75 // gRPC response prior to it being decoded. This is useful for obtaining
76 // response metadata and adding onto the context prior to decoding.
77 func ClientAfter(after ...ClientResponseFunc) ClientOption {
78 return func(c *Client) { c.after = append(c.after, after...) }
7079 }
7180
7281 // Endpoint returns a usable endpoint that will invoke the gRPC specified by the
8796 }
8897 ctx = metadata.NewContext(ctx, *md)
8998
99 var header, trailer metadata.MD
90100 grpcReply := reflect.New(c.grpcReply).Interface()
91 if err = grpc.Invoke(ctx, c.method, req, grpcReply, c.client); err != nil {
101 if err = grpc.Invoke(
102 ctx, c.method, req, grpcReply, c.client,
103 grpc.Header(&header), grpc.Trailer(&trailer),
104 ); err != nil {
92105 return nil, err
106 }
107
108 for _, f := range c.after {
109 ctx = f(ctx, &header, &trailer)
93110 }
94111
95112 response, err := c.dec(ctx, grpcReply)
1111 binHdrSuffix = "-bin"
1212 )
1313
14 // RequestFunc may take information from an gRPC request and put it into a
15 // request context. In Servers, BeforeFuncs are executed prior to invoking the
16 // endpoint. In Clients, BeforeFuncs are executed after creating the request
14 // RequestFunc may take information from a gRPC request and put it into a
15 // request context. In Servers, RequestFuncs are executed prior to invoking the
16 // endpoint. In Clients, RequestFuncs are executed after creating the request
1717 // but prior to invoking the gRPC client.
1818 type RequestFunc func(context.Context, *metadata.MD) context.Context
1919
20 // ResponseFunc may take information from a request context and use it to
20 // ServerResponseFunc may take information from a request context and use it to
2121 // manipulate the gRPC metadata header. ResponseFuncs are only executed in
2222 // servers, after invoking the endpoint but prior to writing a response.
23 type ResponseFunc func(context.Context, *metadata.MD)
23 type ServerResponseFunc func(context.Context, *metadata.MD)
24
25 // ClientResponseFunc may take information from a gRPC metadata header and/or
26 // trailer and make the responses available for consumption. ClientResponseFuncs
27 // are only executed in clients, after a request has been made, but prior to it
28 // being decoded.
29 type ClientResponseFunc func(ctx context.Context, header *metadata.MD, trailer *metadata.MD) context.Context
2430
2531 // SetResponseHeader returns a ResponseFunc that sets the specified metadata
2632 // key-value pair.
27 func SetResponseHeader(key, val string) ResponseFunc {
33 func SetResponseHeader(key, val string) ServerResponseFunc {
2834 return func(_ context.Context, md *metadata.MD) {
2935 key, val := EncodeKeyValue(key, val)
3036 (*md)[key] = append((*md)[key], val)
2323 dec DecodeRequestFunc
2424 enc EncodeResponseFunc
2525 before []RequestFunc
26 after []ResponseFunc
26 after []ServerResponseFunc
2727 logger log.Logger
2828 }
2929
6363
6464 // ServerAfter functions are executed on the HTTP response writer after the
6565 // endpoint is invoked, but before anything is written to the client.
66 func ServerAfter(after ...ResponseFunc) ServerOption {
66 func ServerAfter(after ...ServerResponseFunc) ServerOption {
6767 return func(s *Server) { s.after = append(s.after, after...) }
6868 }
6969
6161 // ClientBefore sets the RequestFuncs that are applied to the outgoing HTTP
6262 // request before it's invoked.
6363 func ClientBefore(before ...RequestFunc) ClientOption {
64 return func(c *Client) { c.before = before }
64 return func(c *Client) { c.before = append(c.before, before...) }
6565 }
6666
6767 // ClientAfter sets the ClientResponseFuncs applied to the incoming HTTP
6868 // request prior to it being decoded. This is useful for obtaining anything off
6969 // of the response and adding onto the context prior to decoding.
7070 func ClientAfter(after ...ClientResponseFunc) ClientOption {
71 return func(c *Client) { c.after = after }
71 return func(c *Client) { c.after = append(c.after, after...) }
7272 }
7373
7474 // BufferedStream sets whether the Response.Body is left open, allowing it