main.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/log"
  5. "app.yhyue.com/data_processing/common_utils/mongodb"
  6. gonsq "app.yhyue.com/data_processing/common_utils/nsq"
  7. "app.yhyue.com/data_processing/common_utils/redis"
  8. "app.yhyue.com/data_processing/common_utils/udp"
  9. "encoding/json"
  10. "field_sync/config"
  11. "field_sync/oss"
  12. "fmt"
  13. "go.uber.org/zap"
  14. "gopkg.in/mgo.v2/bson"
  15. "io/ioutil"
  16. "net"
  17. "net/http"
  18. "strings"
  19. "sync"
  20. "time"
  21. )
  22. var (
  23. MgoB *mongodb.MongodbSim
  24. MgoE *mongodb.MongodbSim
  25. MgoQ *mongodb.MongodbSim // 企业
  26. MgoP *mongodb.MongodbSim // 凭安企业
  27. UdpClient udp.UdpClient
  28. UdpTaskMap = &sync.Map{}
  29. Mcmer *gonsq.Consumer
  30. MgoBulkSize = 200 // mgo批量保存大小
  31. updateBidPool = make(chan []map[string]interface{}, 5000)
  32. updateBidSp = make(chan bool, 5)
  33. updateExtPool = make(chan []map[string]interface{}, 5000)
  34. updateExtSp = make(chan bool, 5)
  35. )
  36. func init() {
  37. config.Init("./common.toml")
  38. oss.InitOss()
  39. InitFileInfo()
  40. InitLog()
  41. InitMgo()
  42. inits()
  43. redis.InitRedis1(config.Conf.DB.Redis.Addr, config.Conf.DB.Redis.DbIndex)
  44. log.Info("init success")
  45. }
  46. func main() {
  47. go checkMapJob()
  48. go nsqMethod()
  49. go UpdateBidding()
  50. go UpdateExtract()
  51. UdpClient = udp.UdpClient{Local: config.Conf.Udp.LocPort, BufSize: 1024}
  52. UdpClient.Listen(processUdpMsg)
  53. log.Info("Udp服务监听", zap.String("port:", config.Conf.Udp.LocPort))
  54. ch := make(chan bool, 1)
  55. <-ch
  56. }
  57. var pool = make(chan bool, 20)
  58. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  59. defer util.Catch()
  60. switch act {
  61. case udp.OP_TYPE_DATA: //上个节点的数据
  62. var mapInfo map[string]interface{}
  63. err := json.Unmarshal(data, &mapInfo)
  64. log.Info("processUdpMsg", zap.Any("mapInfo:", mapInfo))
  65. if err != nil {
  66. UdpClient.WriteUdp([]byte("err:"+err.Error()), udp.OP_NOOP, ra)
  67. } else if mapInfo != nil {
  68. key, _ := mapInfo["key"].(string)
  69. if key == "" {
  70. key = "udpok"
  71. }
  72. go UdpClient.WriteUdp([]byte(key), udp.OP_NOOP, ra)
  73. tasktype, _ := mapInfo["stype"].(string)
  74. switch tasktype {
  75. case "bidding":
  76. pool <- true
  77. go func() {
  78. defer func() {
  79. <-pool
  80. }()
  81. biddingTask(data, mapInfo)
  82. }()
  83. case "bidding_history": //增量id段历史数据
  84. pool <- true
  85. go func() {
  86. defer func() {
  87. <-pool
  88. }()
  89. biddingTask(data, mapInfo)
  90. }()
  91. default:
  92. pool <- true
  93. go func() {
  94. defer func() {
  95. <-pool
  96. }()
  97. log.Error("err", zap.Any("mapinfo", mapInfo))
  98. }()
  99. }
  100. }
  101. case udp.OP_NOOP:
  102. ok := string(data)
  103. if ok != "" {
  104. log.Info("udp re", zap.String("data:", ok))
  105. UdpTaskMap.Delete(ok)
  106. }
  107. }
  108. }
  109. type UdpNode struct {
  110. data []byte
  111. addr *net.UDPAddr
  112. timestamp int64
  113. retry int
  114. }
  115. func NextNode(mapInfo map[string]interface{}) {
  116. var next = &net.UDPAddr{
  117. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  118. Port: util.IntAll(config.Conf.Udp.Next.Port),
  119. }
  120. mapInfo["stype"] = config.Conf.Udp.Next.Stype
  121. key := fmt.Sprintf("%s-%s-%s", util.ObjToString(mapInfo["gtid"]), util.ObjToString(mapInfo["lteid"]), config.Conf.Udp.Next.Stype)
  122. mapInfo["key"] = key
  123. log.Info("udp next node", zap.Any("mapinfo:", mapInfo))
  124. datas, _ := json.Marshal(mapInfo)
  125. node := &UdpNode{datas, next, time.Now().Unix(), 0}
  126. UdpTaskMap.Store(key, node)
  127. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  128. }
  129. func UpdateBidding() {
  130. arru := make([][]map[string]interface{}, MgoBulkSize)
  131. indexu := 0
  132. for {
  133. select {
  134. case v := <-updateBidPool:
  135. arru[indexu] = v
  136. indexu++
  137. if indexu == MgoBulkSize {
  138. updateBidSp <- true
  139. go func(arru [][]map[string]interface{}) {
  140. defer func() {
  141. <-updateBidSp
  142. }()
  143. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  144. }(arru)
  145. arru = make([][]map[string]interface{}, MgoBulkSize)
  146. indexu = 0
  147. }
  148. case <-time.After(1000 * time.Millisecond):
  149. if indexu > 0 {
  150. updateBidSp <- true
  151. go func(arru [][]map[string]interface{}) {
  152. defer func() {
  153. <-updateBidSp
  154. }()
  155. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, arru...)
  156. }(arru[:indexu])
  157. arru = make([][]map[string]interface{}, MgoBulkSize)
  158. indexu = 0
  159. }
  160. }
  161. }
  162. }
  163. func UpdateExtract() {
  164. arru := make([][]map[string]interface{}, MgoBulkSize)
  165. indexu := 0
  166. for {
  167. select {
  168. case v := <-updateExtPool:
  169. arru[indexu] = v
  170. indexu++
  171. if indexu == MgoBulkSize {
  172. updateExtSp <- true
  173. go func(arru [][]map[string]interface{}) {
  174. defer func() {
  175. <-updateExtSp
  176. }()
  177. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  178. }(arru)
  179. arru = make([][]map[string]interface{}, MgoBulkSize)
  180. indexu = 0
  181. }
  182. case <-time.After(1000 * time.Millisecond):
  183. if indexu > 0 {
  184. updateExtSp <- true
  185. go func(arru [][]map[string]interface{}) {
  186. defer func() {
  187. <-updateExtSp
  188. }()
  189. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, arru...)
  190. }(arru[:indexu])
  191. arru = make([][]map[string]interface{}, MgoBulkSize)
  192. indexu = 0
  193. }
  194. }
  195. }
  196. }
  197. func checkMapJob() {
  198. if config.Conf.Mail.Send {
  199. log.Info("checkMapJob", zap.String("to:", config.Conf.Mail.To))
  200. for {
  201. UdpTaskMap.Range(func(k, v interface{}) bool {
  202. now := time.Now().Unix()
  203. node, _ := v.(*UdpNode)
  204. if now-node.timestamp > 120 {
  205. node.retry++
  206. if node.retry > 5 {
  207. UdpTaskMap.Delete(k)
  208. res, err := http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", config.Conf.Mail.Api, config.Conf.Mail.To, "field-sync-send-fail", k.(string)))
  209. if err == nil {
  210. defer res.Body.Close()
  211. read, err := ioutil.ReadAll(res.Body)
  212. log.Info("send mail ...", zap.String("r:", string(read)), zap.Any("err:", err))
  213. }
  214. } else {
  215. log.Info("udp重发", zap.Any("k:", k))
  216. UdpClient.WriteUdp(node.data, udp.OP_TYPE_DATA, node.addr)
  217. }
  218. } else if now-node.timestamp > 10 {
  219. log.Info("udp任务超时中..", zap.Any("k:", k))
  220. }
  221. return true
  222. })
  223. time.Sleep(60 * time.Second)
  224. }
  225. }
  226. }
  227. // @Description nsq处理id不变,内容替换的竞品数据
  228. // @Author J 2022/8/10 11:40
  229. func nsqMethod() {
  230. var err error
  231. Mcmer, err = gonsq.NewConsumer(&gonsq.Cconfig{
  232. IsJsonEncode: true, //与生产者配置对应,设为true会取第1个字节进行类型判断
  233. Addr: config.Conf.Nsq.Addr,
  234. ConnectType: 0, //默认连接nsqd
  235. Topic: config.Conf.Nsq.Topic,
  236. Channel: config.Conf.Nsq.Channel,
  237. Concurrent: config.Conf.Nsq.Concurrent, //并发数
  238. })
  239. if err != nil {
  240. log.Error("nsqMethod err", zap.Error(err))
  241. }
  242. for {
  243. select {
  244. case obj := <-Mcmer.Ch: //从通道读取即可
  245. id := strings.Split(util.ObjToString(obj), "=")
  246. if bson.IsObjectIdHex(id[1]) {
  247. taskinfo(id[1])
  248. } else {
  249. log.Info("jy nsq id err", zap.String("id", id[1]))
  250. }
  251. }
  252. }
  253. }