This commit is contained in:
Manfred Touron
2017-05-18 18:54:23 +02:00
parent dc386661ca
commit 5448f25fd6
645 changed files with 55908 additions and 33297 deletions

View File

@@ -1,7 +1,6 @@
package etcd
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
@@ -11,6 +10,7 @@ import (
"time"
etcd "github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
var (
@@ -156,15 +156,7 @@ func (c *client) Register(s Service) error {
if s.Value == "" {
return ErrNoValue
}
var err error
if s.TTL != nil {
_, err = c.keysAPI.Set(c.ctx, s.Key, s.Value, &etcd.SetOptions{
PrevExist: etcd.PrevIgnore,
TTL: s.TTL.ttl,
})
} else {
_, err = c.keysAPI.Create(c.ctx, s.Key, s.Value)
}
_, err := c.keysAPI.Create(c.ctx, s.Key, s.Value)
return err
}

View File

@@ -6,9 +6,8 @@ import (
"testing"
"time"
"golang.org/x/net/context"
etcd "github.com/coreos/etcd/client"
"golang.org/x/net/context"
)
func TestNewClient(t *testing.T) {

View File

@@ -1,10 +1,11 @@
package etcd
import (
"context"
"io"
"time"
"golang.org/x/net/context"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/sd/lb"

View File

@@ -3,12 +3,13 @@
package etcd
import (
"context"
"io"
"os"
"testing"
"time"
"golang.org/x/net/context"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
)
@@ -49,7 +50,7 @@ func TestIntegration(t *testing.T) {
registrar := NewRegistrar(client, Service{
Key: key,
Value: value,
}, log.With(log.NewLogfmtLogger(os.Stderr), "component", "registrar"))
}, log.NewContext(log.NewLogfmtLogger(os.Stderr)).With("component", "registrar"))
// Register our instance.
registrar.Register()
@@ -71,7 +72,7 @@ func TestIntegration(t *testing.T) {
client,
prefix,
func(string) (endpoint.Endpoint, io.Closer, error) { return endpoint.Nop, nil, nil },
log.With(log.NewLogfmtLogger(os.Stderr), "component", "subscriber"),
log.NewContext(log.NewLogfmtLogger(os.Stderr)).With("component", "subscriber"),
)
if err != nil {
t.Fatalf("NewSubscriber: %v", err)

View File

@@ -1,24 +1,16 @@
package etcd
import (
"sync"
"time"
etcd "github.com/coreos/etcd/client"
"github.com/go-kit/kit/log"
)
const minHeartBeatTime = 500 * time.Millisecond
// Registrar registers service instance liveness information to etcd.
type Registrar struct {
client Client
service Service
logger log.Logger
quitmtx sync.Mutex
quit chan struct{}
}
// Service holds the instance identifying data you want to publish to etcd. Key
@@ -27,43 +19,19 @@ type Registrar struct {
type Service struct {
Key string // unique key, e.g. "/service/foobar/1.2.3.4:8080"
Value string // returned to subscribers, e.g. "http://1.2.3.4:8080"
TTL *TTLOption
DeleteOptions *etcd.DeleteOptions
}
// TTLOption allow setting a key with a TTL. This option will be used by a loop
// goroutine which regularly refreshes the lease of the key.
type TTLOption struct {
heartbeat time.Duration // e.g. time.Second * 3
ttl time.Duration // e.g. time.Second * 10
}
// NewTTLOption returns a TTLOption that contains proper TTL settings. Heartbeat
// is used to refresh the lease of the key periodically; its value should be at
// least 500ms. TTL defines the lease of the key; its value should be
// significantly greater than heartbeat.
//
// Good default values might be 3s heartbeat, 10s TTL.
func NewTTLOption(heartbeat, ttl time.Duration) *TTLOption {
if heartbeat <= minHeartBeatTime {
heartbeat = minHeartBeatTime
}
if ttl <= heartbeat {
ttl = 3 * heartbeat
}
return &TTLOption{
heartbeat: heartbeat,
ttl: ttl,
}
}
// NewRegistrar returns a etcd Registrar acting on the provided catalog
// registration (service).
func NewRegistrar(client Client, service Service, logger log.Logger) *Registrar {
return &Registrar{
client: client,
service: service,
logger: log.With(logger, "key", service.Key, "value", service.Value),
logger: log.NewContext(logger).With(
"key", service.Key,
"value", service.Value,
),
}
}
@@ -75,31 +43,6 @@ func (r *Registrar) Register() {
} else {
r.logger.Log("action", "register")
}
if r.service.TTL != nil {
go r.loop()
}
}
func (r *Registrar) loop() {
r.quitmtx.Lock()
if r.quit != nil {
return // already running
}
r.quit = make(chan struct{})
r.quitmtx.Unlock()
tick := time.NewTicker(r.service.TTL.heartbeat)
defer tick.Stop()
for {
select {
case <-tick.C:
if err := r.client.Register(r.service); err != nil {
r.logger.Log("err", err)
}
case <-r.quit:
return
}
}
}
// Deregister implements the sd.Registrar interface. Call it when you want your
@@ -110,11 +53,4 @@ func (r *Registrar) Deregister() {
} else {
r.logger.Log("action", "deregister")
}
r.quitmtx.Lock()
defer r.quitmtx.Unlock()
if r.quit != nil {
close(r.quit)
r.quit = nil
}
}

View File

@@ -30,7 +30,7 @@ func (tc *testClient) Deregister(s Service) error {
}
// default service used to build registrar in our tests
var testService = Service{"testKey", "testValue", nil, nil}
var testService = Service{"testKey", "testValue", nil}
// NewRegistar should return a registar with a logger using the service key and value
func TestNewRegistar(t *testing.T) {