client.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package dao
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "errors"
  6. "time"
  7. "go-common/app/service/main/push/dao/apns2"
  8. "go-common/app/service/main/push/dao/fcm"
  9. "go-common/app/service/main/push/dao/huawei"
  10. "go-common/app/service/main/push/dao/jpush"
  11. "go-common/app/service/main/push/dao/mi"
  12. "go-common/app/service/main/push/dao/oppo"
  13. "go-common/app/service/main/push/model"
  14. "go-common/library/conf/env"
  15. "go-common/library/log"
  16. )
  17. var errNoClinets = errors.New("no clients")
  18. func (d *Dao) loadClients() {
  19. var cnt int
  20. for cnt < 3 {
  21. auths, err := d.auths(context.Background())
  22. if err != nil {
  23. log.Error("d.auths() error(%v)", err)
  24. time.Sleep(time.Second)
  25. cnt++
  26. continue
  27. }
  28. if len(auths) == 0 {
  29. return
  30. }
  31. for _, a := range auths {
  32. log.Info("new push client. app(%d) platform(%d)", a.APPID, a.PlatformID)
  33. i := fmtRoundIndex(a.APPID, a.PlatformID)
  34. d.clientsIndex[i] = new(uint32)
  35. switch a.PlatformID {
  36. case model.PlatformIPhone:
  37. d.clientsIPhone[a.APPID] = d.newApnsClients(model.PlatformIPhone, a.Value, a.Key, a.BundleID)
  38. d.clientsLen[i] = len(d.clientsIPhone[a.APPID])
  39. case model.PlatformIPad:
  40. d.clientsIPad[a.APPID] = d.newApnsClients(model.PlatformIPad, a.Value, a.Key, a.BundleID)
  41. d.clientsLen[i] = len(d.clientsIPad[a.APPID])
  42. case model.PlatformHuawei:
  43. cs := d.newHuaweiClients(a.APPID, a.BundleID)
  44. if len(cs) > 0 {
  45. d.clientsHuawei[a.APPID] = cs
  46. d.clientsLen[i] = len(d.clientsHuawei)
  47. }
  48. case model.PlatformOppo:
  49. cs := d.newOppoClients(a.APPID, a.BundleID)
  50. if len(cs) > 0 {
  51. d.clientsOppo[a.APPID] = cs
  52. d.clientsLen[i] = len(d.clientsOppo)
  53. }
  54. case model.PlatformXiaomi:
  55. d.clientsMi[a.APPID] = d.newMiClients(a.Key, a.Value)
  56. d.clientsLen[i] = len(d.clientsMi[a.APPID])
  57. d.clientMiByMids[a.APPID] = d.newMiClientByMids(a.Key, a.Value)
  58. case model.PlatformJpush:
  59. d.clientsJpush[a.APPID] = d.newJpushClients(a.Key, a.Value)
  60. d.clientsLen[i] = len(d.clientsJpush[a.APPID])
  61. case model.PlatformFCM:
  62. d.clientsFCM[a.APPID] = d.newFcmClients(a.Key)
  63. d.clientsLen[i] = len(d.clientsFCM[a.APPID])
  64. default:
  65. log.Warn("unknown platform (%+v)", a)
  66. }
  67. }
  68. return
  69. }
  70. }
  71. func (d *Dao) newMiClients(pkg, auth string) (cs []*mi.Client) {
  72. for i := 0; i < d.c.Android.PoolSize; i++ {
  73. c := mi.NewClient(pkg, auth, time.Duration(d.c.Android.Timeout))
  74. if env.DeployEnv == env.DeployEnvDev {
  75. c.SetDevelopmentURL(mi.RegURL)
  76. } else {
  77. if d.c.Android.MiUseVip == model.SwitchOn {
  78. c.SetVipURL(mi.RegURL)
  79. } else {
  80. c.SetProductionURL(mi.RegURL)
  81. }
  82. }
  83. cs = append(cs, c)
  84. }
  85. return
  86. }
  87. func (d *Dao) newMiClientByMids(pkg, auth string) (c *mi.Client) {
  88. c = mi.NewClient(pkg, auth, time.Duration(d.c.Android.Timeout))
  89. if env.DeployEnv == env.DeployEnvDev {
  90. c.SetDevelopmentURL(mi.AccountURL)
  91. } else {
  92. if d.c.Android.MiUseVip == model.SwitchOn {
  93. c.SetVipURL(mi.RegURL)
  94. } else {
  95. c.SetProductionURL(mi.RegURL)
  96. }
  97. }
  98. return
  99. }
  100. func (d *Dao) newApnsClients(platform int, cert, key, bundleID string) (res []*apns2.Client) {
  101. var (
  102. err error
  103. certificate tls.Certificate
  104. )
  105. if certificate, err = tls.X509KeyPair([]byte(cert), []byte(key)); err != nil {
  106. log.Error("tls.X509KeyPair(%s,%s) error(%v)", cert, key, err)
  107. PromError("client:加载证书")
  108. return
  109. }
  110. poolSize := d.c.Apns.PoolSize
  111. if platform == model.PlatformIPad {
  112. poolSize /= 5 // iPad量少,只有iPhone的不到20%
  113. }
  114. for i := 0; i < poolSize; i++ {
  115. var c *apns2.Client
  116. if env.DeployEnv == env.DeployEnvDev {
  117. if d.c.Apns.Proxy == model.SwitchOn {
  118. c = apns2.NewClientWithProxy(certificate, time.Duration(d.c.Apns.Timeout), d.c.Apns.ProxySocket).Development()
  119. } else {
  120. c = apns2.NewClient(certificate, time.Duration(d.c.Apns.Timeout)).Development()
  121. }
  122. } else {
  123. if d.c.Apns.Proxy == model.SwitchOn {
  124. c = apns2.NewClientWithProxy(certificate, time.Duration(d.c.Apns.Timeout), d.c.Apns.ProxySocket).Production()
  125. } else {
  126. c = apns2.NewClient(certificate, time.Duration(d.c.Apns.Timeout)).Production()
  127. }
  128. }
  129. c.BoundID = bundleID
  130. res = append(res, c)
  131. }
  132. return
  133. }
  134. func (d *Dao) newHuaweiClients(appid int64, pkg string) (cs []*huawei.Client) {
  135. retry := _retry
  136. for retry > 0 {
  137. if d.huaweiAuth[appid] != nil {
  138. break
  139. }
  140. retry--
  141. log.Info("retry huawei auth (%d)", retry)
  142. time.Sleep(3 * time.Second)
  143. }
  144. if d.huaweiAuth[appid] == nil {
  145. log.Error("no huawei auth app(%d)", appid)
  146. return
  147. }
  148. for i := 0; i < d.c.Android.PoolSize; i++ {
  149. c := huawei.NewClient(pkg, d.huaweiAuth[appid], time.Duration(d.c.Android.Timeout))
  150. cs = append(cs, c)
  151. }
  152. return
  153. }
  154. func (d *Dao) newOppoClients(appid int64, activity string) (cs []*oppo.Client) {
  155. retry := _retry
  156. for retry > 0 {
  157. if d.oppoAuth[appid] != nil {
  158. break
  159. }
  160. retry--
  161. log.Info("retry oppo auth (%d)", retry)
  162. time.Sleep(3 * time.Second)
  163. }
  164. if d.oppoAuth[appid] == nil {
  165. log.Error("no oppo auth app(%d)", appid)
  166. return
  167. }
  168. for i := 0; i < d.c.Android.PoolSize; i++ {
  169. c := oppo.NewClient(d.oppoAuth[appid], activity, time.Duration(d.c.Android.Timeout))
  170. cs = append(cs, c)
  171. }
  172. return
  173. }
  174. func (d *Dao) newJpushClients(appKey, secret string) (cs []*jpush.Client) {
  175. for i := 0; i < d.c.Android.PoolSize; i++ {
  176. cs = append(cs, jpush.NewClient(appKey, secret, time.Duration(d.c.Android.Timeout)))
  177. }
  178. return
  179. }
  180. func (d *Dao) newFcmClients(key string) (cs []*fcm.Client) {
  181. for i := 0; i < d.c.Android.PoolSize; i++ {
  182. cs = append(cs, fcm.NewClient(key, time.Duration(d.c.Android.Timeout)))
  183. }
  184. return
  185. }