diff options
Diffstat (limited to 'internal/discovery/manager.go')
| -rw-r--r-- | internal/discovery/manager.go | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/internal/discovery/manager.go b/internal/discovery/manager.go deleted file mode 100644 index 4fb4f8e..0000000 --- a/internal/discovery/manager.go +++ /dev/null @@ -1,92 +0,0 @@ -package discovery - -import ( - "context" - "log/slog" - "sync" -) - -// Manager is the discovery struct that is cached -// with some bookkeeping to avoid race conditions -type Manager struct { - disco *Discovery - - cancel context.CancelFunc - mu sync.RWMutex - wait sync.WaitGroup -} - -// NewManager creates a new Discovery manager -func NewManager(disco *Discovery) *Manager { - return &Manager{disco: disco} -} - -func (m *Manager) lock(write bool) { - if write { - m.mu.Lock() - return - } - m.mu.RLock() -} - -func (m *Manager) unlock(write bool) { - if write { - m.mu.Unlock() - return - } - m.mu.RUnlock() -} - -// Discovery gets the cached discovery -// `write` is true if discovery will be written to -func (m *Manager) Discovery(write bool) (*Discovery, func()) { - if write { - m.wait.Wait() - } - m.lock(write) - return m.disco, func() { - m.unlock(write) - } -} - -// Cancel aborts the running discovery startup process -func (m *Manager) Cancel() { - if m.cancel != nil { - m.cancel() - } - m.wait.Wait() -} - -// Startup handles the discovery process in the background -// It's called Startup because it's called when the lib is initialised -func (m *Manager) Startup(ctx context.Context, cb func()) { - ctx, cancel := context.WithCancel(ctx) - m.cancel = cancel - m.wait.Add(1) - go func() { - m.lock(false) - discoCopy, err := m.disco.Copy() - if err != nil { - slog.Warn("failed to clone discovery", "error", err) - return - } - m.unlock(false) - // we already log the warning - discoCopy.Servers(ctx, false) //nolint:errcheck - - m.lock(true) - m.disco.UpdateServers(discoCopy.ServerList) - m.unlock(true) - m.wait.Done() - - select { - case <-ctx.Done(): - return - default: - if cb == nil { - return - } - cb() - } - }() -} |
