Merge pull request #126125 from mprahl/stop-idempotent
Allow calling Stop multiple times on RetryWatcher
This commit is contained in:
		@@ -22,6 +22,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	apierrors "k8s.io/apimachinery/pkg/api/errors"
 | 
						apierrors "k8s.io/apimachinery/pkg/api/errors"
 | 
				
			||||||
@@ -53,6 +54,7 @@ type RetryWatcher struct {
 | 
				
			|||||||
	stopChan            chan struct{}
 | 
						stopChan            chan struct{}
 | 
				
			||||||
	doneChan            chan struct{}
 | 
						doneChan            chan struct{}
 | 
				
			||||||
	minRestartDelay     time.Duration
 | 
						minRestartDelay     time.Duration
 | 
				
			||||||
 | 
						stopChanLock        sync.Mutex
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewRetryWatcher creates a new RetryWatcher.
 | 
					// NewRetryWatcher creates a new RetryWatcher.
 | 
				
			||||||
@@ -286,7 +288,15 @@ func (rw *RetryWatcher) ResultChan() <-chan watch.Event {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Stop implements Interface.
 | 
					// Stop implements Interface.
 | 
				
			||||||
func (rw *RetryWatcher) Stop() {
 | 
					func (rw *RetryWatcher) Stop() {
 | 
				
			||||||
 | 
						rw.stopChanLock.Lock()
 | 
				
			||||||
 | 
						defer rw.stopChanLock.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Prevent closing an already closed channel to prevent a panic
 | 
				
			||||||
 | 
						select {
 | 
				
			||||||
 | 
						case <-rw.stopChan:
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
		close(rw.stopChan)
 | 
							close(rw.stopChan)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Done allows the caller to be notified when Retry watcher stops.
 | 
					// Done allows the caller to be notified when Retry watcher stops.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -585,6 +585,8 @@ func TestRetryWatcherToFinishWithUnreadEvents(t *testing.T) {
 | 
				
			|||||||
	// Give the watcher a chance to get to sending events (blocking)
 | 
						// Give the watcher a chance to get to sending events (blocking)
 | 
				
			||||||
	time.Sleep(10 * time.Millisecond)
 | 
						time.Sleep(10 * time.Millisecond)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						watcher.Stop()
 | 
				
			||||||
 | 
						// Verify a second stop does not cause a panic
 | 
				
			||||||
	watcher.Stop()
 | 
						watcher.Stop()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	maxTime := time.Second
 | 
						maxTime := time.Second
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user