// New returns an AddService backed by a gRPC client connection. It is the // responsibility of the caller to dial, and later close, the connection. func New(conn *grpc.ClientConn, tracer stdopentracing.Tracer, logger log.Logger) addsvc.Service { // We construct a single ratelimiter middleware, to limit the total outgoing // QPS from this client to all methods on the remote instance. We also // construct per-endpoint circuitbreaker middlewares to demonstrate how // that's done, although they could easily be combined into a single breaker // for the entire remote instance, too. limiter := ratelimit.NewTokenBucketLimiter(jujuratelimit.NewBucketWithRate(100, 100)) var sumEndpoint endpoint.Endpoint { sumEndpoint = grpctransport.NewClient( conn, "Add", "Sum", addsvc.EncodeGRPCSumRequest, addsvc.DecodeGRPCSumResponse, pb.SumReply{}, grpctransport.SetClientBefore(opentracing.FromGRPCRequest(tracer, "Sum", logger)), ).Endpoint() sumEndpoint = opentracing.TraceClient(tracer, "Sum")(sumEndpoint) sumEndpoint = limiter(sumEndpoint) sumEndpoint = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{ Name: "Sum", Timeout: 30 * time.Second, }))(sumEndpoint) } var concatEndpoint endpoint.Endpoint { concatEndpoint = grpctransport.NewClient( conn, "Add", "Concat", addsvc.EncodeGRPCConcatRequest, addsvc.DecodeGRPCConcatResponse, pb.ConcatReply{}, grpctransport.SetClientBefore(opentracing.FromGRPCRequest(tracer, "Concat", logger)), ).Endpoint() concatEndpoint = opentracing.TraceClient(tracer, "Concat")(concatEndpoint) concatEndpoint = limiter(concatEndpoint) sumEndpoint = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{ Name: "Concat", Timeout: 30 * time.Second, }))(sumEndpoint) } return addsvc.Endpoints{ SumEndpoint: sumEndpoint, ConcatEndpoint: concatEndpoint, } }
// MakeConcatEndpointFactory returns a loadbalancer.Factory that transforms // GRPC host:port strings into Endpoints that call the Concat method on a GRPC // server at that address. func MakeConcatEndpointFactory(tracer opentracing.Tracer, tracingLogger log.Logger) loadbalancer.Factory { return func(instance string) (endpoint.Endpoint, io.Closer, error) { cc, err := grpc.Dial(instance, grpc.WithInsecure()) return grpctransport.NewClient( cc, "Add", "Concat", encodeConcatRequest, decodeConcatResponse, pb.ConcatReply{}, grpctransport.SetClientBefore(kitot.ToGRPCRequest(tracer, tracingLogger)), ).Endpoint(), cc, err } }