-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
56 lines (43 loc) · 877 Bytes
/
client.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
package client
import (
"io"
"time"
"github.com/hb-go/grpc-contrib/registry"
"google.golang.org/grpc"
)
var (
pool *Pool
)
func init() {
pool = NewPool(100, time.Second*30)
}
func SetPoolSize(size int) {
pool.size = size
}
func SetPoolTTL(ttl time.Duration) {
pool.ttl = int64(ttl.Seconds())
}
func Client(s *registry.Service, options ...Option) (*grpc.ClientConn, io.Closer, error) {
opts := newOptions(options...)
if len(opts.Name) > 0 {
s.Name = opts.Name
}
addr := registry.NewTarget(s, opts.RegistryOptions...)
conn, err := pool.Get(addr, opts.DialOptions...)
if err != nil {
return nil, nil, err
}
c := &funcCloser{
CloseFunc: func() error {
pool.Put(addr, conn, err)
return nil
},
}
return conn.GetCC(), c, nil
}
type funcCloser struct {
CloseFunc func() error
}
func (c *funcCloser) Close() error {
return c.CloseFunc()
}