Copyright 2020 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package poller supports periodic polling to load a value.
package poller

import (
	
	
	
)
A Getter returns a value.
type Getter func(context.Context) (interface{}, error)
A Poller maintains a current value, and refreshes it by periodically polling for a new value.
type Poller struct {
	getter  Getter
	onError func(error)
	mu      sync.Mutex
	current interface{}
}
New creates a new poller with an initial value. The getter is invoked to obtain updated values. Errors returned from the getter are passed to onError.
func ( interface{},  Getter,  func(error)) *Poller {
	return &Poller{
		getter:  ,
		onError: ,
		current: ,
	}
}
Start begins polling in a separate goroutine, at the given period. To stop the goroutine, cancel the context passed to Start.
func ( *Poller) ( context.Context,  time.Duration) {
	 := time.NewTicker()

	go func() {
		for {
			select {
			case <-.Done():
				.Stop()
				return
			case <-.C:
				,  := context.WithTimeout(, )
				.Poll()
				()
			}
		}
	}()
}
Poll calls p's getter immediately and synchronously.
func ( *Poller) ( context.Context) {
	,  := .getter()
	if  != nil {
		.onError()
	} else {
		.mu.Lock()
		.current = 
		.mu.Unlock()
	}
}
Current returns the current value. Initially, this is the value passed to New. After each successful poll, the value is updated. If a poll fails, the value remains unchanged.
func ( *Poller) () interface{} {
	.mu.Lock()
	defer .mu.Unlock()
	return .current