...

Source file src/github.com/go-kit/kit/sd/etcdv3/registrar.go

Documentation: github.com/go-kit/kit/sd/etcdv3

     1  package etcdv3
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/go-kit/log"
     8  )
     9  
    10  const minHeartBeatTime = 500 * time.Millisecond
    11  
    12  // Registrar registers service instance liveness information to etcd.
    13  type Registrar struct {
    14  	client  Client
    15  	service Service
    16  	logger  log.Logger
    17  
    18  	quitmtx sync.Mutex
    19  	quit    chan struct{}
    20  }
    21  
    22  // Service holds the instance identifying data you want to publish to etcd. Key
    23  // must be unique, and value is the string returned to subscribers, typically
    24  // called the "instance" string in other parts of package sd.
    25  type Service struct {
    26  	Key   string // unique key, e.g. "/service/foobar/1.2.3.4:8080"
    27  	Value string // returned to subscribers, e.g. "http://1.2.3.4:8080"
    28  	TTL   *TTLOption
    29  }
    30  
    31  // TTLOption allow setting a key with a TTL. This option will be used by a loop
    32  // goroutine which regularly refreshes the lease of the key.
    33  type TTLOption struct {
    34  	heartbeat time.Duration // e.g. time.Second * 3
    35  	ttl       time.Duration // e.g. time.Second * 10
    36  }
    37  
    38  // NewTTLOption returns a TTLOption that contains proper TTL settings. Heartbeat
    39  // is used to refresh the lease of the key periodically; its value should be at
    40  // least 500ms. TTL defines the lease of the key; its value should be
    41  // significantly greater than heartbeat.
    42  //
    43  // Good default values might be 3s heartbeat, 10s TTL.
    44  func NewTTLOption(heartbeat, ttl time.Duration) *TTLOption {
    45  	if heartbeat <= minHeartBeatTime {
    46  		heartbeat = minHeartBeatTime
    47  	}
    48  	if ttl <= heartbeat {
    49  		ttl = 3 * heartbeat
    50  	}
    51  	return &TTLOption{
    52  		heartbeat: heartbeat,
    53  		ttl:       ttl,
    54  	}
    55  }
    56  
    57  // NewRegistrar returns a etcd Registrar acting on the provided catalog
    58  // registration (service).
    59  func NewRegistrar(client Client, service Service, logger log.Logger) *Registrar {
    60  	return &Registrar{
    61  		client:  client,
    62  		service: service,
    63  		logger:  log.With(logger, "key", service.Key, "value", service.Value),
    64  	}
    65  }
    66  
    67  // Register implements the sd.Registrar interface. Call it when you want your
    68  // service to be registered in etcd, typically at startup.
    69  func (r *Registrar) Register() {
    70  	if err := r.client.Register(r.service); err != nil {
    71  		r.logger.Log("err", err)
    72  		return
    73  	}
    74  	if r.service.TTL != nil {
    75  		r.logger.Log("action", "register", "lease", r.client.LeaseID())
    76  	} else {
    77  		r.logger.Log("action", "register")
    78  	}
    79  }
    80  
    81  // Deregister implements the sd.Registrar interface. Call it when you want your
    82  // service to be deregistered from etcd, typically just prior to shutdown.
    83  func (r *Registrar) Deregister() {
    84  	if err := r.client.Deregister(r.service); err != nil {
    85  		r.logger.Log("err", err)
    86  	} else {
    87  		r.logger.Log("action", "deregister")
    88  	}
    89  
    90  	r.quitmtx.Lock()
    91  	defer r.quitmtx.Unlock()
    92  	if r.quit != nil {
    93  		close(r.quit)
    94  		r.quit = nil
    95  	}
    96  }
    97  

View as plain text