func Start() { cfg := g.Config() if !cfg.Rpc.Enabled { log.Println("rpc.Start warning, not enable") return } addr := cfg.Rpc.Listen server := rpc.NewServer() server.Register(new(Agent)) l, e := net.Listen("tcp", addr) if e != nil { log.Fatalln("rpc.Start error", e) } else { log.Println("rpc.Start ok, listening on", addr) } go func() { for { conn, err := l.Accept() if err != nil { if cfg.Debug { log.Println("rpc accept fail:", err) } time.Sleep(time.Duration(100) * time.Millisecond) continue } go server.ServeCodec(jsonrpc.NewServerCodec(conn)) } }() }
func Start() { cfg := g.Config() if !cfg.Hbs.Enabled { log.Println("proxy.Start warning, hbs not enabled") return } initProxy() log.Println("proxy.Start ok") }
func startHttpServer() { if !g.Config().Http.Enabled { return } addr := g.Config().Http.Listen if addr == "" { return } configCommonRoutes() configProcHttpRoutes() s := &http.Server{ Addr: addr, MaxHeaderBytes: 1 << 30, } log.Println("http.startHttpServer ok, listening", addr) log.Fatalln(s.ListenAndServe()) }
func configProcHttpRoutes() { http.HandleFunc("/proc/config", func(w http.ResponseWriter, r *http.Request) { RenderDataJson(w, g.Config()) }) http.HandleFunc("/proc/counters", func(w http.ResponseWriter, r *http.Request) { RenderDataJson(w, make([]interface{}, 0)) }) http.HandleFunc("/proc/hbs/pools", func(w http.ResponseWriter, r *http.Request) { RenderDataJson(w, proxy.ConnPools.Proc()) }) }
// internal func initProxy() { cfg := g.Config() // init hbs global configs addrs := make([]string, 0) for hn, addr := range cfg.Hbs.Cluster { HbsHostnames = append(HbsHostnames, hn) addrs = append(addrs, addr) HbsMap[hn] = addr } // set consist HbsMaxConns = int64(cfg.Hbs.MaxConns) HbsNum = len(HbsHostnames) // init conn pools ConnPools = NewRpcConnPools(8, cfg.Hbs.MaxConns, cfg.Hbs.MaxIdle, cfg.Hbs.ConnTimeout, cfg.Hbs.CallTimeout, addrs) }