/
director.go
66 lines (52 loc) · 2.33 KB
/
director.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Based on https://github.com/trusch/grpc-proxy
// Copyright Michal Witkowski. Licensed under Apache2 license: https://github.com/trusch/grpc-proxy/blob/master/LICENSE.txt
package internal
import (
"errors"
"sync"
"github.com/zeromicro/go-zero/zrpc"
"go.uber.org/atomic"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/zeromicro/grpc-mock/internal/proxy/internal/codec"
)
// StreamDirector returns a gRPC ClientConn to be used to forward the call to.
//
// The presence of the `Context` allows for rich filtering, e.g. based on Metadata (headers).
// If no handling is meant to be done, a `codes.NotImplemented` gRPC error should be returned.
//
// The context returned from this function should be the context for the *outgoing* (to backend) call. In case you want
// to forward any Metadata between the inbound request and outbound requests, you should do it manually. However, you
// *must* propagate the cancel function (`context.WithCancel`) of the inbound context to the one returned.
//
// It is worth noting that the StreamDirector will be fired *after* all server-side stream interceptors
// are invoked. So decisions around authorization, monitoring etc. are better to be handled there.
//
// See the rather rich example.
var grpcMgr = &grpcManager{}
type grpcManager struct {
mu sync.RWMutex
method2CliAtomic atomic.Value
}
func NewClient(conf *zrpc.RpcClientConf) (zrpc.Client, error) {
return zrpc.NewClient(*conf,
zrpc.WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.CallContentSubtype((&codec.Proxy{}).Name()))))
}
func SetMethod2CliAtomic(mp map[string]zrpc.Client) {
grpcMgr.method2CliAtomic.Store(mp)
}
type StreamDirector func(ctx context.Context, fullMethodName string) (context.Context, grpc.ClientConnInterface, func(), error)
func GetStreamDirector() StreamDirector {
return grpcMgr.StreamDirector
}
func (m *grpcManager) StreamDirector(ctx context.Context, fullMethodName string) (context.Context, grpc.ClientConnInterface, func(), error) {
m.mu.RLock()
cli := grpcMgr.method2CliAtomic.Load().(map[string]zrpc.Client)[fullMethodName]
m.mu.RUnlock()
if cli == nil {
return ctx, nil, func() {}, errors.New("unregistered client")
}
return ctx, cli.Conn(), func() {}, nil
}