-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
58 lines (47 loc) · 1.52 KB
/
client.go
File metadata and controls
58 lines (47 loc) · 1.52 KB
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
package srpc
import (
"context"
"github.com/opensraph/srpc/errors"
"google.golang.org/grpc"
)
type Client interface {
grpc.ClientConnInterface
}
var _ Client = (*client)(nil)
type client struct {
conn *grpc.ClientConn
opts clientOptions
}
func NewClient(target string, opt ...ClientOption) (*client, error) {
opts := defaultClientOptions
for _, o := range globalClientOptions {
o(&opts)
}
for _, o := range opt {
o(&opts)
}
if opts.interceptor.chainUnaryClientInts != nil && len(opts.interceptor.chainUnaryClientInts) > 0 {
opts.grpcOpts = append(opts.grpcOpts, grpc.WithChainUnaryInterceptor(opts.interceptor.chainUnaryClientInts...))
}
if opts.interceptor.chainStreamClientInts != nil && len(opts.interceptor.chainStreamClientInts) > 0 {
opts.grpcOpts = append(opts.grpcOpts, grpc.WithChainStreamInterceptor(opts.interceptor.chainStreamClientInts...))
}
conn, err := grpc.NewClient(target, opts.grpcOpts...)
if err != nil {
return nil, errors.Newf("failed to connect to %s: %v", target, err)
}
return &client{
conn: conn,
}, nil
}
// Invoke implements Client.
func (c *client) Invoke(ctx context.Context, method string, req, reply any, opts ...grpc.CallOption) error {
return c.conn.Invoke(ctx, method, req, reply, opts...)
}
// NewStream implements Client.
func (c *client) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return c.conn.NewStream(ctx, desc, method, opts...)
}
func (c *client) Close() error {
return c.conn.Close()
}