-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrxc.go
1223 lines (1123 loc) · 33.1 KB
/
rrxc.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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Package rrxc is a Request/Response eXchange Controller for synchronizing an
operation against remote asynchronous backends (AKA sync/async or
sync-over-async). The package does not exclusively support sync-over-async as
an exchange in rrxc can very well be fully asynchronous, why it was named a
controller - rrxc correlates requests with responses.
Example usages:
// In main, before starting the http server and queue consumers...
controller := rrxc.NewController()
// In the http server handler...
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
ctx = controller.NewExchangeContext(ctx)
xc, err := rrxc.ExchangeFromContext(ctx)
// handler error
defer xc.Close()
// Use ID in your outgoing message, the other side would have to include it in
// their response.
id := xc.NewCorrelID()
reg := &rrxc.Registration{
CorrelID: id,
Message: "hello world",
}
if err := xc.RegisterRequest(reg); err != nil {
return err
}
producer.Publish(queue, fmt.Sprintf(`{"id":%q,"message":%q}`, reg.CorrelID, reg.Message.(string)))
result, err := rrxc.Wait(ctx) // wait for RegisterResponse (in msghandler below)
if err != nil {
return err
}
// In the message handler (consumer, in the same app as the http server)...
if controller.HasTag(correlatingID, controller.GetRollbackTag()) {
// Silently ACK a rollback-message
return nil
}
if err := controller.RegisterResponse(&rrxc.Registration{
CorrelID: correlatingID,
Message: "hello there",
FailOnDuplicate: true,
}); err != nil {
if errors.Is(err, rrxc.ErrHaveNoCorrelatedRequest) {
// Message was not for me, requeue it?
return err
} else if errors.Is(err, rrxc.ErrDuplicate) {
// Handle additional response with the same correlation ID
return nil
}
return err
}
*/
package rrxc
import (
"context"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"log"
"sync/atomic"
"time"
"github.com/sa6mwa/rrxc/pkg/anystore"
)
const (
defaultRollbackTag string = "rollback"
defaultRollbackTagLifespan time.Duration = 3 * time.Hour
)
// A controller handles multiple exchanges.
type Controller interface {
// Controller_SetRollbackTag sets the name of the rollback tag (by default "rollback")
// which a correlID is tagged with when a request in an exchange context is not
// responded to when the exchange is terminated by a cancelled context.
SetRollbackTag(tag string) Controller
// Controller_GetRollbackTag returns the configured rollback tag (default
// "rollback").
GetRollbackTag() string
// SetRollbacktagLifespan sets the timeout until the rollback tag is removed
// (default 3h). A value of 0 disables the timeout (not recommended).
SetRollbackTagLifespan(d time.Duration) Controller
// Controller.NewControllerContext stores the already initiated controller in
// ctx Values to be retrieved using rrxc.ControllerFromContext. Returns a
// derived context.
NewControllerContext(ctx context.Context) context.Context
// NewExchangeContext is usually executed inside a HTTP handler function or MQ
// message handler function.
//
// ctx := controller.NewExchangeContext(context.Background())
NewExchangeContext(ctx context.Context) context.Context
// NewCorrelID returns a new correlation identifier to stamp on requests. The
// returned CorrelID is guaranteed not to conflict with another request in any
// of the controller's exchanges.
NewCorrelID() string
// HasRequest returns true if this controller has a request with given
// correlID or false if not.
HasRequest(correlID string) bool
// GetRequestAge returns age (time.Duration) of request or error if request
// with correlID does not exist.
GetRequestAge(correlID string) (time.Duration, error)
// RegisterResponse registers a response to a previous request based on
// correlation ID. Input is a rrxc.Registration struct where CorrelID,
// Message, OverwriteOnDuplicate and FailOnDuplicate are used. If you only
// fill in CorrelID and Message, function will return
// rrxc.ErrHaveNoCorrelatedRequest if there is no correlated request. If
// OverwriteOnDuplicate is set to true and FailOnDuplicate is false, function
// will silently return nil (without overwriting a previous response).
RegisterResponse(r *Registration) error
// Synchronize creates a new Exchange and configures a struct with Context,
// Controller and Exchange. The orchestration function will wait until all
// requests have been responded to or the context has timed out.
//
// Usage:
//
// xcresult, err := controller.Synchronize(rrxc.ExchangeFromContext(context.WithTimeout(context.Background(), 15 * time.Second), func(sb SyncBundle) error {
// sb.Exchange.RegisterRequest(...)
// }))
Synchronize(ctx context.Context, operation func(sb SyncBundle) error) (ExchangeResult, error)
// Tag tags the entity with tag and - optionally - sends the entity value
// through the channel(s) specified in notificationChannels in a separate
// goroutine.
Tag(entity any, tag any, notificationChannels ...chan any)
// Untag removes tag from entity and - optionally - sends the entity value
// through the channel(s) specified in notificationChannels in a separate
// goroutine. Returns nil or the only error rrxc.ErrNoSuchEntity if entity was
// not previously tagged.
Untag(entity any, tag any, notificationChannels ...chan any) error
// If zero tags were given HasTag will consider that you want to know whether
// the entity is untagged or not. If it is tagged and zero tags were given,
// HasTag will return false.
HasTag(entity any, tags ...any) bool
// Close the controller (optional) and terminate all child exchanges.
Close()
}
// An exchange is a set of request/response pairs (or just one pair).
type Exchange interface {
// Controller returns the controller attached to this exchange so that
// you can do controller operations even if you only access to an exchange
// context. Can not be used as a chained method as it returns error if the
// exchange could not be retrieved.
Controller() (Controller, error)
// GetID returns the *ExchangeID* of this exchange (not to be confused with
// CorrelID).
GetID() string
// NewCorrelID returns a new correlation identifier to stamp on requests. The
// returned CorrelID is guaranteed not to conflict with another request in any
// of the parent controller's other exchanges.
NewCorrelID() string
// HasCorrelID returns true if this exchange has correlID as an identifier of a
// request/response. Returns false if not.
HasCorrelID(correlID string) bool
// RegisterRequest registers an outgoing request in the controller
// (before you send the actual request). RegisterRequest takes a Registration
// struct where CorrelID, Message and NotifyOnResponse are the only relevant
// fields. Function only returns rrxc.ErrUnableToLoadExchange on error.
//
// notificationChannelsOnResponse are synchronous, if one in the list blocks on
// send, the rest will wait to be notified. TODO: Not sure which behaviour is
// more favourable: asynchronous or synchronous. It's easy to make
// asynchronous, each channel could have it's own goroutine. Behaviour could be
// configurable in the new-constructor.
RegisterRequest(r *Registration) error
// RegisterResponse registers a response to a previous request based on
// correlation ID. Input is a rrxc.Registration struct where CorrelID, Message,
// OverwriteOnDuplicate and FailOnDuplicate are used. If you only fill in
// CorrelID and Message, function will return rrxc.ErrHaveNoCorrelatedRequest if
// there is no correlated request. If OverwriteOnDuplicate is set to true and
// FailOnDuplicate is false, function will silently return nil (without
// overwriting a previous response).
RegisterResponse(r *Registration) error
// HasRequest returns true if a request in this exchange exists with specified
// correlID.
HasRequest(correlID string) bool
// GetExchangeResult returns an ExchangeResult struct. It is recommended to
// call this function after confirming the Exchange has ended.
GetExchangeResult() ExchangeResult
// GetRequestsAndResponses returns a slice with all RequestResponse objects in
// this exchange.
GetRequestsAndResponses() []RequestResponse
// GetRequest returns the message data in request with correlID or error
// (ErrUnableToLoadExchange or ErrNoSuchRequest).
GetRequest(correlID string) (any, error)
// GetResponse returns the message data in response with correlID or error
// (ErrUnableToLoadExchange or ErrNoSuchResponse).
GetResponse(correlID string) (any, error)
// GetRequestAge returns age (time.Duration) of request (in this exchange) or
// error if request with correlID does not exist.
GetRequestAge(correlID string) (time.Duration, error)
// Done returns a receive-only channel which is closed when all requests in
// the exchange have been responded to.
Done() <-chan struct{}
// Close exchange.
Close()
}
// Passed to the operation function in controller.Sync and controller.SyncWithoutExchange
type SyncBundle struct {
Context context.Context
Controller Controller
Exchange Exchange
}
// type SyncBundle struct {
// Context context.Context
// Controller Controller
// Exchange Exchange
// }
// RequestResponse is returned through notificationChannelsOnResponse
type ExchangeResult struct {
ExchangeID string
Created time.Time
Finished time.Time
Latency time.Duration
RequestsAndResponses []RequestResponse
AllRequestsRespondedTo bool
}
type RequestResponse struct {
CorrelID string
Request any
Response any
RespondedTo bool
RequestRegistered time.Time
ResponseRegistered time.Time
Latency time.Duration
}
type Registration struct {
CorrelID string
Message any
NotifyOnResponse []chan RequestResponse
OverwriteOnDuplicate bool
FailOnDuplicate bool
}
// Key used to store and load the controller interface from the AnyStore in the
// context value (of a context.Context).
type controllerKey struct{}
type controller struct {
contexts anystore.AnyStore
mapOfMaps anystore.AnyStore // map[any]any = make(map[any]any))
rollbackTag atomic.Value
rollbackTagLifespan atomic.Value
closed atomic.Value
done chan struct{}
}
type tagMap map[any]struct{}
// Key used to store and load the exchange interface from the AnyStore in the
// atomix struct.
type exchangeKey struct{}
// Un-exported exchange is returned wrapped inside an atomix struct which is
// instantiated by NewExchangeContext. The exchange struct does not have a
// (public) interface as the Exchange interface is attached to the atomix
// struct.
type exchange struct {
controller Controller
id string
created time.Time
finished time.Time
latency time.Duration
requests anystore.AnyStore
responses anystore.AnyStore
durable bool // Not implemented yet
finalize chan struct{}
finalized bool
done chan struct{}
closed bool
succeeded bool
}
// atomix wraps the exchange struct in an AnyStore and what all Exchange
// receiver functions are attached to.
type atomix struct {
anystore.AnyStore
}
//type requestsMap map[string]requestStruct
//type responsesMap map[string]responseStruct
// An instance of requestStruct is stored as the value of the requests
// correlationIdentifierMap in an exchange instance.
type requestStruct struct {
request any
registered time.Time
notificationChannelsOnResponse []chan RequestResponse
completed bool
}
type responseStruct struct {
response any
registered time.Time
}
// Default NewID function is NewID256 returning a hex-encoded sha256 string
// based of a random number. NewID256 can be replaced by a custom function or
// the other provided function NewID256...
//
// rrxc.NewID = rrxc.NewID512
var NewID func() string = NewID256
var (
ErrNoControllerContext error = errors.New("context has no request/response controller")
ErrNoExchangeInContext error = errors.New("context has no request/response exchange")
ErrUnableToLoadExchange error = errors.New("unable to load exchange, key not found")
ErrCorrelIDConflict error = errors.New("correlation identifier conflict: already have a request with that correlID")
//ErrNoTagsGiven error = errors.New("no tags given")
ErrNoSuchEntity error = errors.New("no such entity")
ErrHaveNoCorrelatedRequest error = errors.New("request/response correlation failure: no request found to associate response with")
ErrNoSuchRequest error = errors.New("no such request")
ErrNoSuchResponse error = errors.New("no such response")
ErrDuplicate error = errors.New("duplicate response: have already registered a response for this correlation identifier")
)
// Start with all non-receiver functions
// NewController is initialized before starting HTTP servers or message
// handlers, usually in the main function. The return Controller should be used
// in HandleFuncs and message handlers.
func NewController() Controller {
ctrlr := &controller{
contexts: anystore.New(),
mapOfMaps: anystore.New(),
done: make(chan struct{}),
}
ctrlr.rollbackTag.Store(defaultRollbackTag)
ctrlr.rollbackTagLifespan.Store(defaultRollbackTagLifespan)
return ctrlr
}
// NewControllerContext calls NewController and stores it in the context.Values
// which is simple to load using rrxc.ControllerFromContext. Returns a derived
// context which is to be used to derive Exchange contexts via
// mycontroller.NewExchangeContext. See related ControllerFromContext to load a
// controller context.
func NewControllerContext(ctx context.Context) context.Context {
return context.WithValue(ctx, controllerKey{}, NewController())
}
// ControllerFromContext returns the Controller stored (by NewControllerContext)
// in ctx or error rrxc.ErrNoControllerContext.
func ControllerFromContext(ctx context.Context) (Controller, error) {
ctrlr, ok := ctx.Value(controllerKey{}).(*controller)
if !ok {
return nil, ErrNoControllerContext
}
return ctrlr, nil
}
// ExchangeFromContext returns the Exchange stored (by NewExchangeContext) in
// ctx or error rrxc.ErrNoExchangeInContext.
func ExchangeFromContext(ctx context.Context) (Exchange, error) {
x, ok := ctx.Value(exchangeKey{}).(atomix)
if !ok {
return nil, ErrNoExchangeInContext
}
return x, nil
}
// RegisterRequestByContext registers an outgoing request in the Exchange
// context stored in ctx. RegisterRequest takes a Registration struct where
// CorrelID, Message and NotifyOnResponse are the only relevant fields. Function
// only returns rrxc.ErrUnableToLoadExchange on error.
func RegisterRequestByContext(ctx context.Context, r *Registration) error {
xc, ok := ctx.Value(exchangeKey{}).(atomix)
if !ok {
return ErrNoExchangeInContext
}
return xc.RegisterRequest(r)
}
// RegisterResponseByContext registers a response to a previous request in the
// Exchange context ctx based on correlation ID. Input is a rrxc.Registration
// struct where CorrelID, Message, OverwriteOnDuplicate and FailOnDuplicate are
// used. If you only fill in CorrelID and Message, function will return
// rrxc.ErrHaveNoCorrelatedRequest if there is no correlated request. If
// OverwriteOnDuplicate is set to true and FailOnDuplicate is false, function
// will silently return nil (without overwriting a previous response).
func RegisterResponseByContext(ctx context.Context, r *Registration) error {
xc, ok := ctx.Value(exchangeKey{}).(atomix)
if !ok {
return ErrNoExchangeInContext
}
return xc.RegisterResponse(r)
}
// Wait is a synchronization primitive simply waiting until the Exchange context
// ctx is finished (i.e all requests have been responded to) or the context has
// been cancelled (e.g timed out). Returns an ExchangeResult object or error.
func Wait(ctx context.Context) (ExchangeResult, error) {
xc, err := ExchangeFromContext(ctx)
if err != nil {
return ExchangeResult{}, err
}
defer xc.Close()
select {
case <-ctx.Done():
case <-xc.Done():
}
return xc.GetExchangeResult(), nil
}
// CloseExchangeByContext closes an exchange by it's context ctx. Returns
// ErrNoExchangeInContext in case of error.
func CloseExchangeByContext(ctx context.Context) error {
xc, ok := ctx.Value(exchangeKey{}).(atomix)
if !ok {
return ErrNoExchangeInContext
}
xc.Close()
return nil
}
// NewID256 returns a random hex-encoded sha256 hash as a string.
func NewID256() string {
b := make([]byte, 128)
rand.Read(b) // Care not about errors
h := sha256.New()
n := time.Now().UTC().UnixNano()
nanobytes := []byte{
byte(0xff & n),
byte(0xff & (n >> 8)),
byte(0xff & (n >> 16)),
byte(0xff & (n >> 24)),
byte(0xff & (n >> 32)),
byte(0xff & (n >> 40)),
byte(0xff & (n >> 48)),
byte(0xff & (n >> 56)),
}
h.Write(append(b, nanobytes...))
return hex.EncodeToString(h.Sum(nil))
}
// NewID512 returns a random hex-encoded sha512 hash as a string.
func NewID512() string {
b := make([]byte, 256)
rand.Read(b)
h := sha512.New()
n := time.Now().UTC().UnixNano()
nanobytes := []byte{
byte(0xff & n),
byte(0xff & (n >> 8)),
byte(0xff & (n >> 16)),
byte(0xff & (n >> 24)),
byte(0xff & (n >> 32)),
byte(0xff & (n >> 40)),
byte(0xff & (n >> 48)),
byte(0xff & (n >> 56)),
}
h.Write(append(b, nanobytes...))
return hex.EncodeToString(h.Sum(nil))
}
// Controller interface
func (c *controller) GetExchangeByCorrelID(correlID string) (Exchange, error) {
keys, err := c.contexts.Keys()
if err != nil {
return nil, err
}
for _, exchangeID := range keys {
val, err := c.contexts.Load(exchangeID)
if err != nil {
return nil, err
}
ctx, ok := val.(context.Context)
if !ok {
continue
}
xc, err := ExchangeFromContext(ctx)
if err != nil {
continue
}
if xc.HasRequest(correlID) {
return xc, nil
}
}
return nil, ErrHaveNoCorrelatedRequest
}
func (c *controller) SetRollbackTag(tag string) Controller {
c.rollbackTag.Store(tag)
return c
}
func (c *controller) GetRollbackTag() string {
return c.rollbackTag.Load().(string)
}
func (c *controller) SetRollbackTagLifespan(d time.Duration) Controller {
c.rollbackTagLifespan.Store(d)
return c
}
func (c *controller) NewControllerContext(ctx context.Context) context.Context {
return context.WithValue(ctx, controllerKey{}, c)
}
func (c *controller) NewExchangeContext(ctx context.Context) context.Context {
xc := exchange{
controller: c,
id: NewID(),
created: time.Now(),
requests: anystore.New(),
responses: anystore.New(),
finalize: make(chan struct{}),
done: make(chan struct{}),
}
axc := atomix{
anystore.New(),
}
axc.Store(exchangeKey{}, xc)
newContext := context.WithValue(ctx, exchangeKey{}, axc)
c.contexts.Store(xc.id, newContext)
// A goroutine will remove this exchange from the contexts map and mark it as
// terminated when the exchange is done or the context is cancelled.
go func() {
tagForRollback := true
select {
case <-c.done:
tagForRollback = false
case <-xc.finalize:
case <-newContext.Done():
}
c.contexts.Delete(xc.id)
if axc.HasKey(exchangeKey{}) {
axc.Run(func(a anystore.AnyStore) error {
val, err := a.Load(exchangeKey{})
if err != nil {
return err
}
grxc, ok := val.(exchange)
if ok {
if tagForRollback {
keys, err := grxc.requests.Keys()
if err != nil {
return err
}
for _, correlID := range keys {
val, err := grxc.requests.Load(correlID)
if err != nil {
return err
}
r, ok := val.(requestStruct)
if !ok {
continue
}
if !r.completed {
c.Tag(correlID, c.rollbackTag.Load().(string))
to := c.rollbackTagLifespan.Load().(time.Duration)
// The following goroutine will remove the rollback tag from
// this correlID after 24 hours or when/if the controller is
// closed (c never sends anything on c.done, just closes it).
go func(cid string) {
if to > 0 {
t := time.NewTimer(to)
select {
case <-t.C:
case <-c.done:
if !t.Stop() {
<-t.C
}
}
} else {
<-c.done
}
c.Untag(cid, c.rollbackTag.Load().(string))
}(correlID.(string))
}
}
}
if !grxc.closed {
grxc.closed = true
}
grxc.finished = time.Now()
grxc.latency = grxc.finished.Sub(grxc.created)
a.Store(exchangeKey{}, grxc)
}
return nil
})
}
close(xc.done)
}()
return newContext
}
func (c *controller) NewCorrelID() string {
correlID := NewID()
for {
hasCorrelID := false
keys, err := c.contexts.Keys()
if err != nil {
return correlID
}
for _, exchangeID := range keys {
val, err := c.contexts.Load(exchangeID)
if err != nil {
return correlID
}
ctx, ok := val.(context.Context)
if !ok {
continue
}
xc, err := ExchangeFromContext(ctx)
if err != nil {
log.Print("[ERROR] Unable to load exchange from context")
continue
}
if xc.HasCorrelID(correlID) {
hasCorrelID = true
continue
}
}
if !hasCorrelID {
break
}
}
return correlID
}
func (c *controller) HasRequest(correlID string) bool {
keys, err := c.contexts.Keys()
if err != nil {
return false
}
for _, exchangeID := range keys {
val, err := c.contexts.Load(exchangeID)
if err != nil {
continue
}
ctx, ok := val.(context.Context)
if !ok {
continue
}
xc, err := ExchangeFromContext(ctx)
if err != nil {
log.Print("[ERROR] Unable to load exchange from context")
continue
}
if xc.HasRequest(correlID) {
return true
}
}
return false
}
func (c *controller) GetRequestAge(correlID string) (time.Duration, error) {
keys, err := c.contexts.Keys()
if err != nil {
return -1, err
}
for _, exchangeID := range keys {
val, err := c.contexts.Load(exchangeID)
if err != nil {
continue
}
ctx, ok := val.(context.Context)
if !ok {
continue
}
xc, err := ExchangeFromContext(ctx)
if err != nil {
log.Print("[ERROR] Unable to load exchange from context")
continue
}
r, err := xc.GetRequestAge(correlID)
if err != nil {
continue
}
return r, nil
}
return -1, ErrNoSuchRequest
}
func (c *controller) RegisterResponse(r *Registration) error {
keys, err := c.contexts.Keys()
if err != nil {
return err
}
for _, exchangeID := range keys {
val, err := c.contexts.Load(exchangeID)
if err != nil {
continue
}
ctx, ok := val.(context.Context)
if !ok {
continue
}
xc, err := ExchangeFromContext(ctx)
if err != nil {
continue
}
if xc.HasRequest(r.CorrelID) {
return xc.RegisterResponse(r)
}
}
return ErrHaveNoCorrelatedRequest
}
// // Moved to pkg level
// func (c *controller) RegisterRequestByContext(ctx context.Context, r *Registration) error {
// xc, ok := ctx.Value(exchangeKey{}).(atomix)
// if !ok {
// return ErrNoExchangeInContext
// }
// return xc.RegisterRequest(r)
// }
// // Moved to pkg level
// func (c *controller) RegisterResponseByContext(ctx context.Context, r *Registration) error {
// xc, ok := ctx.Value(exchangeKey{}).(atomix)
// if !ok {
// return ErrNoExchangeInContext
// }
// return xc.RegisterResponse(r)
// }
func (c *controller) Synchronize(ctx context.Context, operation func(sb SyncBundle) error) (ExchangeResult, error) {
xc, err := ExchangeFromContext(ctx)
if err != nil {
return ExchangeResult{}, err
}
defer xc.Close()
syncBundle := SyncBundle{
Context: ctx,
Controller: c,
Exchange: xc,
}
if err := operation(syncBundle); err != nil {
return ExchangeResult{}, err
}
select {
case <-ctx.Done():
case <-xc.Done():
}
return xc.GetExchangeResult(), nil
}
// // Moved to pkg level
// func (c *controller) Wait(ctx context.Context) (ExchangeResult, error) {
// xc, err := ExchangeFromContext(ctx)
// if err != nil {
// return ExchangeResult{}, err
// }
// defer xc.Close()
// select {
// case <-ctx.Done():
// case <-xc.Done():
// }
// return xc.GetExchangeResult(), nil
// }
func (c *controller) Tag(entity any, tag any, notificationChannels ...chan any) {
c.mapOfMaps.Run(func(mm anystore.AnyStore) error {
kv := make(tagMap)
kv[tag] = struct{}{}
mm.Store(entity, kv)
return nil
})
if len(notificationChannels) > 0 {
go func() {
for _, ch := range notificationChannels {
ch <- entity
close(ch)
}
}()
}
}
func (c *controller) Untag(entity any, tag any, notificationChannels ...chan any) error {
err := c.mapOfMaps.Run(func(mm anystore.AnyStore) error {
val, err := c.mapOfMaps.Load(entity)
if err != nil {
return err
}
entityTagMap, ok := val.(tagMap)
if !ok {
return ErrNoSuchEntity
}
delete(entityTagMap, tag)
// If there are no more tags on this entity, delete the entity from the
// store, otherwise commit the updated map.
if len(entityTagMap) == 0 {
mm.Delete(entity)
} else {
mm.Store(entity, entityTagMap)
}
return nil
})
if err != nil {
return err
}
if len(notificationChannels) > 0 {
go func() {
for _, ch := range notificationChannels {
ch <- entity
close(ch)
}
}()
}
return nil
}
func (c *controller) HasTag(entity any, tags ...any) bool {
val, err := c.mapOfMaps.Load(entity)
if err != nil {
return false
}
m, ok := val.(tagMap)
if !ok {
return false
}
if len(tags) == 0 {
// Considered you want to know if the entity is untagged, meaning there
// should be no tags on this entity in the tag map for this to return true.
if len(m) > 0 {
return false
}
} else {
for _, tag := range tags {
if _, found := m[tag]; !found {
return false
}
}
}
return true
}
func (c *controller) Close() {
if c.closed.Load() == nil {
c.closed.Store(struct{}{})
close(c.done)
}
}
// Exchange interface
//
// Receiver functions attached to atomix implement the Exchange interface.
func (x atomix) Controller() (Controller, error) {
val, err := x.Load(exchangeKey{})
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrUnableToLoadExchange, err)
}
xc, ok := val.(exchange)
if !ok {
return nil, ErrUnableToLoadExchange
}
return xc.controller, nil
}
func (x atomix) GetID() string {
val, err := x.Load(exchangeKey{})
if err != nil {
return ""
}
xc, ok := val.(exchange)
if !ok {
return ""
}
return xc.id
}
func (x atomix) NewCorrelID() string {
val, err := x.Load(exchangeKey{})
if err != nil {
// Instead of panicking, just return a NewID, the risk that it already
// exists as an ID or CorrelID in this exchange is still extremely remote.
return NewID()
}
xc, ok := val.(exchange)
if !ok {
// Instead of panicking, just return a NewID, the risk that it already
// exists as an ID or CorrelID in this exchange is still extremely remote.
return NewID()
}
return xc.controller.NewCorrelID()
}
func (x atomix) HasCorrelID(correlID string) bool {
val, err := x.Load(exchangeKey{})
if err != nil {
return false
}
xc, ok := val.(exchange)
if !ok {
return false
}
val, err = xc.requests.Load(correlID)
if err != nil {
return false
}
_, exist := val.(requestStruct)
return exist
}
func (x atomix) RegisterRequest(r *Registration) error {
return x.Run(func(a anystore.AnyStore) error {
val, err := a.Load(exchangeKey{})
if err != nil {
return err
}
xc, ok := val.(exchange)
if !ok {
return ErrUnableToLoadExchange
}
xc.requests.Store(r.CorrelID, requestStruct{
request: r.Message,
registered: time.Now(),
notificationChannelsOnResponse: r.NotifyOnResponse,
})
// Commit changes
return a.Store(exchangeKey{}, xc)
})
}
func (x atomix) RegisterResponse(r *Registration) error {
return x.Run(func(a anystore.AnyStore) error {
val, err := a.Load(exchangeKey{})
if err != nil {
return err
}
xc, ok := val.(exchange)
if !ok {
return ErrUnableToLoadExchange
}
val, err = xc.requests.Load(r.CorrelID)
if err != nil {
return err
}
request, exist := val.(requestStruct)
if !exist {
return ErrHaveNoCorrelatedRequest
}
val, err = xc.responses.Load(r.CorrelID)
if err != nil {
return err
}
if _, exist := val.(responseStruct); exist && !r.OverwriteOnDuplicate {
if r.FailOnDuplicate {
return ErrDuplicate
}
return nil
}
request.completed = true
xc.requests.Store(r.CorrelID, request)
timeRegistered := time.Now()
xc.responses.Store(r.CorrelID, responseStruct{
response: r.Message,
registered: timeRegistered,
})
// If all requests have been responded to, we are done, you can not add
// another request after the last response has finished.
closeExchange := true
keys, err := xc.requests.Keys()
if err != nil {
return err