This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
77 lines (63 loc) · 2.52 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
package grpc
import (
"github.com/cr-mao/lorig/conf"
"github.com/cr-mao/lorig/registry"
"github.com/cr-mao/lorig/transport/grpc/internal/client"
"github.com/cr-mao/lorig/transport/grpc/internal/server"
"google.golang.org/grpc"
)
const (
defaultServerAddr = ":0" // 默认服务器地址
defaultClientPoolSize = 10 // 默认客户端连接池大小
)
const (
defaultServerAddrKey = "transport.grpc.server.addr"
defaultServerKeyFileKey = "transport.grpc.server.keyFile"
defaultServerCertFileKey = "transport.grpc.server.certFile"
defaultClientPoolSizeKey = "transport.grpc.client.poolSize"
defaultClientCertFileKey = "transport.grpc.client.certFile"
defaultClientServerNameKey = "transport.grpc.client.serverName"
)
type Option func(o *options)
type options struct {
server server.Options
client client.Options
}
func defaultOptions() *options {
opts := &options{}
opts.server.Addr = conf.Get(defaultServerAddrKey, defaultServerAddr)
opts.server.KeyFile = conf.Get(defaultServerKeyFileKey)
opts.server.CertFile = conf.Get(defaultServerCertFileKey)
opts.client.PoolSize = conf.GetInt(defaultClientPoolSizeKey, defaultClientPoolSize)
opts.client.CertFile = conf.GetString(defaultClientCertFileKey)
opts.client.ServerName = conf.GetString(defaultClientServerNameKey)
return opts
}
// WithServerListenAddr 设置服务器监听地址
func WithServerListenAddr(addr string) Option {
return func(o *options) { o.server.Addr = addr }
}
// WithServerCredentials 设置服务器证书和秘钥
func WithServerCredentials(certFile, keyFile string) Option {
return func(o *options) { o.server.KeyFile, o.server.CertFile = keyFile, certFile }
}
// WithServerOptions 设置服务器选项
func WithServerOptions(opts ...grpc.ServerOption) Option {
return func(o *options) { o.server.ServerOpts = opts }
}
// WithClientPoolSize 设置客户端连接池大小
func WithClientPoolSize(size int) Option {
return func(o *options) { o.client.PoolSize = size }
}
// WithClientCredentials 设置客户端证书和校验域名
func WithClientCredentials(certFile string, serverName string) Option {
return func(o *options) { o.client.CertFile, o.client.ServerName = certFile, serverName }
}
// WithClientDiscovery 设置客户端服务发现组件
func WithClientDiscovery(discovery registry.Discovery) Option {
return func(o *options) { o.client.Discovery = discovery }
}
// WithClientDialOptions 设置客户端拨号选项
func WithClientDialOptions(opts ...grpc.DialOption) Option {
return func(o *options) { o.client.DialOpts = opts }
}