-
Notifications
You must be signed in to change notification settings - Fork 3
/
option.go
73 lines (62 loc) · 1.75 KB
/
option.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
package grpcx
import (
"time"
"github.com/coreos/etcd/clientv3"
etcdnaming "github.com/coreos/etcd/clientv3/naming"
"github.com/fagongzi/log"
"github.com/labstack/echo"
"google.golang.org/grpc/naming"
)
// ServerOption service side option
type ServerOption func(*serverOptions)
type serverOptions struct {
publisher Publisher
httpAddr string
httpSetup func(*echo.Echo)
}
// WithEtcdPublisher use etcd to publish service
func WithEtcdPublisher(client *clientv3.Client, prefix string, ttl int64, timeout time.Duration) ServerOption {
return func(opts *serverOptions) {
p, err := newEtcdPublisher(client, prefix, ttl, timeout)
if err != nil {
log.Fatalf("rpc: use etcd service publish failed, errors:\n%+v",
err)
}
opts.publisher = p
}
}
// WithHTTPServer with http server
func WithHTTPServer(addr string, httpSetup func(*echo.Echo)) ServerOption {
return func(opts *serverOptions) {
opts.httpAddr = addr
opts.httpSetup = httpSetup
}
}
// ClientOption is client create option
type ClientOption func(*clientOptions)
type clientOptions struct {
prefix string
resolver naming.Resolver
timeout time.Duration
}
// WithEtcdServiceDiscovery returns a etcd discovery option
func WithEtcdServiceDiscovery(prefix string, client *clientv3.Client) ClientOption {
return func(opts *clientOptions) {
opts.prefix = prefix
opts.resolver = &etcdnaming.GRPCResolver{
Client: client,
}
}
}
// WithDirectAddresses returns a direct addresses option
func WithDirectAddresses(addrs ...string) ClientOption {
return func(opts *clientOptions) {
opts.resolver = newLocalResolver(addrs...)
}
}
// WithTimeout returns a timeout option
func WithTimeout(timeout time.Duration) ClientOption {
return func(opts *clientOptions) {
opts.timeout = timeout
}
}