@@ -17,6 +17,7 @@ type Outbox struct {
17
17
queue []* outboxMsg
18
18
slots chan int
19
19
process chan int
20
+ stopC chan struct {}
20
21
}
21
22
22
23
type forwardFunction func (msg * message.Message ) error
@@ -29,6 +30,7 @@ func NewOutbox(config *Config) *Outbox {
29
30
queue : make ([]* outboxMsg , config .NumberSlots ),
30
31
slots : make (chan int , config .NumberSlots ),
31
32
process : make (chan int ),
33
+ stopC : make (chan struct {}),
32
34
}
33
35
// fill up outbox slots
34
36
for i := 0 ; i < cap (outbox .slots ); i ++ {
@@ -44,7 +46,7 @@ func (o *Outbox) Start() {
44
46
45
47
func (o * Outbox ) Stop () {
46
48
log .Info ("Stopping outbox" )
47
- close (o .process )
49
+ close (o .stopC )
48
50
}
49
51
50
52
// Enqueue a new element in the outbox if there is any slot available.
@@ -56,7 +58,10 @@ func (o *Outbox) Enqueue(outboxMsg *outboxMsg) error {
56
58
o .queue [slot ] = outboxMsg
57
59
metrics .GetOrRegisterGauge ("pss.outbox.len" , nil ).Update (int64 (o .len ()))
58
60
// we send this message slot to process
59
- o .process <- slot
61
+ select {
62
+ case <- o .stopC :
63
+ case o .process <- slot :
64
+ }
60
65
return nil
61
66
default :
62
67
metrics .GetOrRegisterCounter ("pss.enqueue.outbox.full" , nil ).Inc (1 )
@@ -68,11 +73,10 @@ func (o *Outbox) Enqueue(outboxMsg *outboxMsg) error {
68
73
func (o * Outbox ) processOutbox () {
69
74
for slot := range o .process {
70
75
go func (slot int ) {
71
- msg := o .msg ( slot )
76
+ msg := o .queue [ slot ]
72
77
metrics .GetOrRegisterResettingTimer ("pss.handle.outbox" , nil ).UpdateSince (msg .startedAt )
73
78
if err := o .forwardFunc (msg .msg ); err != nil {
74
79
metrics .GetOrRegisterCounter ("pss.forward.err" , nil ).Inc (1 )
75
- // if we failed to forward, re-insert message in the queue
76
80
log .Debug (err .Error ())
77
81
// requeue the message for processing
78
82
o .requeue (slot )
@@ -86,18 +90,20 @@ func (o *Outbox) processOutbox() {
86
90
}
87
91
}
88
92
89
- func (o * Outbox ) msg (slot int ) * outboxMsg {
90
- return o .queue [slot ]
91
- }
92
-
93
93
func (o * Outbox ) free (slot int ) {
94
- o .slots <- slot
94
+ select {
95
+ case <- o .stopC :
96
+ case o .slots <- slot :
97
+ }
98
+
95
99
}
96
100
97
101
func (o * Outbox ) requeue (slot int ) {
98
- o .process <- slot
102
+ select {
103
+ case <- o .stopC :
104
+ case o .process <- slot :
105
+ }
99
106
}
100
-
101
107
func (o * Outbox ) len () int {
102
108
return cap (o .slots ) - len (o .slots )
103
109
}
0 commit comments