func Benchmark_TChannel_YARPCToYARPC(b *testing.B) { serverCh, err := tchannel.NewChannel("server", nil) require.NoError(b, err, "failed to build server TChannel") serverCfg := yarpc.Config{ Name: "server", Inbounds: []transport.Inbound{ytchannel.NewInbound(serverCh)}, } clientCh, err := tchannel.NewChannel("client", nil) require.NoError(b, err, "failed to build client TChannel") // no defer close on channels because YARPC will take care of that withDispatcher(b, serverCfg, func(server yarpc.Dispatcher) { server.Register(raw.Procedure("echo", yarpcEcho)) // Need server already started to build client config clientCfg := yarpc.Config{ Name: "client", Outbounds: yarpc.Outbounds{ "server": { Unary: ytchannel.NewOutbound(clientCh, ytchannel.HostPort(serverCh.PeerInfo().HostPort)), }, }, } withDispatcher(b, clientCfg, func(client yarpc.Dispatcher) { b.ResetTimer() runYARPCClient(b, raw.New(client.Channel("server"))) }) }) }
func main() { channel, err := tchannel.NewChannel("keyvalue", nil) if err != nil { log.Fatalln(err) } dispatcher := yarpc.NewDispatcher(yarpc.Config{ Name: "keyvalue", Inbounds: []transport.Inbound{ tch.NewInbound(channel, tch.ListenAddr(":28941")), http.NewInbound(":24034"), }, Interceptor: yarpc.Interceptors(requestLogInterceptor{}), }) handler := handler{items: make(map[string]string)} dispatcher.Register(json.Procedure("get", handler.Get)) dispatcher.Register(json.Procedure("set", handler.Set)) if err := dispatcher.Start(); err != nil { fmt.Println("error:", err.Error()) os.Exit(1) } select {} }
func basicDispatcher(t *testing.T) Dispatcher { ch, err := tchannel.NewChannel("test", nil) require.NoError(t, err, "failed to create TChannel") return NewDispatcher(Config{ Name: "test", Inbounds: []transport.Inbound{ tch.NewInbound(ch, tch.ListenAddr(":0")), http.NewInbound(":0"), }, }) }
func (tt tchannelTransport) WithRegistry(r transport.Registry, f func(transport.UnaryOutbound)) { serverOpts := testutils.NewOpts().SetServiceName(testService) clientOpts := testutils.NewOpts().SetServiceName(testCaller) testutils.WithServer(tt.t, serverOpts, func(ch *tchannel.Channel, hostPort string) { i := tch.NewInbound(ch) require.NoError(tt.t, i.Start(transport.ServiceDetail{Name: testService, Registry: r}, transport.NoDeps), "failed to start") defer i.Stop() // ^ the server is already listening so this will just set up the // handler. client := testutils.NewClient(tt.t, clientOpts) o := tch.NewOutbound(client, tch.HostPort(hostPort)) require.NoError(tt.t, o.Start(transport.NoDeps), "failed to start outbound") defer o.Stop() f(o) }) }
func Benchmark_TChannel_TChannelToYARPC(b *testing.B) { serverCh, err := tchannel.NewChannel("server", nil) require.NoError(b, err, "failed to build server TChannel") serverCfg := yarpc.Config{ Name: "server", Inbounds: []transport.Inbound{ytchannel.NewInbound(serverCh)}, } withDispatcher(b, serverCfg, func(server yarpc.Dispatcher) { server.Register(raw.Procedure("echo", yarpcEcho)) clientCh, err := tchannel.NewChannel("client", nil) require.NoError(b, err, "failed to build client TChannel") defer clientCh.Close() b.ResetTimer() runTChannelClient(b, clientCh, serverCh.PeerInfo().HostPort) }) }
// Start starts the test server that clients will make requests to func Start() { ch, err := tchannel.NewChannel("yarpc-test", nil) if err != nil { log.Fatalln("couldn't create tchannel: %v", err) } dispatcher = yarpc.NewDispatcher(yarpc.Config{ Name: "yarpc-test", Inbounds: []transport.Inbound{ http.NewInbound(":8081"), tch.NewInbound(ch, tch.ListenAddr(":8082")), }, }) register(dispatcher) if err := dispatcher.Start(); err != nil { fmt.Println("error:", err.Error()) } }
func buildDispatcher(t crossdock.T) (dispatcher yarpc.Dispatcher, tconfig server.TransportConfig) { fatals := crossdock.Fatals(t) self := t.Param("ctxclient") subject := t.Param("ctxserver") fatals.NotEmpty(self, "ctxclient is required") fatals.NotEmpty(subject, "ctxserver is required") ch, err := tchannel.NewChannel("ctxclient", nil) fatals.NoError(err, "failed to create TChannel") var outbound transport.UnaryOutbound switch trans := t.Param(params.Transport); trans { case "http": outbound = ht.NewOutbound(fmt.Sprintf("http://%s:8081", subject)) tconfig.TChannel = &server.TChannelTransport{Host: self, Port: 8087} case "tchannel": outbound = tch.NewOutbound(ch, tch.HostPort(fmt.Sprintf("%s:8082", subject))) tconfig.HTTP = &server.HTTPTransport{Host: self, Port: 8086} default: fatals.Fail("", "unknown transport %q", trans) } dispatcher = yarpc.NewDispatcher(yarpc.Config{ Name: "ctxclient", Inbounds: []transport.Inbound{ tch.NewInbound(ch, tch.ListenAddr(":8087")), ht.NewInbound(":8086"), }, Outbounds: yarpc.Outbounds{ "yarpc-test": { Unary: outbound, }, }, }) return dispatcher, tconfig }
func main() { channel, err := tchannel.NewChannel("keyvalue", nil) if err != nil { log.Fatalln(err) } dispatcher := yarpc.NewDispatcher(yarpc.Config{ Name: "keyvalue", Inbounds: []transport.Inbound{ tch.NewInbound(channel, tch.ListenAddr(":28941")), http.NewInbound(":24034"), }, }) handler := handler{items: make(map[string]string)} dispatcher.Register(keyvalueserver.New(&handler)) if err := dispatcher.Start(); err != nil { fmt.Println("error:", err.Error()) } select {} // block forever }
func createTChannelDispatcher(tracer opentracing.Tracer, t *testing.T) yarpc.Dispatcher { // Establish the TChannel ch, err := tchannel.NewChannel("yarpc-test", &tchannel.ChannelOptions{ Tracer: tracer, }) assert.NoError(t, err) hp := "127.0.0.1:4040" ch.ListenAndServe(hp) dispatcher := yarpc.NewDispatcher(yarpc.Config{ Name: "yarpc-test", Inbounds: []transport.Inbound{ ytchannel.NewInbound(ch), }, Outbounds: yarpc.Outbounds{ "yarpc-test": { Unary: ytchannel.NewOutbound(ch, ytchannel.HostPort(hp)), }, }, Tracer: tracer, }) return dispatcher }