Moved to google.golang.org/genproto/googleapis/api/annotations
Fixes #52
This commit is contained in:
29
vendor/github.com/go-kit/kit/sd/cache/benchmark_test.go
generated
vendored
Normal file
29
vendor/github.com/go-kit/kit/sd/cache/benchmark_test.go
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
)
|
||||
|
||||
func BenchmarkEndpoints(b *testing.B) {
|
||||
var (
|
||||
ca = make(closer)
|
||||
cb = make(closer)
|
||||
cmap = map[string]io.Closer{"a": ca, "b": cb}
|
||||
factory = func(instance string) (endpoint.Endpoint, io.Closer, error) { return endpoint.Nop, cmap[instance], nil }
|
||||
c = New(factory, log.NewNopLogger())
|
||||
)
|
||||
|
||||
b.ReportAllocs()
|
||||
|
||||
c.Update([]string{"a", "b"})
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
c.Endpoints()
|
||||
}
|
||||
})
|
||||
}
|
96
vendor/github.com/go-kit/kit/sd/cache/cache.go
generated
vendored
Normal file
96
vendor/github.com/go-kit/kit/sd/cache/cache.go
generated
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sort"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/sd"
|
||||
)
|
||||
|
||||
// Cache collects the most recent set of endpoints from a service discovery
|
||||
// system via a subscriber, and makes them available to consumers. Cache is
|
||||
// meant to be embedded inside of a concrete subscriber, and can serve Service
|
||||
// invocations directly.
|
||||
type Cache struct {
|
||||
mtx sync.RWMutex
|
||||
factory sd.Factory
|
||||
cache map[string]endpointCloser
|
||||
slice atomic.Value // []endpoint.Endpoint
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
type endpointCloser struct {
|
||||
endpoint.Endpoint
|
||||
io.Closer
|
||||
}
|
||||
|
||||
// New returns a new, empty endpoint cache.
|
||||
func New(factory sd.Factory, logger log.Logger) *Cache {
|
||||
return &Cache{
|
||||
factory: factory,
|
||||
cache: map[string]endpointCloser{},
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Update should be invoked by clients with a complete set of current instance
|
||||
// strings whenever that set changes. The cache manufactures new endpoints via
|
||||
// the factory, closes old endpoints when they disappear, and persists existing
|
||||
// endpoints if they survive through an update.
|
||||
func (c *Cache) Update(instances []string) {
|
||||
c.mtx.Lock()
|
||||
defer c.mtx.Unlock()
|
||||
|
||||
// Deterministic order (for later).
|
||||
sort.Strings(instances)
|
||||
|
||||
// Produce the current set of services.
|
||||
cache := make(map[string]endpointCloser, len(instances))
|
||||
for _, instance := range instances {
|
||||
// If it already exists, just copy it over.
|
||||
if sc, ok := c.cache[instance]; ok {
|
||||
cache[instance] = sc
|
||||
delete(c.cache, instance)
|
||||
continue
|
||||
}
|
||||
|
||||
// If it doesn't exist, create it.
|
||||
service, closer, err := c.factory(instance)
|
||||
if err != nil {
|
||||
c.logger.Log("instance", instance, "err", err)
|
||||
continue
|
||||
}
|
||||
cache[instance] = endpointCloser{service, closer}
|
||||
}
|
||||
|
||||
// Close any leftover endpoints.
|
||||
for _, sc := range c.cache {
|
||||
if sc.Closer != nil {
|
||||
sc.Closer.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Populate the slice of endpoints.
|
||||
slice := make([]endpoint.Endpoint, 0, len(cache))
|
||||
for _, instance := range instances {
|
||||
// A bad factory may mean an instance is not present.
|
||||
if _, ok := cache[instance]; !ok {
|
||||
continue
|
||||
}
|
||||
slice = append(slice, cache[instance].Endpoint)
|
||||
}
|
||||
|
||||
// Swap and trigger GC for old copies.
|
||||
c.slice.Store(slice)
|
||||
c.cache = cache
|
||||
}
|
||||
|
||||
// Endpoints yields the current set of (presumably identical) endpoints, ordered
|
||||
// lexicographically by the corresponding instance string.
|
||||
func (c *Cache) Endpoints() []endpoint.Endpoint {
|
||||
return c.slice.Load().([]endpoint.Endpoint)
|
||||
}
|
91
vendor/github.com/go-kit/kit/sd/cache/cache_test.go
generated
vendored
Normal file
91
vendor/github.com/go-kit/kit/sd/cache/cache_test.go
generated
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
)
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
var (
|
||||
ca = make(closer)
|
||||
cb = make(closer)
|
||||
c = map[string]io.Closer{"a": ca, "b": cb}
|
||||
f = func(instance string) (endpoint.Endpoint, io.Closer, error) { return endpoint.Nop, c[instance], nil }
|
||||
cache = New(f, log.NewNopLogger())
|
||||
)
|
||||
|
||||
// Populate
|
||||
cache.Update([]string{"a", "b"})
|
||||
select {
|
||||
case <-ca:
|
||||
t.Errorf("endpoint a closed, not good")
|
||||
case <-cb:
|
||||
t.Errorf("endpoint b closed, not good")
|
||||
case <-time.After(time.Millisecond):
|
||||
t.Logf("no closures yet, good")
|
||||
}
|
||||
if want, have := 2, len(cache.Endpoints()); want != have {
|
||||
t.Errorf("want %d, have %d", want, have)
|
||||
}
|
||||
|
||||
// Duplicate, should be no-op
|
||||
cache.Update([]string{"a", "b"})
|
||||
select {
|
||||
case <-ca:
|
||||
t.Errorf("endpoint a closed, not good")
|
||||
case <-cb:
|
||||
t.Errorf("endpoint b closed, not good")
|
||||
case <-time.After(time.Millisecond):
|
||||
t.Logf("no closures yet, good")
|
||||
}
|
||||
if want, have := 2, len(cache.Endpoints()); want != have {
|
||||
t.Errorf("want %d, have %d", want, have)
|
||||
}
|
||||
|
||||
// Delete b
|
||||
go cache.Update([]string{"a"})
|
||||
select {
|
||||
case <-ca:
|
||||
t.Errorf("endpoint a closed, not good")
|
||||
case <-cb:
|
||||
t.Logf("endpoint b closed, good")
|
||||
case <-time.After(time.Second):
|
||||
t.Errorf("didn't close the deleted instance in time")
|
||||
}
|
||||
if want, have := 1, len(cache.Endpoints()); want != have {
|
||||
t.Errorf("want %d, have %d", want, have)
|
||||
}
|
||||
|
||||
// Delete a
|
||||
go cache.Update([]string{})
|
||||
select {
|
||||
// case <-cb: will succeed, as it's closed
|
||||
case <-ca:
|
||||
t.Logf("endpoint a closed, good")
|
||||
case <-time.After(time.Second):
|
||||
t.Errorf("didn't close the deleted instance in time")
|
||||
}
|
||||
if want, have := 0, len(cache.Endpoints()); want != have {
|
||||
t.Errorf("want %d, have %d", want, have)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadFactory(t *testing.T) {
|
||||
cache := New(func(string) (endpoint.Endpoint, io.Closer, error) {
|
||||
return nil, nil, errors.New("bad factory")
|
||||
}, log.NewNopLogger())
|
||||
|
||||
cache.Update([]string{"foo:1234", "bar:5678"})
|
||||
if want, have := 0, len(cache.Endpoints()); want != have {
|
||||
t.Errorf("want %d, have %d", want, have)
|
||||
}
|
||||
}
|
||||
|
||||
type closer chan struct{}
|
||||
|
||||
func (c closer) Close() error { close(c); return nil }
|
Reference in New Issue
Block a user