Codebase list golang-github-go-kit-kit / c6c2d97
examples/addsvc2: better commentary Peter Bourgon 6 years ago
3 changed file(s) with 71 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
88 "os"
99 "os/signal"
1010 "syscall"
11 "text/tabwriter"
1112
1213 "github.com/apache/thrift/lib/go/thrift"
1314 "github.com/oklog/oklog/pkg/group"
2829 )
2930
3031 func main() {
32 // Define our flags. Your service probably won't need to bind listeners for
33 // *all* supported transports, or support both Zipkin and LightStep, and so
34 // on, but we do it here for demonstration purposes.
35 fs := flag.NewFlagSet("addsvc", flag.ExitOnError)
3136 var (
32 debugAddr = flag.String("debug.addr", ":8080", "Debug and metrics listen address")
33 httpAddr = flag.String("http-addr", ":8081", "HTTP listen address")
34 grpcAddr = flag.String("grpc-addr", ":8082", "gRPC listen address")
35 thriftAddr = flag.String("thrift-addr", ":8082", "Thrift listen address")
36 thriftProtocol = flag.String("thrift.protocol", "binary", "binary, compact, json, simplejson")
37 thriftBufferSize = flag.Int("thrift.buffer.size", 0, "0 for unbuffered")
38 thriftFramed = flag.Bool("thrift.framed", false, "true to enable framing")
39 zipkinURL = flag.String("zipkin-url", "", "Zipkin collector URL e.g. http://localhost:9411/api/v1/spans")
37 debugAddr = fs.String("debug.addr", ":8080", "Debug and metrics listen address")
38 httpAddr = fs.String("http-addr", ":8081", "HTTP listen address")
39 grpcAddr = fs.String("grpc-addr", ":8082", "gRPC listen address")
40 thriftAddr = fs.String("thrift-addr", ":8082", "Thrift listen address")
41 thriftProtocol = fs.String("thrift-protocol", "binary", "binary, compact, json, simplejson")
42 thriftBuffer = fs.Int("thrift-buffer", 0, "0 for unbuffered")
43 thriftFramed = fs.Bool("thrift-framed", false, "true to enable framing")
44 zipkinURL = fs.String("zipkin-url", "", "Zipkin collector URL e.g. http://localhost:9411/api/v1/spans")
4045 )
41 flag.Parse()
42
46 fs.Usage = usageFor(fs, os.Args[0]+" [flags]")
47 fs.Parse(os.Args[1:])
48
49 // Create a single logger, which we'll use and give to other components.
4350 var logger log.Logger
4451 {
4552 logger = log.NewLogfmtLogger(os.Stderr)
4754 logger = log.With(logger, "caller", log.DefaultCaller)
4855 }
4956
57 // Determine which tracer to use. We'll pass the tracer to all the
58 // components that use it, as a dependency.
5059 var tracer stdopentracing.Tracer
5160 {
5261 if *zipkinURL != "" {
7483 }
7584 }
7685
77 // Our metrics are dependencies, here we create them.
86 // Create the (sparse) metrics we'll use in the service. They, too, are
87 // dependencies that we pass to components that use them.
7888 var ints, chars metrics.Counter
7989 {
8090 // Business-level metrics.
102112 }, []string{"method", "success"})
103113 }
104114
115 // Build the layers of the service "onion" from the inside out. First, the
116 // business logic service; then, the set of endpoints that wrap the service;
117 // and finally, a series of concrete transport adapters. The adapters, like
118 // the HTTP handler or the gRPC server, are the bridge between Go kit and
119 // the interfaces that the transports expect. Note that we're not binding
120 // them to ports or anything yet; we'll do that next.
105121 var (
106 service = addservice.New(logger, ints, chars)
107 endpoints = addendpoint.New(service, logger, duration, tracer)
108 httpHandler = addtransport.NewHTTPHandler(context.Background(), endpoints, logger, tracer)
109 grpcServer = addtransport.MakeGRPCServer(endpoints, tracer, logger)
110 thriftHandler = addtransport.MakeThriftHandler(context.Background(), endpoints)
122 service = addservice.New(logger, ints, chars)
123 endpoints = addendpoint.New(service, logger, duration, tracer)
124 httpHandler = addtransport.NewHTTPHandler(context.Background(), endpoints, logger, tracer)
125 grpcServer = addtransport.NewGRPCServer(endpoints, tracer, logger)
126 thriftServer = addtransport.NewThriftServer(context.Background(), endpoints)
111127 )
112128
129 // Now we're to the part of the func main where we want to start actually
130 // running things, like servers bound to listeners to receive connections.
131 //
132 // The method is the same for each component: add a new actor to the group
133 // struct, which is a combination of 2 anonymous functions: the first
134 // function actually runs the component, and the second function should
135 // interrupt the first function and cause it to return.
136
113137 var g group.Group
114138 {
139 // The debug listener mounts the http.DefaultServeMux, and serves up
140 // stuff like the Prometheus metrics route, the Go debug and profiling
141 // routes, and so on.
115142 debugListener, err := net.Listen("tcp", *debugAddr)
116143 if err != nil {
117144 logger.Log("transport", "debug/HTTP", "during", "Listen", "err", err)
125152 })
126153 }
127154 {
155 // The HTTP listener mounts the Go kit HTTP handler we created.
128156 httpListener, err := net.Listen("tcp", *httpAddr)
129157 if err != nil {
130158 logger.Log("transport", "HTTP", "during", "Listen", "err", err)
138166 })
139167 }
140168 {
169 // The gRPC listener mounts the Go kit gRPC server we created.
141170 grpcListener, err := net.Listen("tcp", *grpcAddr)
142171 if err != nil {
143172 logger.Log("transport", "gRPC", "during", "Listen", "err", err)
153182 })
154183 }
155184 {
185 // The Thrift socket mounts the Go kit Thrift server we created earlier.
186 // There's a lot of boilerplate involved here, related to configuring
187 // the protocol and transport; blame Thrift.
156188 thriftSocket, err := thrift.NewTServerSocket(*thriftAddr)
157189 if err != nil {
158190 logger.Log("transport", "Thrift", "during", "Listen", "err", err)
174206 return fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol)
175207 }
176208 var transportFactory thrift.TTransportFactory
177 if *thriftBufferSize > 0 {
178 transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
209 if *thriftBuffer > 0 {
210 transportFactory = thrift.NewTBufferedTransportFactory(*thriftBuffer)
179211 } else {
180212 transportFactory = thrift.NewTTransportFactory()
181213 }
183215 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
184216 }
185217 return thrift.NewTSimpleServer4(
186 addthrift.NewAddServiceProcessor(thriftHandler),
218 addthrift.NewAddServiceProcessor(thriftServer),
187219 thriftSocket,
188220 transportFactory,
189221 protocolFactory,
193225 })
194226 }
195227 {
228 // This function just sits and waits for ctrl-C.
196229 cancelInterrupt := make(chan struct{})
197230 g.Add(func() error {
198231 c := make(chan os.Signal, 1)
209242 }
210243 logger.Log("exit", g.Run())
211244 }
245
246 func usageFor(fs *flag.FlagSet, short string) func() {
247 return func() {
248 fmt.Fprintf(os.Stderr, "USAGE\n")
249 fmt.Fprintf(os.Stderr, " %s\n", short)
250 fmt.Fprintf(os.Stderr, "\n")
251 fmt.Fprintf(os.Stderr, "FLAGS\n")
252 w := tabwriter.NewWriter(os.Stderr, 0, 2, 2, ' ', 0)
253 fs.VisitAll(func(f *flag.Flag) {
254 fmt.Fprintf(w, "\t-%s %s\t%s\n", f.Name, f.DefValue, f.Usage)
255 })
256 w.Flush()
257 fmt.Fprintf(os.Stderr, "\n")
258 }
259 }
1414 "github.com/go-kit/kit/examples/addsvc2/pkg/endpoint"
1515 )
1616
17 // MakeGRPCServer makes a set of endpoints available as a gRPC AddServer.
18 func MakeGRPCServer(endpoints endpoint.Set, tracer stdopentracing.Tracer, logger log.Logger) pb.AddServer {
17 // NewGRPCServer makes a set of endpoints available as a gRPC AddServer.
18 func NewGRPCServer(endpoints endpoint.Set, tracer stdopentracing.Tracer, logger log.Logger) pb.AddServer {
1919 options := []grpctransport.ServerOption{
2020 grpctransport.ServerErrorLogger(logger),
2121 }
88 thriftadd "github.com/go-kit/kit/examples/addsvc2/thrift/gen-go/addsvc"
99 )
1010
11 // MakeThriftHandler makes a set of endpoints available as a Thrift service.
12 func MakeThriftHandler(ctx context.Context, endpoints addendpoint.Set) thriftadd.AddService {
11 // NewThriftServer makes a set of endpoints available as a Thrift service.
12 func NewThriftServer(ctx context.Context, endpoints addendpoint.Set) thriftadd.AddService {
1313 return &thriftServer{
1414 ctx: ctx,
1515 endpoints: endpoints,