-
Notifications
You must be signed in to change notification settings - Fork 2
/
addr_impl.go
644 lines (559 loc) · 15.8 KB
/
addr_impl.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package actorkit
import (
"time"
"github.com/gokit/errors"
)
const (
defaultSize = 16
kitProtocol = "kit"
accessName = "access"
kitNamespace = "localhost"
deadLetterName = "deadletter"
)
//*********************************************
// AddrSet
//*********************************************
// ServiceSet implements a grouping of giving addresses using
// sets based on their service offered which is represented by the
// Addr.Addr(). It allows indexing, checking availability of giving
// address within set.
//
// This is not safe for concurrent access.
type ServiceSet struct {
set map[string]int
addrs []Addr
}
// NewServiceSet returns a new instance of a ServiceSet.
func NewServiceSet() *ServiceSet {
return &ServiceSet{}
}
// ForEach iterates through all available address against provided
// function. It expects the function to return true if it wishes to
// continue iteration or to stop by returning false.
func (ad *ServiceSet) ForEach(fx func(Addr, int) bool) {
for ind, elem := range ad.addrs {
if !fx(elem, ind) {
break
}
}
}
// Has returns true/false if giving underline address (string version) already exists in
// set.
func (ad *ServiceSet) Has(addr string) bool {
if ad.set != nil {
if _, ok := ad.set[addr]; ok {
return true
}
}
return false
}
// HasAddr returns true/false if giving underline address already exists in
// set.
func (ad *ServiceSet) HasAddr(addr Addr) bool {
if ad.set != nil {
if _, ok := ad.set[addr.Addr()]; ok {
return true
}
}
return false
}
// Index returns giving index of address (string version) if within
// set else returns -1.
func (ad *ServiceSet) Index(addr string) int {
if ad.set != nil {
if index, ok := ad.set[addr]; ok {
return index
}
}
return -1
}
// IndexOf returns the giving index of address if in set else returns -1.
func (ad *ServiceSet) IndexOf(addr Addr) int {
if ad.set != nil {
if index, ok := ad.set[addr.Addr()]; ok {
return index
}
}
return -1
}
// Get returns giving Addr for a giving service string if in set.
func (ad *ServiceSet) Get(service string) (Addr, bool) {
if ad.set != nil {
if index, ok := ad.set[service]; ok {
return ad.addrs[index], true
}
}
return nil, false
}
// Remove removes giving address (string version from underline set).
func (ad *ServiceSet) Remove(service string) bool {
if ad.set != nil {
if index, ok := ad.set[service]; ok {
addr := ad.addrs[index]
if swap := len(ad.addrs) - 1; swap > 0 {
ad.addrs[index] = ad.addrs[swap]
ad.addrs = ad.addrs[:swap]
delete(ad.set, addr.Addr())
return true
}
ad.addrs = ad.addrs[:0]
delete(ad.set, addr.Addr())
return true
}
}
return false
}
// RemoveAddr removes giving address from set.
func (ad *ServiceSet) RemoveAddr(addr Addr) bool {
if ad.set != nil {
if index, ok := ad.set[addr.Addr()]; ok {
if swap := len(ad.addrs) - 1; swap > 0 {
ad.addrs[index] = ad.addrs[swap]
ad.addrs = ad.addrs[:swap]
delete(ad.set, addr.Addr())
return true
}
ad.addrs = ad.addrs[:0]
delete(ad.set, addr.Addr())
return true
}
}
return false
}
// Add adds giving address into address set.
func (ad *ServiceSet) Add(addr Addr) bool {
if ad.set == nil {
ad.set = map[string]int{}
ad.addrs = make([]Addr, 0, defaultSize)
}
if _, ok := ad.set[addr.Addr()]; !ok {
ind := len(ad.addrs)
ad.addrs = append(ad.addrs, addr)
ad.set[addr.Addr()] = ind
return true
}
return false
}
// Set exposes the provided underline list of Addr, this slice is only
// valid for use until the next call to Add or Remove. Hence you
// must be adequately careful here.
func (ad *ServiceSet) Set() []Addr {
return ad.addrs[:len(ad.addrs)]
}
//*********************************************
// IDSet
//*********************************************
// IDSet implements a grouping of giving actor addresses using
// sets based on the Addr.ID(). It allows indexing, checking availability of giving
// address within set.
//
// This is not safe for concurrent access.
type IDSet struct {
set map[string]int
addrs []Addr
}
// NewIDSet returns a new instance of a IDSet.
func NewIDSet() *IDSet {
return &IDSet{}
}
// ForEach iterates through all available address against provided
// function. It expects the function to return true if it wishes to
// continue iteration or to stop by returning false.
func (ad *IDSet) ForEach(fx func(Addr, int) bool) {
for ind, elem := range ad.addrs {
if !fx(elem, ind) {
break
}
}
}
// Has returns true/false if giving underline address (string version) already exists in
// set.
func (ad *IDSet) Has(id string) bool {
if ad.set != nil {
if _, ok := ad.set[id]; ok {
return true
}
}
return false
}
// HasAddr returns true/false if giving underline address already exists in
// set.
func (ad *IDSet) HasAddr(addr Addr) bool {
if ad.set != nil {
if _, ok := ad.set[addr.ID()]; ok {
return true
}
}
return false
}
// Index returns giving index of address (string version) if within
// set else returns -1.
func (ad *IDSet) Index(id string) int {
if ad.set != nil {
if index, ok := ad.set[id]; ok {
return index
}
}
return -1
}
// IndexOf returns the giving index of address if in set else returns -1.
func (ad *IDSet) IndexOf(addr Addr) int {
if ad.set != nil {
if index, ok := ad.set[addr.ID()]; ok {
return index
}
}
return -1
}
// Get returns giving Addr for a giving service string if in set.
func (ad *IDSet) Get(service string) (Addr, bool) {
if ad.set != nil {
if index, ok := ad.set[service]; ok {
return ad.addrs[index], true
}
}
return nil, false
}
// Remove removes giving address (string version from underline set).
func (ad *IDSet) Remove(id string) bool {
if ad.set != nil {
if index, ok := ad.set[id]; ok {
addr := ad.addrs[index]
if swap := len(ad.addrs) - 1; swap > 0 {
ad.addrs[index] = ad.addrs[swap]
ad.addrs = ad.addrs[:swap]
delete(ad.set, addr.ID())
return true
}
ad.addrs = ad.addrs[:0]
delete(ad.set, addr.ID())
return true
}
}
return false
}
// RemoveAddr removes giving address from set.
func (ad *IDSet) RemoveAddr(addr Addr) bool {
if ad.set != nil {
if index, ok := ad.set[addr.ID()]; ok {
if swap := len(ad.addrs) - 1; swap > 0 {
ad.addrs[index] = ad.addrs[swap]
ad.addrs = ad.addrs[:swap]
delete(ad.set, addr.ID())
return true
}
ad.addrs = ad.addrs[:0]
delete(ad.set, addr.ID())
return true
}
}
return false
}
// Set exposes the provided underline list of Addr, this slice is only
// valid for use until the next call to Add or Remove. Hence you
// must be adequately careful here.
func (ad *IDSet) Set() []Addr {
return ad.addrs[:len(ad.addrs)]
}
// Add adds giving address into address set.
func (ad *IDSet) Add(addr Addr) bool {
if ad.set == nil {
ad.set = map[string]int{}
ad.addrs = make([]Addr, 0, defaultSize)
}
if _, ok := ad.set[addr.ID()]; !ok {
ind := len(ad.addrs)
ad.addrs = append(ad.addrs, addr)
ad.set[addr.ID()] = ind
return true
}
return false
}
//*********************************************
// AddrImpl
//*********************************************
var (
// ErrHasNoActor is returned when actor implementer has no actor underline
// which is mostly occuring with futures.
ErrHasNoActor = errors.New("Addr implementer has no underline actor")
)
var _ Addr = &AddrImpl{}
// Destroy returns a error which provides a means of forceful shutdown
// and removal of giving actor of address from the system basically making
// the actor and it's children non existent.
func Destroy(addr Addr) error {
if actor := addr.Actor(); actor != nil {
return actor.Destroy()
}
return errors.WrapOnly(ErrHasNoActor)
}
// Kill returns a error which provides a means of shutdown and clearing
// all pending messages of giving actor through it's address. It also kills
// actors children.
func Kill(addr Addr) error {
if actor := addr.Actor(); actor != nil {
return actor.Kill()
}
return errors.WrapOnly(ErrHasNoActor)
}
// Restart restarts giving actor through it's address, the messages are maintained and kept
// safe, the children of actor are also restarted.
func Restart(addr Addr) error {
if actor := addr.Actor(); actor != nil {
return actor.Restart()
}
return errors.WrapOnly(ErrHasNoActor)
}
// Poison stops the actor referenced by giving address, this also causes a restart of actor's children.
func Poison(addr Addr) error {
if actor := addr.Actor(); actor != nil {
return actor.Stop()
}
return errors.WrapOnly(ErrHasNoActor)
}
// AddrImpl implements the Addr interface providing an addressable reference
// to an existing actor.
type AddrImpl struct {
Root Addressable
actor Actor
service string
deadletter bool
}
// AccessOf returns a default "actor:access" service name, it's
// expected to be used when desiring a default address for an
// actor.
func AccessOf(actor Actor) *AddrImpl {
return AddressOf(actor, accessName)
}
// DeadLetters returns a new instance of AddrImpl which directly delivers
// responses and messages to the deadletter event pipeline.
func DeadLetters() *AddrImpl {
var addr AddrImpl
addr.deadletter = true
addr.service = deadLetterName
return &addr
}
// AddressOf returns a new instance of AddrImpl which directly uses the provided
// process as it's underline target for messages.
func AddressOf(actor Actor, service string) *AddrImpl {
var addr AddrImpl
addr.actor = actor
addr.service = service
return &addr
}
// Parent returns the address of parent if giving underline actor
// is not the same as the actor of this address else returning this
// actor.
func (a *AddrImpl) Parent() Addr {
if a.deadletter {
return a
}
if parent := a.actor.Parent(); parent != nil && parent != a.actor {
return AddressOf(parent, accessName)
}
return a
}
// Actor returns associated actor of Address.
func (a *AddrImpl) Actor() Actor {
return a.actor
}
// Future returns a new future instance from giving source.
func (a *AddrImpl) Future() Future {
return NewFuture(a)
}
// TimedFuture returns a new future instance from giving source.
func (a *AddrImpl) TimedFuture(d time.Duration) Future {
return TimedFuture(a, d)
}
// Ancestor returns the address of the root ancestor. If giving underline
// ancestor is the same as this address actor then we return address.
func (a *AddrImpl) Ancestor() Addr {
if a.deadletter {
return a
}
if parent := a.actor.Ancestor(); parent != nil && parent != a.actor {
return AddressOf(parent, accessName)
}
return a
}
// GetAddr calls the underline actor's GetAddr implementation for
// accessing children of actors through the provided addr string which
// must have it's initial ID match this address ID.
func (a *AddrImpl) GetAddr(addr string) (Addr, error) {
if a.deadletter {
return nil, errors.New("has no children")
}
return a.actor.GetAddr(addr)
}
// GetChild calls the underline actor's GetChild implementation for
// accessing children of actors through it's id and sub ids for the
// descendant of the retrieved actor matching the id value.
func (a *AddrImpl) GetChild(id string, subID ...string) (Addr, error) {
if a.deadletter {
return nil, errors.New("has no children")
}
return a.actor.GetChild(id, subID...)
}
// Children returns address of all children actors of this address actor.
func (a *AddrImpl) Children() []Addr {
if a.deadletter {
return nil
}
return a.actor.Children()
}
// Spawn creates a new actor based on giving service name by requesting all
// discovery services registered to giving underline address actor.
func (a *AddrImpl) Spawn(service string, prop Prop) (Addr, error) {
if a.deadletter {
return nil, errors.New("not possible from a deadletter address")
}
return a.actor.Spawn(service, prop)
}
// AddressOf returns the address of giving actor matching giving service name.
func (a *AddrImpl) AddressOf(service string, ancestral bool) (Addr, error) {
if a.deadletter {
return nil, errors.New("not possible from a deadletter address")
}
return a.actor.Discover(service, ancestral)
}
// Forward delivers provided envelope to the current process.
func (a *AddrImpl) Forward(e Envelope) error {
if a.deadletter {
deadLetters.Publish(DeadMail{
To: a,
Message: e,
})
return nil
}
return a.actor.Receive(a, e)
}
// Send delivers provided raw data to this process providing destination/reply address.
func (a *AddrImpl) Send(data interface{}, sender Addr) error {
if a.deadletter {
deadLetters.Publish(DeadMail{
To: a,
Message: CreateEnvelope(sender, Header{}, data),
})
return nil
}
return a.actor.Receive(a, CreateEnvelope(sender, Header{}, data))
}
// SendWithHeader delivers provided raw data to this process providing destination/reply address.
func (a *AddrImpl) SendWithHeader(data interface{}, h Header, sender Addr) error {
if a.deadletter {
deadLetters.Publish(DeadMail{
To: a,
Message: CreateEnvelope(sender, h, data),
})
return nil
}
return a.actor.Receive(a, CreateEnvelope(sender, h, data))
}
// State returns state of actor.
func (a *AddrImpl) State() Signal {
if a.deadletter {
return RUNNING
}
return a.actor.State()
}
// Kill sends a kill signal to the underline process to stop all operations and to close immediately.
func (a *AddrImpl) Kill() error {
if a.deadletter {
return nil
}
return a.actor.Kill()
}
// Stop returns a error for the stopping of the underline actor for giving address.
func (a *AddrImpl) Stop() error {
if a.deadletter {
return nil
}
return a.actor.Stop()
}
// Restart returns a error for the restart of the underline actor for giving address.
func (a *AddrImpl) Restart() error {
if a.deadletter {
return nil
}
return a.actor.Restart()
}
// Destroy returns a error for the termination and destruction of the underline
// actor for giving address.
func (a *AddrImpl) Destroy() error {
if a.deadletter {
return nil
}
return a.actor.Destroy()
}
// Escalate implements the Escalator interface.
func (a *AddrImpl) Escalate(v interface{}) {
if a.deadletter {
deadLetters.Publish(v)
return
}
a.actor.Escalate(v, a)
}
// DeathWatch implements the DeathWatch interface.
func (a *AddrImpl) DeathWatch(addr Addr) error {
if a.deadletter {
return errors.WrapOnly(ErrHasNoActor)
}
return a.actor.DeathWatch(addr)
}
// Watch adds a giving function into the subscription
// listeners of giving address events.
func (a *AddrImpl) Watch(fn func(interface{})) Subscription {
if a.deadletter {
return subscriber{deadLetters.Subscribe(fn)}
}
return a.actor.Watch(fn)
}
// ID returns unique identification value for underline process of Addr.
func (a *AddrImpl) ID() string {
if a.deadletter {
return deadLetterID.String()
}
return a.actor.ID()
}
// Service returns the service name which the giving address represent as it's
// capability and functionality for giving actor.
func (a *AddrImpl) Service() string {
return a.service
}
// Addr returns the unique address which this address points to both the actor
// and service the address is presenting as the underline actor capability.
//
// Address uses a format: ActorAddress/ServiceName
//
func (a *AddrImpl) Addr() string {
if a.deadletter {
return formatAddrService2(FormatAddr(kitProtocol, kitNamespace, a.ID()), a.service)
}
return formatAddrService2(a.actor.Addr(), a.service)
}
// ProtocolAddr returns the Actors.ProtocolAddr() and Addr.ServiceName
// values in the format: Protocol@Namespace/Service.
func (a *AddrImpl) ProtocolAddr() string {
if a.deadletter {
return FormatNamespace(kitProtocol, kitNamespace)
}
return a.actor.ProtocolAddr()
}
// Protocol returns address actor protocol.
func (a *AddrImpl) Protocol() string {
if a.deadletter {
return kitProtocol
}
return a.actor.Protocol()
}
// Namespace returns address actor namespace.
func (a *AddrImpl) Namespace() string {
if a.deadletter {
return kitNamespace
}
return a.actor.Namespace()
}
// String returns address string of giving AddrImpl.
func (a *AddrImpl) String() string {
return a.Addr()
}