feat(GODT-2803): Add Event Poll Waiter

Wait for the current event poll to finish publishing events after a
request to pause the event loop. This is required to change the gluon
cache directory.
This commit is contained in:
Leander Beernaert
2023-08-11 09:55:21 +02:00
parent a46533dcf2
commit a5500629e5
3 changed files with 154 additions and 0 deletions

View File

@ -61,6 +61,9 @@ type Service struct {
pendingSubscriptionsLock sync.Mutex
pendingSubscriptions []pendingSubscription
eventPollWaiters []*EventPollWaiter
eventPollWaitersLock sync.Mutex
}
func NewService(
@ -114,6 +117,21 @@ func (s *Service) Pause() {
atomic.StoreUint32(&s.paused, 1)
}
// PauseWithWaiter pauses the event polling and returns a waiter to notify when the last event has been published
// after the pause request.
func (s *Service) PauseWithWaiter() *EventPollWaiter {
s.log.Info("Pausing")
atomic.StoreUint32(&s.paused, 1)
waiter := newEventPollWaiter()
s.eventPollWaitersLock.Lock()
s.eventPollWaiters = append(s.eventPollWaiters, waiter)
s.eventPollWaitersLock.Unlock()
return waiter
}
// Resume resumes the event polling.
func (s *Service) Resume() {
atomic.StoreUint32(&s.paused, 0)
@ -163,6 +181,7 @@ func (s *Service) run(ctx context.Context, lastEventID string) {
return
case <-s.timer.C:
if s.IsPaused() {
s.closePollWaiters()
continue
}
}
@ -223,6 +242,10 @@ func (s *Service) run(ctx context.Context, lastEventID string) {
}
lastEventID = newEventID
if s.IsPaused() {
s.closePollWaiters()
}
}
}
@ -249,6 +272,17 @@ func (s *Service) Close() {
s.pendingSubscriptions = nil
}
func (s *Service) closePollWaiters() {
s.eventPollWaitersLock.Lock()
defer s.eventPollWaitersLock.Unlock()
for _, v := range s.eventPollWaiters {
v.close()
}
s.eventPollWaiters = nil
}
func (s *Service) handleEvent(ctx context.Context, lastEventID string, event proton.Event) error {
s.log.WithFields(logrus.Fields{
"old": lastEventID,