Ejemplo n.º 1
0
// Serve on a given port
//
// The server will log to the default logger and will gracefully terminate on
// receipt of an interrupt or kill signal.
//
// The following URLs are defined:
//    /
//    /healthz
//    /varz
//    /streamz
//    /birpc
//    /debug/pprof
//
// Setting port to 0 will start the server on an ephemeral port. The assigned
// port will be logged.
func ServeForever(port int) error {
	l, err := vnet.SignalListener(port)
	if err != nil {
		log.Errorf("govtil/net/server: failed to open port: %v", err)
		return err
	}
	return ServeListenerForever(l)
}
Ejemplo n.º 2
0
func LocalListener() (net.Listener, int, error) {
	l, err := vnet.SignalListener(0)
	if err != nil {
		return nil, 0, err
	}
	_, aps, err := net.SplitHostPort(l.Addr().String())
	if err != nil {
		return nil, 0, err
	}
	port, err := strconv.Atoi(aps)
	if err != nil {
		return nil, 0, err
	}
	return l, port, nil
}
Ejemplo n.º 3
0
func TestBiRPC(t *testing.T) {
	// setup RPC
	arith := new(Arith)
	srv := rpc.NewServer()
	srv.Register(arith)
	clientch := make(chan *rpc.Client)

	http.Handle("/birpc", Handler(srv, clientch))

	// Start server
	const port = 11235
	l, err := vnet.SignalListener(port)
	if err != nil {
		t.Fatalf("Server: %v", err)
	}
	go http.Serve(l, http.DefaultServeMux)

	// Dial to self
	dialClient, err := Dial("ws://localhost:"+fmt.Sprint(port)+"/birpc", srv)
	if err != nil {
		t.Fatalf("Dial: %v", err)
	}

	sch := <-clientch // server's client

	// call from dialers side
	prod := Product{}
	err = dialClient.Call("Arith.Multiply", &Args{2, 4}, &prod)
	if err != nil {
		t.Fatalf("Call: %v", err)
	}
	if prod.R != 8 {
		t.Errorf("bad prod '%d', expected %d", prod.R, 8)
	}

	// call from server's side
	quot := Quotient{}
	err = sch.Call("Arith.Divide", &Args{16, 2}, &quot)
	if err != nil {
		t.Fatalf("Call: %v", err)
	}
	if quot.Quo != 8 || quot.Rem != 0 {
		t.Errorf("bad quot %v", quot)
	}
}