|
| 1 | +# proxy |
| 2 | +-- |
| 3 | + import "github.com/mwitkow/grpc-proxy/proxy" |
| 4 | + |
| 5 | +Package proxy provides a reverse proxy handler for gRPC. |
| 6 | + |
| 7 | +The implementation allows a `grpc.Server` to pass a received ServerStream to a |
| 8 | +ClientStream without understanding the semantics of the messages exchanged. It |
| 9 | +basically provides a transparent reverse-proxy. |
| 10 | + |
| 11 | +This package is intentionally generic, exposing a `StreamDirector` function that |
| 12 | +allows users of this package to implement whatever logic of backend-picking, |
| 13 | +dialing and service verification to perform. |
| 14 | + |
| 15 | +See examples on documented functions. |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +#### func Codec |
| 20 | + |
| 21 | +```go |
| 22 | +func Codec() grpc.Codec |
| 23 | +``` |
| 24 | +Codec returns a proxying grpc.Codec with the default protobuf codec as parent. |
| 25 | + |
| 26 | +See CodecWithParent. |
| 27 | + |
| 28 | +#### func CodecWithParent |
| 29 | + |
| 30 | +```go |
| 31 | +func CodecWithParent(fallback grpc.Codec) grpc.Codec |
| 32 | +``` |
| 33 | +CodecWithParent returns a proxying grpc.Codec with a user provided codec as |
| 34 | +parent. |
| 35 | + |
| 36 | +This codec is *crucial* to the functioning of the proxy. It allows the proxy |
| 37 | +server to be oblivious to the schema of the forwarded messages. It basically |
| 38 | +treats a gRPC message frame as raw bytes. However, if the server handler, or the |
| 39 | +client caller are not proxy-internal functions it will fall back to trying to |
| 40 | +decode the message using a fallback codec. |
| 41 | + |
| 42 | +#### func RegisterService |
| 43 | + |
| 44 | +```go |
| 45 | +func RegisterService(server *grpc.Server, director StreamDirector, serviceName string, methodNames ...string) |
| 46 | +``` |
| 47 | +RegisterService sets up a proxy handler for a particular gRPC service and |
| 48 | +method. The behaviour is the same as if you were registering a handler method, |
| 49 | +e.g. from a codegenerated pb.go file. |
| 50 | + |
| 51 | +This can *only* be used if the `server` also uses grpcproxy.CodecForServer() |
| 52 | +ServerOption. |
| 53 | + |
| 54 | +#### func TransparentHandler |
| 55 | + |
| 56 | +```go |
| 57 | +func TransparentHandler(director StreamDirector) grpc.StreamHandler |
| 58 | +``` |
| 59 | +TransparentHandler returns a handler that attempts to proxy all requests that |
| 60 | +are not registered in the server. The indented use here is as a transparent |
| 61 | +proxy, where the server doesn't know about the services implemented by the |
| 62 | +backends. It should be used as a `grpc.UnknownServiceHandler`. |
| 63 | + |
| 64 | +This can *only* be used if the `server` also uses grpcproxy.CodecForServer() |
| 65 | +ServerOption. |
| 66 | + |
| 67 | +#### type StreamDirector |
| 68 | + |
| 69 | +```go |
| 70 | +type StreamDirector func(ctx context.Context, fullMethodName string) (*grpc.ClientConn, error) |
| 71 | +``` |
| 72 | + |
| 73 | +StreamDirector returns a gRPC ClientConn to be used to forward the call to. |
| 74 | + |
| 75 | +The presence of the `Context` allows for rich filtering, e.g. based on Metadata |
| 76 | +(headers). If no handling is meant to be done, a `codes.NotImplemented` gRPC |
| 77 | +error should be returned. |
| 78 | + |
| 79 | +It is worth noting that the StreamDirector will be fired *after* all server-side |
| 80 | +stream interceptors are invoked. So decisions around authorization, monitoring |
| 81 | +etc. are better to be handled there. |
| 82 | + |
| 83 | +See the rather rich example. |
0 commit comments