github.com/darciopacifico/docker@v1.9.0-rc1/pkg/discovery/backends.go (about) 1 package discovery 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 log "github.com/Sirupsen/logrus" 9 ) 10 11 var ( 12 // Backends is a global map of discovery backends indexed by their 13 // associated scheme. 14 backends map[string]Backend 15 ) 16 17 func init() { 18 backends = make(map[string]Backend) 19 } 20 21 // Register makes a discovery backend available by the provided scheme. 22 // If Register is called twice with the same scheme an error is returned. 23 func Register(scheme string, d Backend) error { 24 if _, exists := backends[scheme]; exists { 25 return fmt.Errorf("scheme already registered %s", scheme) 26 } 27 log.WithField("name", scheme).Debug("Registering discovery service") 28 backends[scheme] = d 29 return nil 30 } 31 32 func parse(rawurl string) (string, string) { 33 parts := strings.SplitN(rawurl, "://", 2) 34 35 // nodes:port,node2:port => nodes://node1:port,node2:port 36 if len(parts) == 1 { 37 return "nodes", parts[0] 38 } 39 return parts[0], parts[1] 40 } 41 42 // New returns a new Discovery given a URL, heartbeat and ttl settings. 43 // Returns an error if the URL scheme is not supported. 44 func New(rawurl string, heartbeat time.Duration, ttl time.Duration, clusterOpts map[string]string) (Backend, error) { 45 scheme, uri := parse(rawurl) 46 if backend, exists := backends[scheme]; exists { 47 log.WithFields(log.Fields{"name": scheme, "uri": uri}).Debug("Initializing discovery service") 48 err := backend.Initialize(uri, heartbeat, ttl, clusterOpts) 49 return backend, err 50 } 51 52 return nil, ErrNotSupported 53 }