forked from Tochemey/goakt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
434 lines (371 loc) · 11.2 KB
/
api.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package actors
import (
"context"
"strings"
"time"
"connectrpc.com/connect"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"github.com/tochemey/goakt/v2/address"
"github.com/tochemey/goakt/v2/internal/http"
"github.com/tochemey/goakt/v2/internal/internalpb"
"github.com/tochemey/goakt/v2/internal/internalpb/internalpbconnect"
)
// Ask sends a synchronous message to another actor and expect a response.
// This block until a response is received or timed out.
func Ask(ctx context.Context, to *PID, message proto.Message, timeout time.Duration) (response proto.Message, err error) {
if !to.IsRunning() {
return nil, ErrDead
}
receiveContext, err := toReceiveContext(ctx, to, message, false)
if err != nil {
return nil, err
}
to.doReceive(receiveContext)
// await patiently to receive the response from the actor
select {
case response = <-receiveContext.response:
return
case <-time.After(timeout):
err = ErrRequestTimeout
to.toDeadletterQueue(receiveContext, err)
return
}
}
// Tell sends an asynchronous message to an actor
func Tell(ctx context.Context, to *PID, message proto.Message) error {
if !to.IsRunning() {
return ErrDead
}
receiveContext, err := toReceiveContext(ctx, to, message, true)
if err != nil {
return err
}
to.doReceive(receiveContext)
return nil
}
// BatchTell sends bulk asynchronous messages to an actor
// The messages will be processed one after the other in the order they are sent
// This is a design choice to follow the simple principle of one message at a time processing by actors.
func BatchTell(ctx context.Context, to *PID, messages ...proto.Message) error {
// messages are processed one after the other
for _, mesage := range messages {
if err := Tell(ctx, to, mesage); err != nil {
return err
}
}
return nil
}
// BatchAsk sends a synchronous bunch of messages to the given PID and expect responses in the same order as the messages.
// The messages will be processed one after the other in the order they are sent
// This is a design choice to follow the simple principle of one message at a time processing by actors.
func BatchAsk(ctx context.Context, to *PID, timeout time.Duration, messages ...proto.Message) (responses chan proto.Message, err error) {
responses = make(chan proto.Message, len(messages))
defer close(responses)
for _, mesage := range messages {
response, err := Ask(ctx, to, mesage, timeout)
if err != nil {
return nil, err
}
responses <- response
}
return
}
// RemoteTell sends a message to an actor remotely without expecting any reply
func RemoteTell(ctx context.Context, to *address.Address, message proto.Message) error {
marshaled, err := anypb.New(message)
if err != nil {
return ErrInvalidRemoteMessage(err)
}
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(to.GetHost(), int(to.GetPort())),
)
request := &internalpb.RemoteTellRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: address.NoSender,
Receiver: to.Address,
Message: marshaled,
},
}
stream := remoteClient.RemoteTell(ctx)
if err := stream.Send(request); err != nil {
if eof(err) {
if _, err := stream.CloseAndReceive(); err != nil {
return err
}
return nil
}
return err
}
// close the connection
if _, err := stream.CloseAndReceive(); err != nil {
return err
}
return nil
}
// RemoteAsk sends a synchronous message to another actor remotely and expect a response.
func RemoteAsk(ctx context.Context, to *address.Address, message proto.Message, timeout time.Duration) (response *anypb.Any, err error) {
marshaled, err := anypb.New(message)
if err != nil {
return nil, ErrInvalidRemoteMessage(err)
}
remotingService := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(to.GetHost(), int(to.GetPort())),
)
request := &internalpb.RemoteAskRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: address.NoSender,
Receiver: to.Address,
Message: marshaled,
},
Timeout: durationpb.New(timeout),
}
stream := remotingService.RemoteAsk(ctx)
errc := make(chan error, 1)
go func() {
defer close(errc)
for {
resp, err := stream.Receive()
if err != nil {
errc <- err
return
}
response = resp.GetMessage()
}
}()
err = stream.Send(request)
if err != nil {
return nil, err
}
if err := stream.CloseRequest(); err != nil {
return nil, err
}
err = <-errc
if eof(err) {
return response, nil
}
if err != nil {
return nil, err
}
return
}
// RemoteLookup look for an actor address on a remote node.
func RemoteLookup(ctx context.Context, host string, port int, name string) (addr *address.Address, err error) {
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(host, port),
)
request := connect.NewRequest(&internalpb.RemoteLookupRequest{
Host: host,
Port: int32(port),
Name: name,
})
response, err := remoteClient.RemoteLookup(ctx, request)
if err != nil {
code := connect.CodeOf(err)
if code == connect.CodeNotFound {
return nil, nil
}
return nil, err
}
return address.From(response.Msg.GetAddress()), nil
}
// RemoteBatchTell sends bulk asynchronous messages to an actor
func RemoteBatchTell(ctx context.Context, to *address.Address, messages ...proto.Message) error {
var requests []*internalpb.RemoteTellRequest
for _, message := range messages {
packed, err := anypb.New(message)
if err != nil {
return ErrInvalidRemoteMessage(err)
}
requests = append(requests, &internalpb.RemoteTellRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: address.NoSender,
Receiver: to.Address,
Message: packed,
},
})
}
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(to.GetHost(), int(to.GetPort())),
)
stream := remoteClient.RemoteTell(ctx)
for _, request := range requests {
err := stream.Send(request)
if eof(err) {
if _, err := stream.CloseAndReceive(); err != nil {
return err
}
return nil
}
if err != nil {
return err
}
}
// close the connection
if _, err := stream.CloseAndReceive(); err != nil {
return err
}
return nil
}
// RemoteBatchAsk sends bulk messages to an actor with responses expected
func RemoteBatchAsk(ctx context.Context, to *address.Address, messages ...proto.Message) (responses []*anypb.Any, err error) {
var requests []*internalpb.RemoteAskRequest
for _, message := range messages {
packed, err := anypb.New(message)
if err != nil {
return nil, ErrInvalidRemoteMessage(err)
}
requests = append(requests, &internalpb.RemoteAskRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: address.NoSender,
Receiver: to.Address,
Message: packed,
},
})
}
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(to.GetHost(), int(to.GetPort())),
)
stream := remoteClient.RemoteAsk(ctx)
errc := make(chan error, 1)
go func() {
defer close(errc)
for {
resp, err := stream.Receive()
if err != nil {
errc <- err
return
}
responses = append(responses, resp.GetMessage())
}
}()
for _, request := range requests {
err := stream.Send(request)
if err != nil {
return nil, err
}
}
if err := stream.CloseRequest(); err != nil {
return nil, err
}
err = <-errc
if eof(err) {
return responses, nil
}
if err != nil {
return nil, err
}
return
}
// RemoteReSpawn restarts actor on a remote node.
func RemoteReSpawn(ctx context.Context, host string, port int, name string) error {
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(host, port),
)
request := connect.NewRequest(&internalpb.RemoteReSpawnRequest{
Host: host,
Port: int32(port),
Name: name,
})
if _, err := remoteClient.RemoteReSpawn(ctx, request); err != nil {
code := connect.CodeOf(err)
if code == connect.CodeNotFound {
return nil
}
return err
}
return nil
}
// RemoteStop stops an actor on a remote node.
func RemoteStop(ctx context.Context, host string, port int, name string) error {
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(host, port),
)
request := connect.NewRequest(&internalpb.RemoteStopRequest{
Host: host,
Port: int32(port),
Name: name,
})
if _, err := remoteClient.RemoteStop(ctx, request); err != nil {
code := connect.CodeOf(err)
if code == connect.CodeNotFound {
return nil
}
return err
}
return nil
}
// RemoteSpawn creates an actor on a remote node. The given actor needs to be registered on the remote node using the Register method of ActorSystem
func RemoteSpawn(ctx context.Context, host string, port int, name, actorType string) error {
remoteClient := internalpbconnect.NewRemotingServiceClient(
http.NewClient(),
http.URL(host, port),
)
request := connect.NewRequest(&internalpb.RemoteSpawnRequest{
Host: host,
Port: int32(port),
ActorName: name,
ActorType: actorType,
})
if _, err := remoteClient.RemoteSpawn(ctx, request); err != nil {
code := connect.CodeOf(err)
if code == connect.CodeFailedPrecondition {
connectErr := err.(*connect.Error)
e := connectErr.Unwrap()
// TODO: find a better way to use errors.Is with connect.Error
if strings.Contains(e.Error(), ErrTypeNotRegistered.Error()) {
return ErrTypeNotRegistered
}
}
return err
}
return nil
}
// toReceiveContext creates a ReceiveContext provided a message and a receiver
func toReceiveContext(ctx context.Context, to *PID, message proto.Message, async bool) (*ReceiveContext, error) {
switch msg := message.(type) {
case *internalpb.RemoteMessage:
actual, err := msg.GetMessage().UnmarshalNew()
if err != nil {
return nil, ErrInvalidRemoteMessage(err)
}
receiveContext := contextFromPool()
receiveContext.build(ctx, NoSender, to, actual, async)
return receiveContext.withRemoteSender(address.From(msg.GetSender())), nil
default:
receiveContext := contextFromPool()
receiveContext.build(ctx, NoSender, to, message, async)
return receiveContext.withRemoteSender(address.From(address.NoSender)), nil
}
}