-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
remoting.go
360 lines (311 loc) · 9.14 KB
/
remoting.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
/*
* MIT License
*
* Copyright (c) 2022-2024 Arsene Tochemey Gandote
*
* 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"
"errors"
nethttp "net/http"
"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"
)
// Remoting defines the Remoting APIs
// This requires Remoting is enabled on the connected actor system
type Remoting struct {
client *nethttp.Client
}
// NewRemoting creates an instance Remoting with an insecure connection. To use a secure connection
// one need to call the WithTLS method of the remoting instance to set the certificates of the secure connection
// This requires Remoting is enabled on the connected actor system
// Make sure to call Close to free up resources otherwise you may be leaking socket connections
//
// One can also override the remoting option when calling any of the method for custom one.
func NewRemoting() *Remoting {
r := &Remoting{
client: http.NewClient(),
}
return r
}
// RemoteTell sends a message to an actor remotely without expecting any reply
func (r *Remoting) RemoteTell(ctx context.Context, from, to *address.Address, message proto.Message) error {
marshaled, err := anypb.New(message)
if err != nil {
return ErrInvalidMessage(err)
}
remoteClient := r.serviceClient(to.GetHost(), int(to.GetPort()))
request := &internalpb.RemoteTellRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: from.Address,
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 (r *Remoting) RemoteAsk(ctx context.Context, from, to *address.Address, message proto.Message, timeout time.Duration) (response *anypb.Any, err error) {
marshaled, err := anypb.New(message)
if err != nil {
return nil, ErrInvalidMessage(err)
}
remoteClient := r.serviceClient(to.GetHost(), int(to.GetPort()))
request := &internalpb.RemoteAskRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: from.Address,
Receiver: to.Address,
Message: marshaled,
},
Timeout: durationpb.New(timeout),
}
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
}
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 (r *Remoting) RemoteLookup(ctx context.Context, host string, port int, name string) (addr *address.Address, err error) {
remoteClient := r.serviceClient(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 (r *Remoting) RemoteBatchTell(ctx context.Context, from, to *address.Address, messages []proto.Message) error {
var requests []*internalpb.RemoteTellRequest
for _, message := range messages {
packed, err := anypb.New(message)
if err != nil {
return ErrInvalidMessage(err)
}
requests = append(
requests, &internalpb.RemoteTellRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: from.Address,
Receiver: to.Address,
Message: packed,
},
},
)
}
remoteClient := r.serviceClient(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 (r *Remoting) RemoteBatchAsk(ctx context.Context, from, to *address.Address, messages []proto.Message, timeout time.Duration) (responses []*anypb.Any, err error) {
var requests []*internalpb.RemoteAskRequest
for _, message := range messages {
packed, err := anypb.New(message)
if err != nil {
return nil, ErrInvalidMessage(err)
}
requests = append(
requests, &internalpb.RemoteAskRequest{
RemoteMessage: &internalpb.RemoteMessage{
Sender: from.Address,
Receiver: to.Address,
Message: packed,
},
Timeout: durationpb.New(timeout),
},
)
}
remoteClient := r.serviceClient(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
}
// 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 (r *Remoting) RemoteSpawn(ctx context.Context, host string, port int, name, actorType string) error {
remoteClient := r.serviceClient(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 {
var connectErr *connect.Error
errors.As(err, &connectErr)
e := connectErr.Unwrap()
if strings.Contains(e.Error(), ErrTypeNotRegistered.Error()) {
return ErrTypeNotRegistered
}
}
return err
}
return nil
}
// RemoteReSpawn restarts actor on a remote node.
func (r *Remoting) RemoteReSpawn(ctx context.Context, host string, port int, name string) error {
remoteClient := r.serviceClient(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 (r *Remoting) RemoteStop(ctx context.Context, host string, port int, name string) error {
remoteClient := r.serviceClient(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
}
// Close closes the serviceClient connection
func (r *Remoting) Close() {
r.client.CloseIdleConnections()
}
// serviceClient returns a Remoting service client instance
func (r *Remoting) serviceClient(host string, port int) internalpbconnect.RemotingServiceClient {
endpoint := http.URL(host, port)
return internalpbconnect.NewRemotingServiceClient(r.client, endpoint)
}