watch: Clean up watch when restarting mirror after an error (#3993)

Add cleanup for watch code when it exits and mirroring is restarted.
This commit is contained in:
Anis Elleuch
2022-02-25 20:15:21 -08:00
committed by GitHub
parent a1d6c3d4af
commit cce6451b63
4 changed files with 37 additions and 24 deletions
+3
View File
@@ -181,6 +181,9 @@ func (f *fsClient) Watch(ctx context.Context, options WatchOptions) (*WatchObjec
close(eventChan)
close(errorChan)
notify.Stop(in)
// At this point, notify is guaranteed to not write
// in 'in' channel so we can close it.
close(in)
}()
timeFormatFS := "2006-01-02T15:04:05.000Z"
+25 -17
View File
@@ -783,26 +783,34 @@ func (c *S3Client) Watch(ctx context.Context, options WatchOptions) (*WatchObjec
}
go func() {
// Start listening on all bucket events.
for notificationInfo := range eventsCh {
if notificationInfo.Err != nil {
var perr *probe.Error
if minio.ToErrorResponse(notificationInfo.Err).Code == "NotImplemented" {
perr = probe.NewError(APINotImplemented{
API: "Watch",
APIType: c.GetURL().String(),
})
} else {
perr = probe.NewError(notificationInfo.Err)
defer close(wo.EventInfoChan)
defer close(wo.ErrorChan)
for {
// Start listening on all bucket events.
select {
case notificationInfo, ok := <-eventsCh:
if !ok {
return
}
wo.Errors() <- perr
} else {
wo.Events() <- c.notificationToEventsInfo(notificationInfo)
if notificationInfo.Err != nil {
var perr *probe.Error
if minio.ToErrorResponse(notificationInfo.Err).Code == "NotImplemented" {
perr = probe.NewError(APINotImplemented{
API: "Watch",
APIType: c.GetURL().String(),
})
} else {
perr = probe.NewError(notificationInfo.Err)
}
wo.Errors() <- perr
} else {
wo.Events() <- c.notificationToEventsInfo(notificationInfo)
}
case <-wo.DoneChan:
return
}
}
close(wo.EventInfoChan)
close(wo.ErrorChan)
}()
return wo, nil
+2
View File
@@ -645,6 +645,8 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo)
// this goroutine will watch for notifications, and add modified objects to the queue
func (mj *mirrorJob) watchMirror(ctx context.Context) {
defer mj.watcher.Stop()
for {
select {
case events, ok := <-mj.watcher.Events():
+7 -7
View File
@@ -103,13 +103,11 @@ func (w *Watcher) Events() chan []EventInfo {
return w.EventInfoChan
}
// Watching returns if the watcher is watching for notifications
func (w *Watcher) Watching() bool {
return (len(w.o) > 0)
}
// Wait for watcher to wait
func (w *Watcher) Wait() {
// Stop all watchers
func (w *Watcher) Stop() {
for _, w := range w.o {
close(w.DoneChan)
}
w.wg.Wait()
}
@@ -135,6 +133,8 @@ func (w *Watcher) Join(ctx context.Context, client Client, recursive bool) *prob
for {
select {
case <-wo.DoneChan:
return
case events, ok := <-wo.Events():
if !ok {
return