github.com/psychoss/docker@v1.9.0/daemon/discovery.go (about)

     1  package daemon
     2  
     3  import (
     4  	"time"
     5  
     6  	log "github.com/Sirupsen/logrus"
     7  	"github.com/docker/docker/pkg/discovery"
     8  
     9  	// Register the libkv backends for discovery.
    10  	_ "github.com/docker/docker/pkg/discovery/kv"
    11  )
    12  
    13  const (
    14  	// defaultDiscoveryHeartbeat is the default value for discovery heartbeat interval.
    15  	defaultDiscoveryHeartbeat = 20 * time.Second
    16  
    17  	// defaultDiscoveryTTL is the default TTL interface for discovery.
    18  	defaultDiscoveryTTL = 60 * time.Second
    19  )
    20  
    21  // initDiscovery initialized the nodes discovery subsystem by connecting to the specified backend
    22  // and start a registration loop to advertise the current node under the specified address.
    23  func initDiscovery(backend, address string, clusterOpts map[string]string) (discovery.Backend, error) {
    24  	var (
    25  		discoveryBackend discovery.Backend
    26  		err              error
    27  	)
    28  	if discoveryBackend, err = discovery.New(backend, defaultDiscoveryHeartbeat, defaultDiscoveryTTL, clusterOpts); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	// We call Register() on the discovery backend in a loop for the whole lifetime of the daemon,
    33  	// but we never actually Watch() for nodes appearing and disappearing for the moment.
    34  	go registrationLoop(discoveryBackend, address)
    35  	return discoveryBackend, nil
    36  }
    37  
    38  // registrationLoop registers the current node against the discovery backend using the specified
    39  // address. The function never returns, as registration against the backend comes with a TTL and
    40  // requires regular heartbeats.
    41  func registrationLoop(discoveryBackend discovery.Backend, address string) {
    42  	for {
    43  		if err := discoveryBackend.Register(address); err != nil {
    44  			log.Errorf("Registering as %q in discovery failed: %v", address, err)
    45  		}
    46  		time.Sleep(defaultDiscoveryHeartbeat)
    47  	}
    48  }