conf.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package conf
  2. import (
  3. "time"
  4. ecode "go-common/library/ecode/tip"
  5. "go-common/library/log"
  6. "go-common/library/naming/discovery"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/rpc/warden"
  9. "go-common/library/net/trace"
  10. xtime "go-common/library/time"
  11. "github.com/BurntSushi/toml"
  12. )
  13. var (
  14. // Conf config
  15. Conf = &Config{}
  16. )
  17. // Config .
  18. type Config struct {
  19. Broadcast *Broadcast
  20. Log *log.Config
  21. HTTP *bm.ServerConfig
  22. Tracer *trace.Config
  23. Ecode *ecode.Config
  24. WardenServer *warden.ServerConfig
  25. WardenClient *warden.ClientConfig
  26. Discovery *discovery.Config
  27. HTTPClient *bm.ClientConfig
  28. TCP *TCP
  29. WebSocket *WebSocket
  30. Timer *Timer
  31. ProtoSection *ProtoSection
  32. Whitelist *Whitelist
  33. Bucket *Bucket
  34. }
  35. // Broadcast config.
  36. type Broadcast struct {
  37. Debug bool
  38. MaxProc int
  39. ServerTick xtime.Duration
  40. OnlineTick xtime.Duration
  41. Failover bool
  42. APIHost string
  43. APIToken string
  44. OnlineRetries int
  45. OpenPortV1 bool
  46. }
  47. // TCP config
  48. type TCP struct {
  49. Bind []string
  50. BindV1 []string
  51. Sndbuf int
  52. Rcvbuf int
  53. Keepalive bool
  54. Reader int
  55. ReadBuf int
  56. ReadBufSize int
  57. Writer int
  58. WriteBuf int
  59. WriteBufSize int
  60. }
  61. // WebSocket config
  62. type WebSocket struct {
  63. Bind []string
  64. BindV1 []string
  65. TLSOpen bool
  66. TLSBind []string
  67. TLSBindV1 []string
  68. CertFile string
  69. PrivateFile string
  70. }
  71. // Timer config
  72. type Timer struct {
  73. Timer int
  74. TimerSize int
  75. }
  76. // ProtoSection config
  77. type ProtoSection struct {
  78. HandshakeTimeout xtime.Duration
  79. WriteTimeout xtime.Duration
  80. SvrProto int
  81. CliProto int
  82. }
  83. // Whitelist .
  84. type Whitelist struct {
  85. Whitelist []int64
  86. WhiteLog string
  87. }
  88. // Bucket .
  89. type Bucket struct {
  90. Size int
  91. Channel int
  92. Room int
  93. RoutineAmount uint64
  94. RoutineSize int
  95. }
  96. // Fix fix config to default.
  97. func (c *Config) Fix() {
  98. if c.Broadcast == nil {
  99. c.Broadcast = new(Broadcast)
  100. }
  101. if c.Broadcast.MaxProc <= 0 {
  102. c.Broadcast.MaxProc = 32
  103. }
  104. if c.Broadcast.ServerTick <= 0 {
  105. c.Broadcast.ServerTick = xtime.Duration(5 * time.Second)
  106. }
  107. if c.Broadcast.OnlineTick <= 0 {
  108. c.Broadcast.OnlineTick = xtime.Duration(10 * time.Second)
  109. }
  110. if c.Broadcast.APIHost == "" {
  111. c.Broadcast.APIHost = "http://api.bilibili.com"
  112. }
  113. }
  114. // Set set config and decode.
  115. func (c *Config) Set(text string) error {
  116. var tmp Config
  117. if _, err := toml.Decode(text, &tmp); err != nil {
  118. return err
  119. }
  120. tmp.Fix()
  121. *c = tmp
  122. return nil
  123. }