main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "go-common/app/service/live/broadcast-proxy/conf"
  6. "go-common/app/service/live/broadcast-proxy/server"
  7. "go-common/app/service/live/broadcast-proxy/service"
  8. "go-common/library/log"
  9. "net/http"
  10. "net/http/pprof"
  11. "runtime"
  12. "time"
  13. )
  14. func RunPprofServer(addr string) {
  15. pprofServer := http.NewServeMux()
  16. pprofServer.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
  17. pprofServer.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
  18. pprofServer.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
  19. pprofServer.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
  20. pprofServer.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
  21. go func() {
  22. if e := http.ListenAndServe(addr, pprofServer); e != nil {
  23. log.Error("pprof server error ListenAndServe addr:%s,error:%+v", addr, e)
  24. }
  25. defer func() {
  26. if e := recover(); e != nil {
  27. log.Error("expected panic from pprof server,error:%+v", e)
  28. }
  29. }()
  30. }()
  31. }
  32. func main() {
  33. runtime.GOMAXPROCS(runtime.NumCPU())
  34. var confTomlFile string
  35. flag.StringVar(&confTomlFile, "conf", "", "config file for broadcast proxy")
  36. flag.Parse()
  37. conf, err := conf.NewBroadcastProxyConfig(confTomlFile)
  38. if err != nil {
  39. panic(err)
  40. }
  41. log.Init(conf.Log)
  42. log.Info("Broadcast Proxy Service:%s", time.Now().String())
  43. log.Info("Broadcast Proxy Config:%+v", conf)
  44. proxy, err := server.NewBroadcastProxy(conf.Backend.BackendServer, conf.Backend.ProbePath,
  45. conf.Backend.MaxIdleConnsPerHost, conf.Backend.ProbeSample)
  46. if err != nil {
  47. panic(err)
  48. }
  49. defer proxy.Close()
  50. dispatcher, err := server.NewCometDispatcher(conf.Ipip, conf.Dispatch, conf.Sven)
  51. if err != nil {
  52. panic(err)
  53. }
  54. defer dispatcher.Close()
  55. httpService, err := server.NewBroadcastService(conf.Http.Address, proxy, dispatcher)
  56. if err != nil {
  57. panic(err)
  58. }
  59. defer httpService.Close()
  60. grpcService, err := service.NewGrpcService(proxy, dispatcher)
  61. if err != nil {
  62. panic(err)
  63. }
  64. defer grpcService.Shutdown(context.Background())
  65. RunPprofServer(conf.Perf)
  66. quit := make(chan struct{})
  67. <-quit
  68. }