@@ -15,11 +15,12 @@ var _ ProjectionTrigger = (&NotificationQueue{}).ReQueue
1515type (
1616 // NotificationQueuer describes a smart queue for projection notifications
1717 NotificationQueuer interface {
18- Channel () chan * ProjectionNotification
18+ Open () chan struct {}
1919 Close ()
20+
2021 Empty () bool
21- Open () chan struct {}
22- PutBack ( * ProjectionNotification )
22+ Next (context. Context ) ( * ProjectionNotification , bool )
23+
2324 Queue (context.Context , * ProjectionNotification ) error
2425 ReQueue (context.Context , * ProjectionNotification ) error
2526 }
@@ -46,9 +47,12 @@ func newNotificationQueue(queueBuffer int, retryDelay time.Duration, metrics Met
4647 }
4748}
4849
49- // Channel returns the queue channel
50- func (nq * NotificationQueue ) Channel () chan * ProjectionNotification {
51- return nq .queue
50+ // Open enables the queue for business
51+ func (nq * NotificationQueue ) Open () chan struct {} {
52+ nq .done = make (chan struct {})
53+ nq .queue = make (chan * ProjectionNotification , nq .queueBuffer )
54+
55+ return nq .done
5256}
5357
5458// Close closes the queue channel
@@ -61,9 +65,22 @@ func (nq *NotificationQueue) Empty() bool {
6165 return len (nq .queue ) == 0
6266}
6367
64- // PutBack sends a notification to the queue channel without further ado
65- func (nq * NotificationQueue ) PutBack (notification * ProjectionNotification ) {
66- nq .queue <- notification
68+ // Next yields the next notification on the queue or stopped when processor has stopped
69+ func (nq * NotificationQueue ) Next (ctx context.Context ) (* ProjectionNotification , bool ) {
70+ for {
71+ select {
72+ case <- nq .done :
73+ return nil , true
74+ case <- ctx .Done ():
75+ return nil , true
76+ case notification := <- nq .queue :
77+ if notification != nil && notification .ValidAfter .After (time .Now ()) {
78+ nq .queue <- notification
79+ continue
80+ }
81+ return notification , false
82+ }
83+ }
6784}
6885
6986// Queue sends a notification to the queue
@@ -88,11 +105,3 @@ func (nq *NotificationQueue) ReQueue(ctx context.Context, notification *Projecti
88105
89106 return nq .Queue (ctx , notification )
90107}
91-
92- // Open enables the queue for business
93- func (nq * NotificationQueue ) Open () chan struct {} {
94- nq .done = make (chan struct {})
95- nq .queue = make (chan * ProjectionNotification , nq .queueBuffer )
96-
97- return nq .done
98- }
0 commit comments