Exemplo n.º 1
0
func main() {

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "hello",
		Inbounds: []transport.Inbound{
			http.NewInbound(":8086"),
		},
		Outbounds: yarpc.Outbounds{
			"hello": {
				Unary: http.NewOutbound("http://127.0.0.1:8086"),
			},
		},
	})

	dispatcher.Register(helloserver.New(&helloHandler{}))
	client := helloclient.New(dispatcher.Channel("hello"))

	if err := dispatcher.Start(); err != nil {
		log.Fatal(err)
	}
	defer dispatcher.Stop()

	response, headers := call(client, "Hi There")
	fmt.Println(response, headers)

	select {}
}
Exemplo n.º 2
0
func withConnectedClient(t *testing.T, recorder *Recorder, f func(raw.Client)) {
	serverHTTP := http.NewInbound(":0")

	serverDisp := yarpc.NewDispatcher(yarpc.Config{
		Name:     "server",
		Inbounds: []transport.Inbound{serverHTTP},
	})

	serverDisp.Register(raw.Procedure("hello",
		func(ctx context.Context, reqMeta yarpc.ReqMeta, body []byte) ([]byte, yarpc.ResMeta, error) {
			return append(body, []byte(", World")...), nil, nil
		}))

	require.NoError(t, serverDisp.Start())
	defer serverDisp.Stop()

	clientDisp := yarpc.NewDispatcher(yarpc.Config{
		Name: "client",
		Outbounds: yarpc.Outbounds{
			"server": {
				Unary: http.NewOutbound(fmt.Sprintf("http://%s", serverHTTP.Addr().String())),
			},
		},
		Filter: recorder,
	})
	require.NoError(t, clientDisp.Start())
	defer clientDisp.Stop()

	client := raw.New(clientDisp.Channel("server"))
	f(client)
}
Exemplo n.º 3
0
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 {}
}
Exemplo n.º 4
0
func (ht httpTransport) WithRegistry(r transport.Registry, f func(transport.UnaryOutbound)) {
	i := http.NewInbound("127.0.0.1:0")
	require.NoError(ht.t, i.Start(transport.ServiceDetail{Name: testService, Registry: r}, transport.NoDeps), "failed to start")
	defer i.Stop()

	addr := fmt.Sprintf("http://%v/", i.Addr().String())
	o := http.NewOutbound(addr)
	require.NoError(ht.t, o.Start(transport.NoDeps), "failed to start outbound")
	defer o.Stop()
	f(o)
}
Exemplo n.º 5
0
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"),
		},
	})
}
Exemplo n.º 6
0
func Benchmark_HTTP_NetHTTPToYARPC(b *testing.B) {
	serverCfg := yarpc.Config{
		Name:     "server",
		Inbounds: []transport.Inbound{yhttp.NewInbound(":8996")},
	}

	withDispatcher(b, serverCfg, func(server yarpc.Dispatcher) {
		server.Register(raw.Procedure("echo", yarpcEcho))

		b.ResetTimer()
		runHTTPClient(b, http.DefaultClient, "http://localhost:8996")
	})
}
Exemplo n.º 7
0
func newDispatcher(t crossdock.T) yarpc.Dispatcher {
	server := t.Param(params.Server)
	crossdock.Fatals(t).NotEmpty(server, "server is required")

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "client",
		Outbounds: yarpc.Outbounds{
			"oneway-test": {
				Oneway: http.NewOutbound(fmt.Sprintf("http://%s:8084", server)),
			},
		},
		//for call back
		Inbounds: []transport.Inbound{http.NewInbound(fmt.Sprintf("%s:8089", server))},
	})

	// register procedure for remote server to call us back on
	dispatcher.Register(raw.OnewayProcedure("call-back", callBack))
	return dispatcher
}
Exemplo n.º 8
0
func createHTTPDispatcher(tracer opentracing.Tracer) yarpc.Dispatcher {
	// TODO: Use port 0 once https://github.com/yarpc/yarpc-go/issues/381 is
	// fixed.

	dispatcher := yarpc.NewDispatcher(yarpc.Config{
		Name: "yarpc-test",
		Inbounds: []transport.Inbound{
			http.NewInbound(":18080"),
		},
		Outbounds: yarpc.Outbounds{
			"yarpc-test": {
				Unary: http.NewOutbound("http://127.0.0.1:18080"),
			},
		},
		Tracer: tracer,
	})

	return dispatcher
}
Exemplo n.º 9
0
// 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())
	}
}
Exemplo n.º 10
0
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
}
Exemplo n.º 11
0
// Start starts the test server that clients will make requests to
func Start() {
	h := onewayHandler{
		Outbound: http.NewOutbound("http://127.0.0.1:8089"),
	}

	dispatcher = yarpc.NewDispatcher(yarpc.Config{
		Name: "oneway-test",
		Inbounds: []transport.Inbound{
			http.NewInbound(":8084"),
		},
		Outbounds: yarpc.Outbounds{
			"client": {Oneway: h.Outbound},
		},
	})

	dispatcher.Register(raw.OnewayProcedure("echo/raw", h.EchoRaw))
	dispatcher.Register(json.OnewayProcedure("echo/json", h.EchoJSON))
	dispatcher.Register(onewayserver.New(&h))

	if err := dispatcher.Start(); err != nil {
		fmt.Println("error:", err.Error())
	}
}
Exemplo n.º 12
0
func Benchmark_HTTP_YARPCToYARPC(b *testing.B) {
	serverCfg := yarpc.Config{
		Name:     "server",
		Inbounds: []transport.Inbound{yhttp.NewInbound(":8999")},
	}

	clientCfg := yarpc.Config{
		Name: "client",
		Outbounds: yarpc.Outbounds{
			"server": {
				Unary: yhttp.NewOutbound("http://localhost:8999"),
			},
		},
	}

	withDispatcher(b, serverCfg, func(server yarpc.Dispatcher) {
		server.Register(raw.Procedure("echo", yarpcEcho))
		withDispatcher(b, clientCfg, func(client yarpc.Dispatcher) {
			b.ResetTimer()
			runYARPCClient(b, raw.New(client.Channel("server")))
		})
	})
}
Exemplo n.º 13
0
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
}