Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/dnstap/golang-dnstap v0.0.0-20170829151710-2cf77a2b5e11
github.com/golang/protobuf v1.3.2
github.com/google/uuid v1.1.1
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/infobloxopen/go-trees v0.0.0-20190313150506-2af4e13f9062
github.com/miekg/dns v1.1.15
Expand Down
44 changes: 15 additions & 29 deletions go.sum

Large diffs are not rendered by default.

38 changes: 24 additions & 14 deletions pep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

ot "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
)

var (
Expand Down Expand Up @@ -83,6 +84,14 @@ type Option func(*options)

const virtualServerAddress = "pdp"

// WithClientUnaryInterceptors returns an Options which appends to
// the client unary interceptors of the underlying connection to pdp
func WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) Option {
return func(o *options) {
o.clientUnaryInterceptors = append([]grpc.UnaryClientInterceptor{}, interceptors...)
}
}

// WithRoundRobinBalancer returns an Option which sets round-robin balancer with given set of servers.
func WithRoundRobinBalancer(addresses ...string) Option {
return func(o *options) {
Expand Down Expand Up @@ -209,20 +218,21 @@ const (
)

type options struct {
addresses []string
balancer int
tracer ot.Tracer
maxStreams int
ctx context.Context
connTimeout time.Duration
connStateCb ConnectionStateNotificationCallback
autoRequestSize bool
maxRequestSize uint32
noPool bool
cache bool
cacheTTL time.Duration
cacheMaxSize int
onCacheHitHandler OnCacheHitHandler
addresses []string
balancer int
tracer ot.Tracer
maxStreams int
ctx context.Context
connTimeout time.Duration
connStateCb ConnectionStateNotificationCallback
autoRequestSize bool
maxRequestSize uint32
noPool bool
cache bool
cacheTTL time.Duration
cacheMaxSize int
onCacheHitHandler OnCacheHitHandler
clientUnaryInterceptors []grpc.UnaryClientInterceptor
}

// NewClient creates client instance using given options.
Expand Down
18 changes: 18 additions & 0 deletions pep/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

ot "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
)

func TestNewClient(t *testing.T) {
Expand Down Expand Up @@ -59,6 +60,23 @@ func TestNewClientWithTracer(t *testing.T) {
}
}

var noOpClientInterceptor grpc.UnaryClientInterceptor = func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return nil
}

func TestNewClientWithInterceptor(t *testing.T) {
ci := noOpClientInterceptor
c := NewClient(WithClientUnaryInterceptors(ci))
uc, ok := c.(*unaryClient)
if !ok {
t.Fatalf("Expected *unaryClient from NewClient got %#v", c)
}

if len(uc.opts.clientUnaryInterceptors) == 0 {
t.Errorf("Expected noOpClientInterceptor as client option but got %v", uc.opts.clientUnaryInterceptors)
}
}

func TestNewClientWithAutoRequestSize(t *testing.T) {
c := NewClient(WithAutoRequestSize(true))
uc, ok := c.(*unaryClient)
Expand Down
29 changes: 18 additions & 11 deletions pep/unary_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/allegro/bigcache"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
ot "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
Expand Down Expand Up @@ -64,20 +65,26 @@ func (c *unaryClient) Connect(addr string) error {
}
}

var interceptors []grpc.UnaryClientInterceptor

if c.opts.tracer != nil {
opts = append(opts,
grpc.WithUnaryInterceptor(
otgrpc.OpenTracingClientInterceptor(
c.opts.tracer,
otgrpc.IncludingSpans(
func(parentSpanCtx ot.SpanContext, method string, req, resp interface{}) bool {
return parentSpanCtx != nil
},
),
interceptors = append(interceptors,
otgrpc.OpenTracingClientInterceptor(
c.opts.tracer,
otgrpc.IncludingSpans(
func(parentSpanCtx ot.SpanContext, method string, req, resp interface{}) bool {
return parentSpanCtx != nil
},
),
),
)
))
}

if c.opts.clientUnaryInterceptors != nil {
interceptors = append(interceptors, c.opts.clientUnaryInterceptors...)
}
opts = append(opts, grpc.WithUnaryInterceptor(
grpc_middleware.ChainUnaryClient(interceptors...),
))

cache, err := newCacheFromOptions(c.opts)
if err != nil {
Expand Down
202 changes: 202 additions & 0 deletions vendor/github.com/grpc-ecosystem/go-grpc-middleware/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/grpc-ecosystem/go-grpc-middleware/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading