-
-
Notifications
You must be signed in to change notification settings - Fork 515
/
protos_protoactor.go
359 lines (316 loc) · 9.51 KB
/
protos_protoactor.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package shared
import (
"errors"
"fmt"
"log"
"math"
"time"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/AsynkronIT/protoactor-go/cluster"
"github.com/AsynkronIT/protoactor-go/remote"
"github.com/gogo/protobuf/proto"
)
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
var rootContext = actor.EmptyRootContext
var xHelloFactory func() Hello
// HelloFactory produces a Hello
func HelloFactory(factory func() Hello) {
xHelloFactory = factory
}
// GetHelloGrain instantiates a new HelloGrain with given ID
func GetHelloGrain(id string) *HelloGrain {
return &HelloGrain{ID: id}
}
// Hello interfaces the services available to the Hello
type Hello interface {
Init(id string)
Terminate()
SayHello(*HelloRequest, cluster.GrainContext) (*HelloResponse, error)
Add(*AddRequest, cluster.GrainContext) (*AddResponse, error)
VoidFunc(*AddRequest, cluster.GrainContext) (*Unit, error)
}
// HelloGrain holds the base data for the HelloGrain
type HelloGrain struct {
ID string
}
// SayHello requests the execution on to the cluster using default options
func (g *HelloGrain) SayHello(r *HelloRequest) (*HelloResponse, error) {
return g.SayHelloWithOpts(r, cluster.DefaultGrainCallOptions())
}
// SayHelloWithOpts requests the execution on to the cluster
func (g *HelloGrain) SayHelloWithOpts(r *HelloRequest, opts *cluster.GrainCallOptions) (*HelloResponse, error) {
fun := func() (*HelloResponse, error) {
pid, statusCode := cluster.Get(g.ID, "Hello")
if statusCode != remote.ResponseStatusCodeOK {
return nil, fmt.Errorf("get PID failed with StatusCode: %v", statusCode)
}
bytes, err := proto.Marshal(r)
if err != nil {
return nil, err
}
request := &cluster.GrainRequest{MethodIndex: 0, MessageData: bytes}
response, err := rootContext.RequestFuture(pid, request, opts.Timeout).Result()
if err != nil {
return nil, err
}
switch msg := response.(type) {
case *cluster.GrainResponse:
result := &HelloResponse{}
err = proto.Unmarshal(msg.MessageData, result)
if err != nil {
return nil, err
}
return result, nil
case *cluster.GrainErrorResponse:
return nil, errors.New(msg.Err)
default:
return nil, errors.New("unknown response")
}
}
var res *HelloResponse
var err error
for i := 0; i < opts.RetryCount; i++ {
res, err = fun()
if err == nil || err.Error() != "future: timeout" {
return res, err
} else if opts.RetryAction != nil {
opts.RetryAction(i)
}
}
return nil, err
}
// SayHelloChan allows to use a channel to execute the method using default options
func (g *HelloGrain) SayHelloChan(r *HelloRequest) (<-chan *HelloResponse, <-chan error) {
return g.SayHelloChanWithOpts(r, cluster.DefaultGrainCallOptions())
}
// SayHelloChanWithOpts allows to use a channel to execute the method
func (g *HelloGrain) SayHelloChanWithOpts(r *HelloRequest, opts *cluster.GrainCallOptions) (<-chan *HelloResponse, <-chan error) {
c := make(chan *HelloResponse)
e := make(chan error)
go func() {
res, err := g.SayHelloWithOpts(r, opts)
if err != nil {
e <- err
} else {
c <- res
}
close(c)
close(e)
}()
return c, e
}
// Add requests the execution on to the cluster using default options
func (g *HelloGrain) Add(r *AddRequest) (*AddResponse, error) {
return g.AddWithOpts(r, cluster.DefaultGrainCallOptions())
}
// AddWithOpts requests the execution on to the cluster
func (g *HelloGrain) AddWithOpts(r *AddRequest, opts *cluster.GrainCallOptions) (*AddResponse, error) {
fun := func() (*AddResponse, error) {
pid, statusCode := cluster.Get(g.ID, "Hello")
if statusCode != remote.ResponseStatusCodeOK {
return nil, fmt.Errorf("get PID failed with StatusCode: %v", statusCode)
}
bytes, err := proto.Marshal(r)
if err != nil {
return nil, err
}
request := &cluster.GrainRequest{MethodIndex: 1, MessageData: bytes}
response, err := rootContext.RequestFuture(pid, request, opts.Timeout).Result()
if err != nil {
return nil, err
}
switch msg := response.(type) {
case *cluster.GrainResponse:
result := &AddResponse{}
err = proto.Unmarshal(msg.MessageData, result)
if err != nil {
return nil, err
}
return result, nil
case *cluster.GrainErrorResponse:
return nil, errors.New(msg.Err)
default:
return nil, errors.New("unknown response")
}
}
var res *AddResponse
var err error
for i := 0; i < opts.RetryCount; i++ {
res, err = fun()
if err == nil || err.Error() != "future: timeout" {
return res, err
} else if opts.RetryAction != nil {
opts.RetryAction(i)
}
}
return nil, err
}
// AddChan allows to use a channel to execute the method using default options
func (g *HelloGrain) AddChan(r *AddRequest) (<-chan *AddResponse, <-chan error) {
return g.AddChanWithOpts(r, cluster.DefaultGrainCallOptions())
}
// AddChanWithOpts allows to use a channel to execute the method
func (g *HelloGrain) AddChanWithOpts(r *AddRequest, opts *cluster.GrainCallOptions) (<-chan *AddResponse, <-chan error) {
c := make(chan *AddResponse)
e := make(chan error)
go func() {
res, err := g.AddWithOpts(r, opts)
if err != nil {
e <- err
} else {
c <- res
}
close(c)
close(e)
}()
return c, e
}
// VoidFunc requests the execution on to the cluster using default options
func (g *HelloGrain) VoidFunc(r *AddRequest) (*Unit, error) {
return g.VoidFuncWithOpts(r, cluster.DefaultGrainCallOptions())
}
// VoidFuncWithOpts requests the execution on to the cluster
func (g *HelloGrain) VoidFuncWithOpts(r *AddRequest, opts *cluster.GrainCallOptions) (*Unit, error) {
fun := func() (*Unit, error) {
pid, statusCode := cluster.Get(g.ID, "Hello")
if statusCode != remote.ResponseStatusCodeOK {
return nil, fmt.Errorf("get PID failed with StatusCode: %v", statusCode)
}
bytes, err := proto.Marshal(r)
if err != nil {
return nil, err
}
request := &cluster.GrainRequest{MethodIndex: 2, MessageData: bytes}
response, err := rootContext.RequestFuture(pid, request, opts.Timeout).Result()
if err != nil {
return nil, err
}
switch msg := response.(type) {
case *cluster.GrainResponse:
result := &Unit{}
err = proto.Unmarshal(msg.MessageData, result)
if err != nil {
return nil, err
}
return result, nil
case *cluster.GrainErrorResponse:
return nil, errors.New(msg.Err)
default:
return nil, errors.New("unknown response")
}
}
var res *Unit
var err error
for i := 0; i < opts.RetryCount; i++ {
res, err = fun()
if err == nil || err.Error() != "future: timeout" {
return res, err
} else if opts.RetryAction != nil {
opts.RetryAction(i)
}
}
return nil, err
}
// VoidFuncChan allows to use a channel to execute the method using default options
func (g *HelloGrain) VoidFuncChan(r *AddRequest) (<-chan *Unit, <-chan error) {
return g.VoidFuncChanWithOpts(r, cluster.DefaultGrainCallOptions())
}
// VoidFuncChanWithOpts allows to use a channel to execute the method
func (g *HelloGrain) VoidFuncChanWithOpts(r *AddRequest, opts *cluster.GrainCallOptions) (<-chan *Unit, <-chan error) {
c := make(chan *Unit)
e := make(chan error)
go func() {
res, err := g.VoidFuncWithOpts(r, opts)
if err != nil {
e <- err
} else {
c <- res
}
close(c)
close(e)
}()
return c, e
}
// HelloActor represents the actor structure
type HelloActor struct {
inner Hello
Timeout *time.Duration
}
// Receive ensures the lifecycle of the actor for the received message
func (a *HelloActor) Receive(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *actor.Started:
a.inner = xHelloFactory()
id := ctx.Self().Id
a.inner.Init(id[7:]) // skip "remote$"
if a.Timeout != nil {
ctx.SetReceiveTimeout(*a.Timeout)
}
case *actor.ReceiveTimeout:
a.inner.Terminate()
rootContext.PoisonFuture(ctx.Self()).Wait()
case actor.AutoReceiveMessage: // pass
case actor.SystemMessage: // pass
case *cluster.GrainRequest:
switch msg.MethodIndex {
case 0:
req := &HelloRequest{}
err := proto.Unmarshal(msg.MessageData, req)
if err != nil {
log.Fatalf("[GRAIN] proto.Unmarshal failed %v", err)
}
r0, err := a.inner.SayHello(req, ctx)
if err == nil {
bytes, errMarshal := proto.Marshal(r0)
if errMarshal != nil {
log.Fatalf("[GRAIN] proto.Marshal failed %v", errMarshal)
}
resp := &cluster.GrainResponse{MessageData: bytes}
ctx.Respond(resp)
} else {
resp := &cluster.GrainErrorResponse{Err: err.Error()}
ctx.Respond(resp)
}
case 1:
req := &AddRequest{}
err := proto.Unmarshal(msg.MessageData, req)
if err != nil {
log.Fatalf("[GRAIN] proto.Unmarshal failed %v", err)
}
r0, err := a.inner.Add(req, ctx)
if err == nil {
bytes, errMarshal := proto.Marshal(r0)
if errMarshal != nil {
log.Fatalf("[GRAIN] proto.Marshal failed %v", errMarshal)
}
resp := &cluster.GrainResponse{MessageData: bytes}
ctx.Respond(resp)
} else {
resp := &cluster.GrainErrorResponse{Err: err.Error()}
ctx.Respond(resp)
}
case 2:
req := &AddRequest{}
err := proto.Unmarshal(msg.MessageData, req)
if err != nil {
log.Fatalf("[GRAIN] proto.Unmarshal failed %v", err)
}
r0, err := a.inner.VoidFunc(req, ctx)
if err == nil {
bytes, errMarshal := proto.Marshal(r0)
if errMarshal != nil {
log.Fatalf("[GRAIN] proto.Marshal failed %v", errMarshal)
}
resp := &cluster.GrainResponse{MessageData: bytes}
ctx.Respond(resp)
} else {
resp := &cluster.GrainErrorResponse{Err: err.Error()}
ctx.Respond(resp)
}
}
default:
log.Printf("Unknown message %v", msg)
}
}