Skip to content

Commit f153bf5

Browse files
committed
Improve grammar in the interceptor tutorial
1 parent 4e8f9d4 commit f153bf5

File tree

1 file changed

+74
-73
lines changed

1 file changed

+74
-73
lines changed

examples/features/interceptor/README.md

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Interceptor
22

33
gRPC provides simple APIs to implement and install interceptors on a per
4-
ClientConn/Server basis. Interceptor intercepts the execution of each RPC call.
5-
Users can use interceptors to do logging, authentication/authorization, metrics
6-
collection, and many other functionality that can be shared across RPCs.
4+
ClientConn/Server basis. Interceptors act as a layer between the application and
5+
gRPC and can be used to observe or control the behavior of gRPC. Interceptors
6+
can be used for logging, authentication/authorization, metrics collection, and
7+
other functionality that is shared across RPCs.
78

89
## Try it
910

@@ -17,102 +18,102 @@ go run client/main.go
1718

1819
## Explanation
1920

20-
In gRPC, interceptors can be categorized into two kinds in terms of the type of
21-
RPC calls they intercept. The first one is the **unary interceptor**, which
22-
intercepts unary RPC calls. And the other is the **stream interceptor** which
23-
deals with streaming RPC calls. See
24-
[here](https://grpc.io/docs/guides/concepts.html#rpc-life-cycle) for explanation
25-
about unary RPCs and streaming RPCs. Each of client and server has their own
26-
types of unary and stream interceptors. Thus, there are in total four different
27-
types of interceptors in gRPC.
21+
gRPC has separate interceptors for unary RPCs and streaming RPCs. See the
22+
[gRPC docs](https://grpc.io/docs/guides/concepts.html#rpc-life-cycle) for an
23+
explanation about unary and streaming RPCs. Both the client and the server have
24+
their own types of unary and stream interceptors. Thus, there are four different
25+
types of interceptors in total.
2826

2927
### Client-side
3028

3129
#### Unary Interceptor
3230

33-
[`UnaryClientInterceptor`](https://godoc.org/google.golang.org/grpc#UnaryClientInterceptor)
34-
is the type for client-side unary interceptor. It is essentially a function type
35-
with signature: `func(ctx context.Context, method string, req, reply
36-
interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error`.
37-
An implementation of a unary interceptor can usually be divided into three
38-
parts: pre-processing, invoking RPC method, and post-processing.
31+
The type for client-side unary interceptors is
32+
[`UnaryClientInterceptor`](https://godoc.org/google.golang.org/grpc#UnaryClientInterceptor).
33+
It is essentially a function type with signature: `func(ctx context.Context,
34+
method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker,
35+
opts ...CallOption) error`. Unary interceptor implementations can usually be
36+
divided into three parts: pre-processing, invoking the RPC method, and
37+
post-processing.
3938

4039
For pre-processing, users can get info about the current RPC call by examining
41-
the args passed in, such as RPC context, method string, request to be sent, and
42-
CallOptions configured. With the info, users can even modify the RPC call. For
43-
instance, in the example, we examine the list of CallOptions and see if call
44-
credential has been configured. If not, configure it to use oauth2 with token
45-
"some-secret-token" as fallback. In our example, we intentionally omit
46-
configuring the per RPC credential to resort to fallback.
40+
the args passed in. The args include the RPC context, method string, request to
41+
be sent, and the CallOptions configured. With this info, users can even modify
42+
the RPC call. For instance, in the example, we examine the list of CallOptions
43+
and check if the call credentials have been configured. If not, the interceptor
44+
configures the RPC call to use oauth2 with a token "some-secret-token" as a
45+
fallback. In our example, we intentionally omit configuring the per RPC
46+
credential to resort to the fallback.
4747

48-
After pre-processing is done, use can invoke the RPC call by calling the
49-
`invoker`.
48+
After pre-processing, users can invoke the RPC call by calling the `invoker`.
5049

51-
Once the invoker returns the reply and error, user can do post-processing of the
52-
RPC call. Usually, it's about dealing with the returned reply and error. In the
53-
example, we log the RPC timing and error info.
50+
Once the invoker returns, users can post-process the RPC call. This usually
51+
involves dealing with the returned reply and error. In the example, we log the
52+
RPC timing and error info.
5453

55-
To install a unary interceptor on a ClientConn, configure `Dial` with
56-
`DialOption`
57-
[`WithUnaryInterceptor`](https://godoc.org/google.golang.org/grpc#WithUnaryInterceptor).
54+
To install a unary interceptor on a ClientConn, configure `Dial` with the
55+
[`WithUnaryInterceptor`](https://godoc.org/google.golang.org/grpc#WithUnaryInterceptor)
56+
`DialOption`.
5857

5958
#### Stream Interceptor
6059

61-
[`StreamClientInterceptor`](https://godoc.org/google.golang.org/grpc#StreamClientInterceptor)
62-
is the type for client-side stream interceptor. It is a function type with
63-
signature: `func(ctx context.Context, desc *StreamDesc, cc *ClientConn, method
64-
string, streamer Streamer, opts ...CallOption) (ClientStream, error)`. An
65-
implementation of a stream interceptor usually include pre-processing, and
66-
stream operation interception.
67-
68-
For pre-processing, it's similar to unary interceptor.
69-
70-
However, rather than doing the RPC method invocation and post-processing
71-
afterwards, stream interceptor intercepts the users' operation on the stream.
72-
First, the interceptor calls the passed-in `streamer` to get a `ClientStream`,
73-
and then wraps around the `ClientStream` and overloading its methods with
74-
intercepting logic. Finally, interceptors returns the wrapped `ClientStream` to
75-
user to operate on.
76-
77-
In the example, we define a new struct `wrappedStream`, which is embedded with a
78-
`ClientStream`. Then, we implement (overload) the `SendMsg` and `RecvMsg`
79-
methods on `wrappedStream` to intercept these two operations on the embedded
60+
The type for client-side stream interceptors is
61+
[`StreamClientInterceptor`](https://godoc.org/google.golang.org/grpc#StreamClientInterceptor).
62+
It is a function type with signature: `func(ctx context.Context, desc
63+
*StreamDesc, cc *ClientConn, method string, streamer Streamer, opts
64+
...CallOption) (ClientStream, error)`. An implementation of a stream interceptor
65+
usually includes pre-processing, and stream operation interception.
66+
67+
The pre-processing is similar to unary interceptors.
68+
69+
However, rather than invoking the RPC method followed by post-processing, stream
70+
interceptors intercept the users' operations on the stream. The interceptor
71+
first calls the passed-in `streamer` to get a `ClientStream`, and then wraps the
72+
`ClientStream` while overloading its methods with the interception logic.
73+
Finally, the interceptor returns the wrapped `ClientStream` to user to operate
74+
on.
75+
76+
In the example, we define a new struct `wrappedStream`, which embeds a
77+
`ClientStream`. We then implement (overload) the `SendMsg` and `RecvMsg` methods
78+
on `wrappedStream` to intercept these two operations on the embedded
8079
`ClientStream`. In the example, we log the message type info and time info for
8180
interception purpose.
8281

83-
To install the stream interceptor for a ClientConn, configure `Dial` with
84-
`DialOption`
85-
[`WithStreamInterceptor`](https://godoc.org/google.golang.org/grpc#WithStreamInterceptor).
82+
To install a stream interceptor for a ClientConn, configure `Dial` with the
83+
[`WithStreamInterceptor`](https://godoc.org/google.golang.org/grpc#WithStreamInterceptor)
84+
`DialOption`.
8685

8786
### Server-side
8887

89-
Server side interceptor is similar to client side, though with slightly
90-
different provided info.
88+
Server side interceptors are similar to client side interceptors, with slightly
89+
different information provided as args.
9190

9291
#### Unary Interceptor
9392

94-
[`UnaryServerInterceptor`](https://godoc.org/google.golang.org/grpc#UnaryServerInterceptor)
95-
is the type for server-side unary interceptor. It is a function type with
96-
signature: `func(ctx context.Context, req interface{}, info *UnaryServerInfo,
97-
handler UnaryHandler) (resp interface{}, err error)`.
93+
The type for server-side unary interceptors is
94+
[`UnaryServerInterceptor`](https://godoc.org/google.golang.org/grpc#UnaryServerInterceptor).
95+
It is a function type with signature: `func(ctx context.Context, req
96+
interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err
97+
error)`.
9898

99-
Refer to client-side unary interceptor section for detailed implementation
100-
explanation.
99+
Refer to the client-side unary interceptor section for a detailed implementation
100+
and explanation.
101101

102-
To install the unary interceptor for a Server, configure `NewServer` with
103-
`ServerOption`
104-
[`UnaryInterceptor`](https://godoc.org/google.golang.org/grpc#UnaryInterceptor).
102+
To install a unary interceptor on a Server, configure `NewServer` with the
103+
[`UnaryInterceptor`](https://godoc.org/google.golang.org/grpc#UnaryInterceptor)
104+
`ServerOption`.
105105

106106
#### Stream Interceptor
107107

108-
[`StreamServerInterceptor`](https://godoc.org/google.golang.org/grpc#StreamServerInterceptor)
109-
is the type for server-side stream interceptor. It is a function type with
110-
signature: `func(srv interface{}, ss ServerStream, info *StreamServerInfo,
111-
handler StreamHandler) error`.
108+
The type for server-side stream interceptors is
109+
[`StreamServerInterceptor`](https://godoc.org/google.golang.org/grpc#StreamServerInterceptor).
110+
It is a function type with the signature: `func(srv interface{}, ss
111+
ServerStream, info *StreamServerInfo, handler StreamHandler) error`.
112112

113-
Refer to client-side stream interceptor section for detailed implementation
114-
explanation.
113+
Refer to the client-side stream interceptor section for a detailed
114+
implementation and explanation.
115+
116+
To install a stream interceptor on a Server, configure `NewServer` with the
117+
[`StreamInterceptor`](https://godoc.org/google.golang.org/grpc#StreamInterceptor)
118+
`ServerOption`.
115119

116-
To install the stream interceptor for a Server, configure `NewServer` with
117-
`ServerOption`
118-
[`StreamInterceptor`](https://godoc.org/google.golang.org/grpc#StreamInterceptor).

0 commit comments

Comments
 (0)