// New returns an AddService that's backed by the URL. baseurl will have its // scheme and hostport used, but its path will be overwritten. If client is // nil, http.DefaultClient will be used. func New(ctx context.Context, baseurl *url.URL, logger log.Logger, c *http.Client) server.AddService { sumURL, err := url.Parse(baseurl.String()) if err != nil { panic(err) } concatURL, err := url.Parse(baseurl.String()) if err != nil { panic(err) } sumURL.Path = "/sum" concatURL.Path = "/concat" return client{ Context: ctx, Logger: logger, sum: httptransport.NewClient( "GET", sumURL, server.EncodeSumRequest, server.DecodeSumResponse, ).Endpoint(), concat: httptransport.NewClient( "GET", concatURL, server.EncodeConcatRequest, server.DecodeConcatResponse, ).Endpoint(), } }
// MakeClientEndpoints returns an Endpoints struct where each endpoint invokes // the corresponding method on the remote instance, via a transport/http.Client. // Useful in a restsvc client. func MakeClientEndpoints(instance string) (Endpoints, error) { if !strings.HasPrefix(instance, "http") { instance = "http://" + instance } tgt, err := url.Parse(instance) if err != nil { return Endpoints{}, err } tgt.Path = "" options := []httptransport.ClientOption{} // Note that the request encoders need to modify the request URL, changing // the path and method. That's fine: we simply need to provide specific // encoders for each endpoint. return Endpoints{ PostConfigEndpoint: httptransport.NewClient("POST", tgt, encodePostConfigRequest, decodePostConfigResponse, options...).Endpoint(), GetConfigEndpoint: httptransport.NewClient("GET", tgt, encodeGetConfigRequest, decodeGetConfigResponse, options...).Endpoint(), PutConfigEndpoint: httptransport.NewClient("PUT", tgt, encodePutConfigRequest, decodePutConfigResponse, options...).Endpoint(), PatchConfigEndpoint: httptransport.NewClient("PATCH", tgt, encodePatchConfigRequest, decodePatchConfigResponse, options...).Endpoint(), DeleteConfigEndpoint: httptransport.NewClient("DELETE", tgt, encodeDeleteConfigRequest, decodeDeleteConfigResponse, options...).Endpoint(), GetChannelsEndpoint: httptransport.NewClient("GET", tgt, encodeGetChannelsRequest, decodeGetChannelsResponse, options...).Endpoint(), GetChannelEndpoint: httptransport.NewClient("GET", tgt, encodeGetChannelRequest, decodeGetChannelResponse, options...).Endpoint(), PostChannelEndpoint: httptransport.NewClient("POST", tgt, encodePostChannelRequest, decodePostChannelResponse, options...).Endpoint(), DeleteChannelEndpoint: httptransport.NewClient("DELETE", tgt, encodeDeleteChannelRequest, decodeDeleteChannelResponse, options...).Endpoint(), GetNotesEndpoint: httptransport.NewClient("GET", tgt, encodeGetNotesRequest, decodeGetNotesResponse, options...).Endpoint(), }, nil }
// New returns an AddService backed by an HTTP server living at the remote // instance. We expect instance to come from a service discovery system, so // likely of the form "host:port". func New(instance string, tracer stdopentracing.Tracer, logger log.Logger) (addsvc.Service, error) { if !strings.HasPrefix(instance, "http") { instance = "http://" + instance } u, err := url.Parse(instance) if err != nil { return nil, err } // 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 = httptransport.NewClient( "POST", copyURL(u, "/sum"), addsvc.EncodeHTTPGenericRequest, addsvc.DecodeHTTPSumResponse, httptransport.SetClientBefore(opentracing.FromHTTPRequest(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 = httptransport.NewClient( "POST", copyURL(u, "/concat"), addsvc.EncodeHTTPGenericRequest, addsvc.DecodeHTTPConcatResponse, httptransport.SetClientBefore(opentracing.FromHTTPRequest(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, }, nil }
func TestClientEndpointEncodeError(t *testing.T) { var ( sampleErr = errors.New("Oh no, an error") enc = func(r *http.Request, request interface{}) error { return sampleErr } dec = func(r *http.Response) (response interface{}, err error) { return nil, nil } ) u := &url.URL{ Scheme: "https", Host: "localhost", Path: "/does/not/matter", } c := httptransport.NewClient( "GET", u, enc, dec, ) _, err := c.Endpoint()(context.Background(), nil) if err == nil { t.Fatal("err == nil") } e, ok := err.(httptransport.TransportError) if !ok { t.Fatal("err is not of type github.com/go-kit/kit/transport/http.Err") } if want, have := sampleErr, e.Err; want != have { t.Fatalf("want %v, have %v", want, have) } }
func stringsvcFactory(ctx context.Context, method, path string) sd.Factory { return func(instance string) (endpoint.Endpoint, io.Closer, error) { if !strings.HasPrefix(instance, "http") { instance = "http://" + instance } tgt, err := url.Parse(instance) if err != nil { return nil, nil, err } tgt.Path = path // Since stringsvc doesn't have any kind of package we can import, or // any formal spec, we are forced to just assert where the endpoints // live, and write our own code to encode and decode requests and // responses. Ideally, if you write the service, you will want to // provide stronger guarantees to your clients. var ( enc httptransport.EncodeRequestFunc dec httptransport.DecodeResponseFunc ) switch path { case "/uppercase": enc, dec = encodeJSONRequest, decodeUppercaseResponse case "/count": enc, dec = encodeJSONRequest, decodeCountResponse default: return nil, nil, fmt.Errorf("unknown stringsvc path %q", path) } return httptransport.NewClient(method, tgt, enc, dec).Endpoint(), nil, nil } }
func newSetVerbosityEndpoint(URL url.URL, path string) endpoint.Endpoint { URL.Path = path URL.RawPath = path newEndpoint := httptransport.NewClient( "POST", &URL, setVerbosityEncoder, setVerbosityDecoder, ).Endpoint() return newEndpoint }
func newResetLevelsEndpoint(URL url.URL, path string) endpoint.Endpoint { URL.Path = path URL.RawPath = path newEndpoint := httptransport.NewClient( "POST", &URL, resetLevelsEncoder, resetLevelsDecoder, ).Endpoint() return newEndpoint }
func makeFetchRoutesEndpoint(ctx context.Context, instance string) endpoint.Endpoint { u, err := url.Parse(instance) if err != nil { panic(err) } if u.Path == "" { u.Path = "/paths" } return kithttp.NewClient( "GET", u, encodeFetchRoutesRequest, decodeFetchRoutesResponse, ).Endpoint() }
func httpFactory(ctx context.Context, method, path string) loadbalancer.Factory { return func(instance string) (endpoint.Endpoint, io.Closer, error) { var e endpoint.Endpoint if !strings.HasPrefix(instance, "http") { instance = "http://" + instance } u, err := url.Parse(instance) if err != nil { return nil, nil, err } u.Path = path e = httptransport.NewClient(method, u, passEncode, passDecode).Endpoint() return e, nil, nil } }
// SumEndpointFactory transforms a http url into an Endpoint. // The path of the url is reset to /sum. func SumEndpointFactory(instance string) (endpoint.Endpoint, io.Closer, error) { sumURL, err := url.Parse(instance) if err != nil { return nil, nil, err } sumURL.Path = "/sum" client := httptransport.NewClient( "GET", sumURL, server.EncodeSumRequest, server.DecodeSumResponse, httptransport.SetClient(nil), ) return client.Endpoint(), nil, nil }
// ConcatEndpointFactory transforms a http url into an Endpoint. // The path of the url is reset to /concat. func ConcatEndpointFactory(instance string) (endpoint.Endpoint, io.Closer, error) { concatURL, err := url.Parse(instance) if err != nil { return nil, nil, err } concatURL.Path = "/concat" client := httptransport.NewClient( "GET", concatURL, server.EncodeConcatRequest, server.DecodeConcatResponse, httptransport.SetClient(nil), ) return client.Endpoint(), nil, nil }
func makeUppercaseProxy(ctx context.Context, instance string) endpoint.Endpoint { if !strings.HasPrefix(instance, "http") { instance = "http://" + instance } u, err := url.Parse(instance) if err != nil { panic(err) } if u.Path == "" { u.Path = "/uppercase" } return httptransport.NewClient( "GET", u, encodeRequest, decodeUppercaseResponse, ).Endpoint() }
func TestHTTPClientBufferedStream(t *testing.T) { var ( testbody = "testbody" encode = func(context.Context, *http.Request, interface{}) error { return nil } decode = func(_ context.Context, r *http.Response) (interface{}, error) { return TestResponse{r.Body, ""}, nil } ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte(testbody)) })) client := httptransport.NewClient( "GET", mustParse(server.URL), encode, decode, httptransport.SetBufferedStream(true), ) res, err := client.Endpoint()(context.Background(), struct{}{}) if err != nil { t.Fatal(err) } // Check that the response was successfully decoded response, ok := res.(TestResponse) if !ok { t.Fatal("response should be TestResponse") } // Check that response body was NOT closed b := make([]byte, len(testbody)) _, err = response.Body.Read(b) if want, have := io.EOF, err; have != want { t.Fatalf("want %q, have %q", want, have) } if want, have := testbody, string(b); want != have { t.Errorf("want %q, have %q", want, have) } }
// MakeConcatEndpointFactory generates a Factory that transforms an http url // into an Endpoint. // // The path of the url is reset to /concat. func MakeConcatEndpointFactory(tracer opentracing.Tracer, tracingLogger log.Logger) loadbalancer.Factory { return func(instance string) (endpoint.Endpoint, io.Closer, error) { concatURL, err := url.Parse(instance) if err != nil { return nil, nil, err } concatURL.Path = "/concat" client := httptransport.NewClient( "GET", concatURL, server.EncodeConcatRequest, server.DecodeConcatResponse, httptransport.SetClient(nil), httptransport.SetClientBefore(kitot.ToHTTPRequest(tracer, tracingLogger)), ) return client.Endpoint(), nil, nil } }
func TestHTTPClient(t *testing.T) { var ( encode = func(*http.Request, interface{}) error { return nil } decode = func(*http.Response) (interface{}, error) { return struct{}{}, nil } headers = make(chan string, 1) headerKey = "X-Foo" headerVal = "abcde" ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { headers <- r.Header.Get(headerKey) w.WriteHeader(http.StatusOK) })) client := httptransport.NewClient( "GET", mustParse(server.URL), encode, decode, httptransport.SetClientBefore(httptransport.SetRequestHeader(headerKey, headerVal)), ) _, err := client.Endpoint()(context.Background(), struct{}{}) if err != nil { t.Fatal(err) } var have string select { case have = <-headers: case <-time.After(time.Millisecond): t.Fatalf("timeout waiting for %s", headerKey) } if want := headerVal; want != have { t.Errorf("want %q, have %q", want, have) } }
func TestClient(t *testing.T) { type myResponse struct { V int `json:"v"` } const v = 123 codec := jsoncodec.New() server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { codec.Encode(w, myResponse{v}) })) defer server.Close() makeResponse := func() interface{} { return &myResponse{} } client := httptransport.NewClient(server.URL, codec, makeResponse) resp, err := client(context.Background(), struct{}{}) if err != nil { t.Fatal(err) } response, ok := resp.(*myResponse) if !ok { t.Fatalf("not myResponse (%s)", reflect.TypeOf(response)) } if want, have := v, response.V; want != have { t.Errorf("want %d, have %d", want, have) } }
func TestHTTPClient(t *testing.T) { var ( testbody = "testbody" encode = func(context.Context, *http.Request, interface{}) error { return nil } decode = func(_ context.Context, r *http.Response) (interface{}, error) { buffer := make([]byte, len(testbody)) r.Body.Read(buffer) return TestResponse{r.Body, string(buffer)}, nil } headers = make(chan string, 1) headerKey = "X-Foo" headerVal = "abcde" ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { headers <- r.Header.Get(headerKey) w.WriteHeader(http.StatusOK) w.Write([]byte(testbody)) })) client := httptransport.NewClient( "GET", mustParse(server.URL), encode, decode, httptransport.SetClientBefore(httptransport.SetRequestHeader(headerKey, headerVal)), ) res, err := client.Endpoint()(context.Background(), struct{}{}) if err != nil { t.Fatal(err) } var have string select { case have = <-headers: case <-time.After(time.Millisecond): t.Fatalf("timeout waiting for %s", headerKey) } // Check that Request Header was successfully received if want := headerVal; want != have { t.Errorf("want %q, have %q", want, have) } // Check that the response was successfully decoded response, ok := res.(TestResponse) if !ok { t.Fatal("response should be TestResponse") } if want, have := testbody, response.String; want != have { t.Errorf("want %q, have %q", want, have) } // Check that response body was closed b := make([]byte, 1) _, err = response.Body.Read(b) if err == nil { t.Fatal("wanted error, got none") } if doNotWant, have := io.EOF, err; doNotWant == have { t.Errorf("do not want %q, have %q", doNotWant, have) } }
func main() { // Flag domain. Note that gRPC transitively registers flags via its import // of glog. So, we define a new flag set, to keep those domains distinct. fs := flag.NewFlagSet("", flag.ExitOnError) var ( debugAddr = fs.String("debug.addr", ":8000", "Address for HTTP debug/instrumentation server") httpAddr = fs.String("http.addr", ":8001", "Address for HTTP (JSON) server") grpcAddr = fs.String("grpc.addr", ":8002", "Address for gRPC server") thriftAddr = fs.String("thrift.addr", ":8003", "Address for Thrift server") thriftProtocol = fs.String("thrift.protocol", "binary", "binary, compact, json, simplejson") thriftBufferSize = fs.Int("thrift.buffer.size", 0, "0 for unbuffered") thriftFramed = fs.Bool("thrift.framed", false, "true to enable framing") proxyHTTPAddr = fs.String("proxy.http.url", "", "if set, proxy requests over HTTP to this addsvc") zipkinServiceName = fs.String("zipkin.service.name", "addsvc", "Zipkin service name") zipkinCollectorAddr = fs.String("zipkin.collector.addr", "", "Zipkin Scribe collector address (empty will log spans)") zipkinCollectorTimeout = fs.Duration("zipkin.collector.timeout", time.Second, "Zipkin collector timeout") zipkinCollectorBatchSize = fs.Int("zipkin.collector.batch.size", 100, "Zipkin collector batch size") zipkinCollectorBatchInterval = fs.Duration("zipkin.collector.batch.interval", time.Second, "Zipkin collector batch interval") ) flag.Usage = fs.Usage // only show our flags fs.Parse(os.Args[1:]) // `package log` domain var logger kitlog.Logger logger = kitlog.NewLogfmtLogger(os.Stderr) logger = kitlog.With(logger, "ts", kitlog.DefaultTimestampUTC, "caller", kitlog.DefaultCaller) stdlog.SetOutput(kitlog.NewStdlibAdapter(logger)) // redirect stdlib logging to us stdlog.SetFlags(0) // flags are handled in our logger // `package metrics` domain requests := metrics.NewMultiCounter( expvar.NewCounter("requests"), statsd.NewCounter(ioutil.Discard, "requests_total", time.Second), prometheus.NewCounter(stdprometheus.CounterOpts{ Namespace: "addsvc", Subsystem: "add", Name: "requests_total", Help: "Total number of received requests.", }, []string{}), ) duration := metrics.NewMultiHistogram( expvar.NewHistogram("duration_nanoseconds_total", 0, 100000000, 3), statsd.NewHistogram(ioutil.Discard, "duration_nanoseconds_total", time.Second), prometheus.NewSummary(stdprometheus.SummaryOpts{ Namespace: "addsvc", Subsystem: "add", Name: "duration_nanoseconds_total", Help: "Total nanoseconds spend serving requests.", }, []string{}), ) // `package tracing` domain zipkinHostPort := "localhost:1234" // TODO Zipkin makes overly simple assumptions about services var zipkinCollector zipkin.Collector = loggingCollector{logger} if *zipkinCollectorAddr != "" { var err error if zipkinCollector, err = zipkin.NewScribeCollector( *zipkinCollectorAddr, *zipkinCollectorTimeout, *zipkinCollectorBatchSize, *zipkinCollectorBatchInterval, ); err != nil { logger.Log("err", err) os.Exit(1) } } zipkinMethodName := "add" zipkinSpanFunc := zipkin.MakeNewSpanFunc(zipkinHostPort, *zipkinServiceName, zipkinMethodName) zipkin.Log.Swap(logger) // log diagnostic/error details // Our business and operational domain var a Add = pureAdd if *proxyHTTPAddr != "" { codec := jsoncodec.New() makeResponse := func() interface{} { return &addResponse{} } var e endpoint.Endpoint e = httptransport.NewClient(*proxyHTTPAddr, codec, makeResponse, httptransport.ClientBefore(zipkin.ToRequest(zipkinSpanFunc))) e = zipkin.AnnotateClient(zipkinSpanFunc, zipkinCollector)(e) a = proxyAdd(e, logger) } a = logging(logger)(a) // Server domain var e endpoint.Endpoint e = makeEndpoint(a) e = zipkin.AnnotateServer(zipkinSpanFunc, zipkinCollector)(e) // Mechanical stuff rand.Seed(time.Now().UnixNano()) root := context.Background() errc := make(chan error) go func() { errc <- interrupt() }() // Transport: HTTP (debug/instrumentation) go func() { logger.Log("addr", *debugAddr, "transport", "debug") errc <- http.ListenAndServe(*debugAddr, nil) }() // Transport: HTTP (JSON) go func() { ctx, cancel := context.WithCancel(root) defer cancel() field := metrics.Field{Key: "transport", Value: "http"} before := httptransport.BindingBefore(zipkin.ToContext(zipkinSpanFunc)) after := httptransport.BindingAfter(httptransport.SetContentType("application/json")) makeRequest := func() interface{} { return &addRequest{} } var handler http.Handler handler = httptransport.NewBinding(ctx, makeRequest, jsoncodec.New(), e, before, after) handler = encoding.Gzip(handler) handler = cors.Middleware(cors.Config{})(handler) handler = httpInstrument(requests.With(field), duration.With(field))(handler) mux := http.NewServeMux() mux.Handle("/add", handler) logger.Log("addr", *httpAddr, "transport", "HTTP") errc <- http.ListenAndServe(*httpAddr, mux) }() // Transport: gRPC go func() { ln, err := net.Listen("tcp", *grpcAddr) if err != nil { errc <- err return } s := grpc.NewServer() // uses its own context? field := metrics.Field{Key: "transport", Value: "grpc"} var addServer pb.AddServer addServer = grpcBinding{e} addServer = grpcInstrument(requests.With(field), duration.With(field))(addServer) pb.RegisterAddServer(s, addServer) logger.Log("addr", *grpcAddr, "transport", "gRPC") errc <- s.Serve(ln) }() // Transport: Thrift go func() { ctx, cancel := context.WithCancel(root) defer cancel() var protocolFactory thrift.TProtocolFactory switch *thriftProtocol { case "binary": protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() case "compact": protocolFactory = thrift.NewTCompactProtocolFactory() case "json": protocolFactory = thrift.NewTJSONProtocolFactory() case "simplejson": protocolFactory = thrift.NewTSimpleJSONProtocolFactory() default: errc <- fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol) return } var transportFactory thrift.TTransportFactory if *thriftBufferSize > 0 { transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize) } else { transportFactory = thrift.NewTTransportFactory() } if *thriftFramed { transportFactory = thrift.NewTFramedTransportFactory(transportFactory) } transport, err := thrift.NewTServerSocket(*thriftAddr) if err != nil { errc <- err return } field := metrics.Field{Key: "transport", Value: "thrift"} var service thriftadd.AddService service = thriftBinding{ctx, e} service = thriftInstrument(requests.With(field), duration.With(field))(service) logger.Log("addr", *thriftAddr, "transport", "Thrift") errc <- thrift.NewTSimpleServer4( thriftadd.NewAddServiceProcessor(service), transport, transportFactory, protocolFactory, ).Serve() }() logger.Log("fatal", <-errc) }