Ejemplo n.º 1
0
func main() {
	var help = flag.Bool("help", false, "print help")
	var port = flag.Int("port", -1, "port to listen")
	var rout = flag.String("rout", "random", "routing policy: random, roundrobin")
	var urls Urls
	flag.Var(&urls, "url", "backend url")
	var gracefulRestart = graceful.SetFlag()
	flag.Parse()

	if *help || *port == -1 || urls == nil {
		flag.PrintDefaults()
		return
	}

	router, err := balanser.NewChooser(*rout, urls)
	if err != nil {
		flag.PrintDefaults()
		return
	}

	balanser := btcp.NewBalanser(router, urls)
	server := grpc.NewServer(rpc.NewServer(), func(srv *rpc.Server, conn io.ReadWriteCloser) {
		if err := balanser.Request(conn.(*net.TCPConn)); err != nil {
			log.Errorln(err)
		}
	})

	graceful.SetSighup(server)

	if err := server.ListenAndServe(":"+strconv.Itoa(*port), *gracefulRestart); err != nil {
		log.Fatal(errors.NewErr(err))
	}

	if err := graceful.Restart(server); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 2
0
func NewServer(server *rpc.Server) *grpc.Server {
	return grpc.NewServer(server, func(srv *rpc.Server, conn io.ReadWriteCloser) {
		srv.ServeCodec(jsonrpc.NewServerCodec(conn))
	})
}