-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
170 lines (137 loc) · 4.67 KB
/
rpc.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
package lock
import (
"context"
"errors"
"time"
lockApi "github.com/roadrunner-server/api/v4/build/lock/v1beta1"
"go.uber.org/zap"
)
type rpc struct {
log *zap.Logger
pl *Plugin
}
func (r *rpc) Lock(req *lockApi.Request, resp *lockApi.Response) error {
r.log.Debug("lock request received", zap.Int("ttl", int(req.GetTtl())), zap.Int("wait_ttl", int(req.GetWait())), zap.String("resource", req.GetResource()), zap.String("id", req.GetId()))
if req.GetId() == "" {
return errors.New("empty ID is not allowed")
}
var ctx context.Context
var cancel context.CancelFunc
switch req.GetWait() {
case int64(0):
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
default:
ctx, cancel = context.WithTimeout(context.Background(), time.Microsecond*time.Duration(req.GetWait()))
defer cancel()
}
acq := r.pl.locks.lock(ctx, req.GetResource(), req.GetId(), int(req.GetTtl()))
if acq {
resp.Ok = true
return nil
}
resp.Ok = false
return nil
}
func (r *rpc) LockRead(req *lockApi.Request, resp *lockApi.Response) error {
r.log.Debug("read lock request received", zap.Int("ttl", int(req.GetTtl())), zap.Int("wait_ttl", int(req.GetWait())), zap.String("resource", req.GetResource()), zap.String("id", req.GetId()))
if req.GetId() == "" {
return errors.New("empty ID is not allowed")
}
var ctx context.Context
var cancel context.CancelFunc
switch req.GetWait() {
case int64(0):
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
default:
ctx, cancel = context.WithTimeout(context.Background(), time.Microsecond*time.Duration(req.GetWait()))
defer cancel()
}
acq := r.pl.locks.lockRead(ctx, req.GetResource(), req.GetId(), int(req.GetTtl()))
if acq {
resp.Ok = true
return nil
}
resp.Ok = false
return nil
}
func (r *rpc) Release(req *lockApi.Request, resp *lockApi.Response) error {
r.log.Debug("release request received",
zap.Int("ttl", int(req.GetTtl())),
zap.Int("wait_ttl", int(req.GetWait())),
zap.String("resource", req.GetResource()),
zap.String("id", req.GetId()),
)
if req.GetId() == "" {
return errors.New("empty ID is not allowed")
}
var ctx context.Context
var cancel context.CancelFunc
switch req.GetWait() {
case int64(0):
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
default:
ctx, cancel = context.WithTimeout(context.Background(), time.Microsecond*time.Duration(req.GetWait()))
defer cancel()
}
resp.Ok = r.pl.locks.release(ctx, req.GetResource(), req.GetId())
return nil
}
func (r *rpc) ForceRelease(req *lockApi.Request, resp *lockApi.Response) error {
r.log.Debug("force release request received", zap.Int("ttl", int(req.GetTtl())), zap.Int("wait_ttl", int(req.GetWait())), zap.String("resource", req.GetResource()), zap.String("id", req.GetId()))
var ctx context.Context
var cancel context.CancelFunc
switch req.GetWait() {
case int64(0):
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
default:
ctx, cancel = context.WithTimeout(context.Background(), time.Microsecond*time.Duration(req.GetWait()))
defer cancel()
}
resp.Ok = r.pl.locks.forceRelease(ctx, req.GetResource())
return nil
}
func (r *rpc) Exists(req *lockApi.Request, resp *lockApi.Response) error {
r.log.Debug("exists request received",
zap.Int("ttl", int(req.GetTtl())),
zap.Int("wait_ttl", int(req.GetWait())),
zap.String("resource", req.GetResource()),
zap.String("id", req.GetId()),
)
if req.GetId() == "" {
return errors.New("empty ID is not allowed")
}
var ctx context.Context
var cancel context.CancelFunc
switch req.GetWait() {
case int64(0):
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
default:
ctx, cancel = context.WithTimeout(context.Background(), time.Microsecond*time.Duration(req.GetWait()))
defer cancel()
}
resp.Ok = r.pl.locks.exists(ctx, req.GetResource(), req.GetId())
return nil
}
func (r *rpc) UpdateTTL(req *lockApi.Request, resp *lockApi.Response) error {
r.log.Debug("updateTTL request received", zap.Int("ttl", int(req.GetTtl())), zap.Int("wait_ttl", int(req.GetWait())), zap.String("resource", req.GetResource()), zap.String("id", req.GetId()))
if req.GetId() == "" {
return errors.New("empty ID is not allowed")
}
var ctx context.Context
var cancel context.CancelFunc
switch req.GetWait() {
case int64(0):
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
default:
ctx, cancel = context.WithTimeout(context.Background(), time.Microsecond*time.Duration(req.GetWait()))
defer cancel()
}
resp.Ok = r.pl.locks.updateTTL(ctx, req.GetResource(), req.GetId(), int(req.GetTtl()))
return nil
}