mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-23 10:26:44 +00:00
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:
@ -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,
|
||||
|
||||
Reference in New Issue
Block a user