2016-01-06 17:57:02 -05:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2016-05-24 22:52:35 -04:00
|
|
|
"sync"
|
2016-01-06 17:57:02 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/discovery"
|
|
|
|
)
|
|
|
|
|
2016-05-24 22:52:35 -04:00
|
|
|
// Discovery implements a discovery backend that keeps
|
2016-01-06 17:57:02 -05:00
|
|
|
// data in memory.
|
|
|
|
type Discovery struct {
|
|
|
|
heartbeat time.Duration
|
|
|
|
values []string
|
2016-05-24 22:52:35 -04:00
|
|
|
mu sync.Mutex
|
2016-01-06 17:57:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init registers the memory backend on demand.
|
|
|
|
func Init() {
|
|
|
|
discovery.Register("memory", &Discovery{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize sets the heartbeat for the memory backend.
|
|
|
|
func (s *Discovery) Initialize(_ string, heartbeat time.Duration, _ time.Duration, _ map[string]string) error {
|
|
|
|
s.heartbeat = heartbeat
|
2015-12-10 18:35:10 -05:00
|
|
|
s.values = make([]string, 0)
|
2016-01-06 17:57:02 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch sends periodic discovery updates to a channel.
|
|
|
|
func (s *Discovery) Watch(stopCh <-chan struct{}) (<-chan discovery.Entries, <-chan error) {
|
|
|
|
ch := make(chan discovery.Entries)
|
|
|
|
errCh := make(chan error)
|
|
|
|
ticker := time.NewTicker(s.heartbeat)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(errCh)
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
// Send the initial entries if available.
|
|
|
|
var currentEntries discovery.Entries
|
2016-05-24 22:52:35 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
s.mu.Lock()
|
2016-01-06 17:57:02 -05:00
|
|
|
if len(s.values) > 0 {
|
|
|
|
currentEntries, err = discovery.CreateEntries(s.values)
|
2016-05-24 22:52:35 -04:00
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
} else if currentEntries != nil {
|
|
|
|
ch <- currentEntries
|
2016-01-06 17:57:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Periodically send updates.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2016-05-24 22:52:35 -04:00
|
|
|
s.mu.Lock()
|
2016-01-06 17:57:02 -05:00
|
|
|
newEntries, err := discovery.CreateEntries(s.values)
|
2016-05-24 22:52:35 -04:00
|
|
|
s.mu.Unlock()
|
2016-01-06 17:57:02 -05:00
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file has really changed.
|
|
|
|
if !newEntries.Equals(currentEntries) {
|
|
|
|
ch <- newEntries
|
|
|
|
}
|
|
|
|
currentEntries = newEntries
|
|
|
|
case <-stopCh:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch, errCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register adds a new address to the discovery.
|
|
|
|
func (s *Discovery) Register(addr string) error {
|
2016-05-24 22:52:35 -04:00
|
|
|
s.mu.Lock()
|
2016-01-06 17:57:02 -05:00
|
|
|
s.values = append(s.values, addr)
|
2016-05-24 22:52:35 -04:00
|
|
|
s.mu.Unlock()
|
2016-01-06 17:57:02 -05:00
|
|
|
return nil
|
|
|
|
}
|