Package list golang-github-go-kit-kit / 0ad6191
Use io.Closer, allow nil Closers Peter Bourgon 6 years ago
7 changed file(s) with 35 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "errors"
44 "fmt"
5 "io"
56 "net/url"
67 "strings"
78 "time"
6364 }
6465
6566 func factory(ctx context.Context, qps int) loadbalancer.Factory {
66 return func(instance string) (endpoint.Endpoint, loadbalancer.Closer, error) {
67 return func(instance string) (endpoint.Endpoint, io.Closer, error) {
6768 var e endpoint.Endpoint
6869 e = makeUppercaseProxy(ctx, instance)
6970 e = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{}))(e)
7071 e = kitratelimit.NewTokenBucketLimiter(jujuratelimit.NewBucketWithRate(float64(qps), int64(qps)))(e)
71 return e, make(loadbalancer.Closer), nil
72 return e, nil, nil
7273 }
7374 }
7475
11
22 import (
33 "errors"
4 "io"
45 "net"
56 "sync/atomic"
67 "testing"
910 "golang.org/x/net/context"
1011
1112 "github.com/go-kit/kit/endpoint"
12 "github.com/go-kit/kit/loadbalancer"
1313 "github.com/go-kit/kit/log"
1414 )
1515
1818 name = "foo"
1919 ttl = time.Second
2020 e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
21 c = make(chan struct{})
22 factory = func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { return e, c, nil }
21 factory = func(string) (endpoint.Endpoint, io.Closer, error) { return e, nil, nil }
2322 logger = log.NewNopLogger()
2423 )
2524
4039 name = "some-name"
4140 ttl = time.Second
4241 e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
43 c = make(chan struct{})
44 factory = func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { return e, c, nil }
42 factory = func(string) (endpoint.Endpoint, io.Closer, error) { return e, nil, nil }
4543 logger = log.NewNopLogger()
4644 )
4745
6361 addrs = []*net.SRV{addr}
6462 name = "some-name"
6563 ttl = time.Second
66 factory = func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { return nil, nil, errors.New("kaboom") }
64 factory = func(string) (endpoint.Endpoint, io.Closer, error) { return nil, nil, errors.New("kaboom") }
6765 logger = log.NewNopLogger()
6866 )
6967
9694 addrs = []*net.SRV{addr}
9795 name = "my-name"
9896 ttl = time.Second
99 factory = func(string) (endpoint.Endpoint, loadbalancer.Closer, error) { return nil, nil, errors.New("kaboom") }
97 factory = func(string) (endpoint.Endpoint, io.Closer, error) { return nil, nil, errors.New("kaboom") }
10098 logger = log.NewNopLogger()
10199 )
102100
00 package loadbalancer
11
22 import (
3 "io"
34 "sync"
45
56 "github.com/go-kit/kit/endpoint"
3637
3738 type endpointCloser struct {
3839 endpoint.Endpoint
39 Closer
40 io.Closer
4041 }
4142
4243 // Replace replaces the current set of endpoints with endpoints manufactured
6768
6869 // Close any leftover endpoints.
6970 for _, ec := range t.m {
70 close(ec.Closer)
71 if ec.Closer != nil {
72 ec.Closer.Close()
73 }
7174 }
7275
7376 // Swap and GC.
00 package loadbalancer_test
11
22 import (
3 "io"
34 "testing"
45 "time"
56
1314 func TestEndpointCache(t *testing.T) {
1415 var (
1516 e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
16 ca = make(loadbalancer.Closer)
17 cb = make(loadbalancer.Closer)
18 c = map[string]loadbalancer.Closer{"a": ca, "b": cb}
19 f = func(s string) (endpoint.Endpoint, loadbalancer.Closer, error) { return e, c[s], nil }
17 ca = make(closer)
18 cb = make(closer)
19 c = map[string]io.Closer{"a": ca, "b": cb}
20 f = func(s string) (endpoint.Endpoint, io.Closer, error) { return e, c[s], nil }
2021 ec = loadbalancer.NewEndpointCache(f, log.NewNopLogger())
2122 )
2223
6364 t.Errorf("didn't close the deleted instance in time")
6465 }
6566 }
67
68 type closer chan struct{}
69
70 func (c closer) Close() error { close(c); return nil }
11
22 import (
33 "errors"
4 "io"
45 "testing"
56
67 stdetcd "github.com/coreos/go-etcd/etcd"
78 "golang.org/x/net/context"
89
910 "github.com/go-kit/kit/endpoint"
10 "github.com/go-kit/kit/loadbalancer"
1111 kitetcd "github.com/go-kit/kit/loadbalancer/etcd"
1212 "github.com/go-kit/kit/log"
1313 )
3131 e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
3232 )
3333
34 factory := func(string) (endpoint.Endpoint, loadbalancer.Closer, error) {
35 return e, make(loadbalancer.Closer), nil
34 factory := func(string) (endpoint.Endpoint, io.Closer, error) {
35 return e, nil, nil
3636 }
3737
3838 client := &fakeClient{
5353 func TestBadFactory(t *testing.T) {
5454 logger := log.NewNopLogger()
5555
56 factory := func(string) (endpoint.Endpoint, loadbalancer.Closer, error) {
56 factory := func(string) (endpoint.Endpoint, io.Closer, error) {
5757 return nil, nil, errors.New("kaboom")
5858 }
5959
00 package loadbalancer
11
2 import "github.com/go-kit/kit/endpoint"
2 import (
3 "io"
4
5 "github.com/go-kit/kit/endpoint"
6 )
37
48 // Factory is a function that converts an instance string, e.g. a host:port,
59 // to a usable endpoint. Factories are used by load balancers to convert
711 // endpoints. Users are expected to provide their own factory functions that
812 // assume specific transports, or can deduce transports by parsing the
913 // instance string.
10 type Factory func(instance string) (endpoint.Endpoint, Closer, error)
11
12 // Closer is returned by factory functions as a way to close a generated
13 // endpoint.
14 type Closer chan struct{}
14 type Factory func(instance string) (endpoint.Endpoint, io.Closer, error)
11
22 import (
33 "fmt"
4 "io"
45 "testing"
56
67 "golang.org/x/net/context"
78
89 "github.com/go-kit/kit/endpoint"
9 "github.com/go-kit/kit/loadbalancer"
1010 "github.com/go-kit/kit/loadbalancer/static"
1111 "github.com/go-kit/kit/log"
1212 )
1919 "bar": func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil },
2020 "baz": func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil },
2121 }
22 factory = func(instance string) (endpoint.Endpoint, loadbalancer.Closer, error) {
22 factory = func(instance string) (endpoint.Endpoint, io.Closer, error) {
2323 if e, ok := endpoints[instance]; ok {
24 return e, make(loadbalancer.Closer), nil
24 return e, nil, nil
2525 }
2626 return nil, nil, fmt.Errorf("%s: not found", instance)
2727 }