/
options.go
89 lines (75 loc) · 1.94 KB
/
options.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package client
import (
"time"
"github.com/aukbit/pluto/common"
"github.com/rs/zerolog"
"google.golang.org/grpc"
)
// Option is used to set options for the service.
type Option interface {
apply(*Client)
}
// optionFunc wraps a func so it satisfies the Option interface.
type optionFunc func(*Client)
func (f optionFunc) apply(s *Client) {
f(s)
}
// ID client id
func ID(id string) Option {
return optionFunc(func(c *Client) {
c.cfg.ID = id
})
}
// Name client name
func Name(n string) Option {
return optionFunc(func(c *Client) {
c.cfg.Name = common.SafeName(n, DefaultName)
})
}
// Description client description
func Description(d string) Option {
return optionFunc(func(c *Client) {
c.cfg.Description = d
})
}
// Target server address
func Target(t string) Option {
return optionFunc(func(c *Client) {
c.cfg.Target = t
})
}
// GRPCRegister register client gRPC function
func GRPCRegister(fn func(*grpc.ClientConn) interface{}) Option {
return optionFunc(func(c *Client) {
c.cfg.GRPCRegister = fn
})
}
// UnaryClientInterceptors ...
func UnaryClientInterceptors(uci []grpc.UnaryClientInterceptor) Option {
return optionFunc(func(c *Client) {
c.cfg.mu.Lock()
defer c.cfg.mu.Unlock()
c.cfg.UnaryClientInterceptors = append(c.cfg.UnaryClientInterceptors, uci...)
})
}
// StreamClientInterceptors ...
func StreamClientInterceptors(uci []grpc.StreamClientInterceptor) Option {
return optionFunc(func(c *Client) {
c.cfg.mu.Lock()
defer c.cfg.mu.Unlock()
c.cfg.StreamClientInterceptors = append(c.cfg.StreamClientInterceptors, uci...)
})
}
// Logger sets a shallow copy from an input logger
func Logger(l zerolog.Logger) Option {
return optionFunc(func(c *Client) {
c.logger = l
})
}
// Timeout returns an Option that configures a timeout for dialing a ClientConn
// initially. This is valid if and only if WithBlock() is present.
func Timeout(d time.Duration) Option {
return optionFunc(func(c *Client) {
c.cfg.Timeout = d
})
}