-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
104 lines (89 loc) · 2.54 KB
/
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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package rpc
import (
"context"
"errors"
"sync"
"github.com/cnlisea/ant/app/net/rpc/selector"
"github.com/cnlisea/ant/app/proxy"
rpcNacosClient "github.com/rpcxio/rpcx-nacos/client"
"github.com/smallnest/rpcx/client"
)
type Client struct {
client client.XClient
closed bool
mutex sync.RWMutex
}
func NewClient(discovery proxy.Discovery, name string, serviceName string, options ...ClientOptionFunc) (*Client, error) {
if discovery == nil {
return nil, errors.New("discovery invalid")
}
var op ClientOption
for _, f := range options {
f(&op)
}
var (
failMode client.FailMode
selectMode client.SelectMode
selectorInstance client.Selector
)
switch op.SelectMode {
case ClientSelectModeHash:
failMode = client.Failtry
selectMode = client.SelectByUser
selectorInstance = new(selector.Hash)
case ClientSelectModeSoftState:
failMode = client.Failtry
selectMode = client.SelectByUser
selectorInstance = new(selector.SoftState)
default:
failMode = client.Failover
selectMode = client.RoundRobin
}
xClient := client.NewXClient(serviceName,
failMode,
selectMode,
rpcNacosClient.NewNacosDiscoveryWithClient(name, discovery.ClusterName(), op.GroupName, discovery.Instance()),
client.DefaultOption)
if selectorInstance != nil {
xClient.SetSelector(selectorInstance)
}
return &Client{
client: xClient,
}, nil
}
func (c *Client) WithHashKey(ctx context.Context, val string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, selector.CtxHashTag, val)
}
func (c *Client) WithSoftStateKey(ctx context.Context, val string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, selector.CtxSoftStateTag, val)
}
func (c *Client) Invoke(ctx context.Context, method string, args interface{}, reply interface{}) error {
return c.client.Call(ctx, method, args, reply)
}
func (c *Client) OneWay(ctx context.Context, method string, args interface{}) error {
return c.client.Call(ctx, method, args, nil)
}
func (c *Client) Broadcast(ctx context.Context, method string, args interface{}, reply interface{}) error {
return c.client.Broadcast(ctx, method, args, reply)
}
func (c *Client) Callback(ctx context.Context, method string, args interface{}, reply interface{}, f func(reply interface{}, err error)) {
go func() {
f(reply, c.client.Call(ctx, method, args, reply))
}()
}
func (c *Client) Close() error {
var err error
c.mutex.Lock()
if !c.closed {
c.closed = true
err = c.client.Close()
}
c.mutex.Unlock()
return err
}