-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
208 lines (180 loc) · 4.34 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package accord
import (
"context"
"io"
"os"
"time"
"github.com/bsm/accord/internal/cache"
"github.com/bsm/accord/rpc"
"github.com/google/uuid"
"google.golang.org/grpc"
)
// ClientOptions contains options for the client
type ClientOptions struct {
Owner string // owner, default: random UUID
Namespace string // namespace, default: ""
Metadata map[string]string // default metadata
TTL time.Duration // TTL, default: 10 minutes
Dir string // Temporary directory, defaults to os.TempDir()
OnError func(error) // custom error handler for background tasks
}
func (o *ClientOptions) ttlSeconds() uint32 {
return uint32(o.TTL / time.Second)
}
func (o *ClientOptions) handleError(err error) {
if o.OnError != nil {
o.OnError(err)
}
}
func (o *ClientOptions) norm() *ClientOptions {
var p ClientOptions
if o != nil {
p = *o
}
if p.Owner == "" {
p.Owner = uuid.New().String()
}
if p.TTL < time.Second {
p.TTL = 10 * time.Minute
}
return &p
}
func (o *ClientOptions) mergeMeta(meta map[string]string) map[string]string {
if meta == nil && len(o.Metadata) != 0 {
meta = make(map[string]string, len(o.Metadata))
}
for k, v := range o.Metadata {
if _, ok := meta[k]; !ok {
meta[k] = v
}
}
return meta
}
// --------------------------------------------------------------------
// Client is a convenience client to the accord API.
type Client struct {
rpc rpc.V1Client
opt *ClientOptions
cache cache.Cache
ownCC *grpc.ClientConn
}
// RPCClient inits a new client.
func RPCClient(ctx context.Context, rpc rpc.V1Client, opt *ClientOptions) (*Client, error) {
opt = opt.norm()
cacheDir, err := os.MkdirTemp(opt.Dir, "accord-client-cache")
if err != nil {
return nil, err
}
cache, err := cache.OpenBadger(cacheDir)
if err != nil {
return nil, err
}
client := &Client{
rpc: rpc,
opt: opt,
cache: cache,
}
if err := client.fetchDone(ctx); err != nil {
_ = cache.Close()
return nil, err
}
return client, nil
}
// WrapClient inits a new client by wrapping a gRCP client connection.
func WrapClient(ctx context.Context, cc *grpc.ClientConn, opt *ClientOptions) (*Client, error) {
return RPCClient(ctx, rpc.NewV1Client(cc), opt)
}
// DialClient creates a new client connection.
func DialClient(ctx context.Context, target string, opt *ClientOptions, dialOpt ...grpc.DialOption) (*Client, error) {
cc, err := grpc.DialContext(ctx, target, dialOpt...)
if err != nil {
return nil, err
}
ci, err := WrapClient(ctx, cc, opt)
if err != nil {
_ = cc.Close()
return nil, err
}
ci.ownCC = cc
return ci, nil
}
// Acquire implements ClientConn interface.
func (c *Client) Acquire(ctx context.Context, name string, meta map[string]string) (*Handle, error) {
// check in cache first
if found, err := c.cache.Contains(name); err != nil {
return nil, err
} else if found {
return nil, ErrDone
}
// try to acquire
res, err := c.rpc.Acquire(ctx, &rpc.AcquireRequest{
Owner: c.opt.Owner,
Name: name,
Namespace: c.opt.Namespace,
Ttl: c.opt.ttlSeconds(),
Metadata: c.opt.mergeMeta(meta),
})
if err != nil {
return nil, err
}
switch res.Status {
case rpc.Status_HELD:
return nil, ErrAcquired
case rpc.Status_DONE:
if err := c.cache.Add(name); err != nil {
return nil, err
}
return nil, ErrDone
}
handleID := uuid.Must(uuid.FromBytes(res.Handle.Id))
return newHandle(handleID, c.rpc, res.Handle.Metadata, c.opt), nil
}
// RPC implements Client interface.
func (c *Client) RPC() rpc.V1Client {
return c.rpc
}
// Close implements Client interface.
func (c *Client) Close() error {
var err error
if c.cache != nil {
if e2 := c.cache.Close(); e2 != nil {
err = e2
}
}
if c.ownCC != nil {
if e2 := c.ownCC.Close(); e2 != nil {
err = e2
}
}
return err
}
func (c *Client) fetchDone(ctx context.Context) error {
res, err := c.rpc.List(ctx, &rpc.ListRequest{
Filter: &rpc.ListRequest_Filter{
Prefix: c.opt.Namespace,
Status: rpc.ListRequest_Filter_DONE,
},
})
if err != nil {
return err
}
wb, err := c.cache.AddBatch()
if err != nil {
return err
}
defer wb.Discard()
for {
handle, err := res.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
if handle.Namespace == c.opt.Namespace {
if err := wb.Add(handle.Name); err != nil {
return err
}
}
}
return wb.Flush()
}