conf.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/net/rpc/warden"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/conf"
  9. "go-common/library/database/sql"
  10. eCode "go-common/library/ecode/tip"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/rpc"
  15. "go-common/library/net/rpc/liverpc"
  16. "go-common/library/net/trace"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. Redis *redis.Config
  32. MemCache *memcache.Config
  33. MySQL *sql.Config
  34. ECode *eCode.Config
  35. LiveRpc map[string]*liverpc.ClientConfig
  36. HttpClient *bm.ClientConfig
  37. SkyHorseGray map[string]bool
  38. SkyHorseStatus bool
  39. RpcTimeout map[string]int64
  40. Bvc map[string]string
  41. ChunkSize map[string]int64
  42. DummyUid map[string]int64
  43. AccountRPC *rpc.ClientConfig
  44. XuserClient *warden.ClientConfig
  45. LiveGray map[string]bool
  46. Warden *warden.ClientConfig
  47. AppConf map[string]string
  48. }
  49. // ErrLogStrut ...
  50. // 自定义ErrLog结构
  51. type ErrLogStrut struct {
  52. Code int64
  53. Msg string
  54. ErrDesc string
  55. ErrType string
  56. URLName string
  57. RPCTimeout int64
  58. ChunkSize int64
  59. ChunkNum int64
  60. ErrorPtr *error
  61. }
  62. // ChunkCallInfo rpc调用配置
  63. type ChunkCallInfo struct {
  64. ParamsName string
  65. URLName string
  66. ChunkSize int64
  67. ChunkNum int64
  68. RPCTimeout int64
  69. }
  70. const (
  71. // EmptyResultEn 返回结果集为空
  72. EmptyResultEn = "got_empty_result"
  73. // EmptyResult 返回结果集为空
  74. EmptyResult = "调用直播服务返回data为空"
  75. // GetStatusInfoByUfos 获取房间信息
  76. GetStatusInfoByUfos = "room/v1/Room/get_status_info_by_uids"
  77. // TargetsWithMedal 获取房间信息
  78. TargetsWithMedal = "fans_medal/v1/FansMedal/targetsWithMedal"
  79. // GetRoomID 获取房间信息
  80. GetRoomID = "room/v2/Room/room_id_by_uid_multi"
  81. // Record 获取房间信息
  82. Record = "live_data/v1/Record/get"
  83. // GetPkIdsByRoomIds 获取房间信息
  84. GetPkIdsByRoomIds = "av/v1/Pk/getPkIdsByRoomIds"
  85. // RoomPendent 获取房间信息
  86. RoomPendent = "room/v1/RoomPendant/getPendantByIds"
  87. // RoomNews 获取房间信息
  88. RoomNews = "/room_ex/v1/RoomNews/multiGet"
  89. // RelationGiftInfo 获取房间信息
  90. // RelationGiftInfo = "/relation/v1/BaseInfo/getGiftInfo"
  91. // AccountGRPC ... 主站grpc用户信息
  92. AccountGRPC = "Cards3"
  93. // LiveUserExpGRPC ...
  94. // 直播用户经验grpc
  95. LiveUserExpGRPC = "xuserExp"
  96. )
  97. func init() {
  98. flag.StringVar(&confPath, "conf", "", "default config path")
  99. }
  100. // Init init conf
  101. func Init() error {
  102. if confPath != "" {
  103. return local()
  104. }
  105. return remote()
  106. }
  107. func local() (err error) {
  108. _, err = toml.DecodeFile(confPath, &Conf)
  109. return
  110. }
  111. func remote() (err error) {
  112. if client, err = conf.New(); err != nil {
  113. return
  114. }
  115. if err = load(); err != nil {
  116. return
  117. }
  118. client.Watch("app-interface.toml")
  119. go func() {
  120. for range client.Event() {
  121. log.Info("config reload")
  122. if load() != nil {
  123. log.Error("config reload error (%v)", err)
  124. }
  125. }
  126. }()
  127. return
  128. }
  129. func load() (err error) {
  130. var (
  131. s string
  132. ok bool
  133. tmpConf *Config
  134. )
  135. if s, ok = client.Toml2(); !ok {
  136. return errors.New("load config center error")
  137. }
  138. if _, err = toml.Decode(s, &tmpConf); err != nil {
  139. return errors.New("could not decode config")
  140. }
  141. *Conf = *tmpConf
  142. return
  143. }
  144. // GetTimeout implementation
  145. // 获取超时
  146. func GetTimeout(k string, def int64) (timeout int64) {
  147. if t, ok := Conf.RpcTimeout[k]; ok {
  148. timeout = t
  149. } else {
  150. timeout = def
  151. }
  152. return
  153. }
  154. // GetDummyUidConf implementation
  155. // 获取模拟配置开关
  156. func GetDummyUidConf() (config int64) {
  157. if t, ok := Conf.DummyUid["enable"]; ok {
  158. config = t
  159. } else {
  160. config = 0
  161. }
  162. return
  163. }
  164. // GetChunkSize implementation
  165. // 获取模GetChunkSize
  166. func GetChunkSize(k string, def int64) (timeout int64) {
  167. if t, ok := Conf.ChunkSize[k]; ok {
  168. timeout = t
  169. } else {
  170. timeout = def
  171. }
  172. return
  173. }
  174. // CheckReturn ...
  175. // 检查返回
  176. func CheckReturn(err error, code int64, msg string, urlName string,
  177. rpcTimeout int64, chunkSize int64, chunkNum int64) (errLog *ErrLogStrut, success bool) {
  178. errInfo := ErrLogStrut{}
  179. errInfo.URLName = urlName
  180. errInfo.RPCTimeout = rpcTimeout
  181. errInfo.ChunkSize = chunkSize
  182. errInfo.ChunkNum = chunkNum
  183. success = true
  184. if err != nil {
  185. errInfo.Code = 1003000
  186. errInfo.Msg = ""
  187. errInfo.ErrDesc = "liveRpc调用失败"
  188. errInfo.ErrType = "LiveRpcFrameWorkCallError"
  189. errInfo.ErrorPtr = &err
  190. success = false
  191. } else if code != 0 {
  192. errInfo.Code = code
  193. errInfo.Msg = msg
  194. errInfo.ErrDesc = "调用直播服务" + urlName + "出错"
  195. errInfo.ErrType = "CallLiveRpcCodeError"
  196. success = false
  197. }
  198. errLog = &errInfo
  199. return
  200. }