token-store.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package channel
  2. import (
  3. "message-pusher/common"
  4. "message-pusher/model"
  5. "sync"
  6. "time"
  7. )
  8. type TokenStoreItem interface {
  9. Key() string
  10. Token() string
  11. Refresh()
  12. IsFilled() bool
  13. }
  14. type tokenStore struct {
  15. Map map[string]*TokenStoreItem
  16. Mutex sync.RWMutex
  17. ExpirationSeconds int
  18. }
  19. var s tokenStore
  20. func TokenStoreInit() {
  21. s.Map = make(map[string]*TokenStoreItem)
  22. s.ExpirationSeconds = 2 * 60 * 60
  23. go func() {
  24. users, err := model.GetAllUsersWithSecrets()
  25. if err != nil {
  26. common.FatalLog(err.Error())
  27. }
  28. var items []TokenStoreItem
  29. for _, user := range users {
  30. if user.WeChatTestAccountId != "" {
  31. item := &WeChatTestAccountTokenStoreItem{
  32. AppID: user.WeChatTestAccountId,
  33. AppSecret: user.WeChatTestAccountSecret,
  34. }
  35. items = append(items, item)
  36. }
  37. if user.WeChatCorpAccountId != "" {
  38. item := &WeChatCorpAccountTokenStoreItem{
  39. CorpId: user.WeChatCorpAccountId,
  40. AgentSecret: user.WeChatCorpAccountAgentSecret,
  41. AgentId: user.WeChatCorpAccountAgentId,
  42. }
  43. items = append(items, item)
  44. }
  45. }
  46. s.Mutex.RLock()
  47. for i := range items {
  48. // s.Map[item.Key()] = &item // This is wrong, you are getting the address of a local variable!
  49. s.Map[items[i].Key()] = &items[i]
  50. }
  51. s.Mutex.RUnlock()
  52. for {
  53. s.Mutex.RLock()
  54. var tmpMap = make(map[string]*TokenStoreItem)
  55. for k, v := range s.Map {
  56. tmpMap[k] = v
  57. }
  58. s.Mutex.RUnlock()
  59. for k := range tmpMap {
  60. (*tmpMap[k]).Refresh()
  61. }
  62. s.Mutex.RLock()
  63. // we shouldn't directly replace the old map with the new map, cause the old map's keys may already change
  64. for k := range s.Map {
  65. v, okay := tmpMap[k]
  66. if okay {
  67. s.Map[k] = v
  68. }
  69. }
  70. sleepDuration := common.Max(s.ExpirationSeconds, 60)
  71. s.Mutex.RUnlock()
  72. time.Sleep(time.Duration(sleepDuration) * time.Second)
  73. }
  74. }()
  75. }
  76. // TokenStoreAddItem It's okay to add an incomplete item.
  77. func TokenStoreAddItem(item TokenStoreItem) {
  78. if !item.IsFilled() {
  79. return
  80. }
  81. item.Refresh()
  82. s.Mutex.RLock()
  83. s.Map[item.Key()] = &item
  84. s.Mutex.RUnlock()
  85. }
  86. func TokenStoreRemoveItem(item TokenStoreItem) {
  87. s.Mutex.RLock()
  88. delete(s.Map, item.Key())
  89. s.Mutex.RUnlock()
  90. }
  91. func TokenStoreAddUser(user *model.User) {
  92. testItem := WeChatTestAccountTokenStoreItem{
  93. AppID: user.WeChatTestAccountId,
  94. AppSecret: user.WeChatTestAccountSecret,
  95. }
  96. TokenStoreAddItem(&testItem)
  97. corpItem := WeChatCorpAccountTokenStoreItem{
  98. CorpId: user.WeChatCorpAccountId,
  99. AgentSecret: user.WeChatCorpAccountAgentSecret,
  100. AgentId: user.WeChatCorpAccountAgentId,
  101. }
  102. TokenStoreAddItem(&corpItem)
  103. }
  104. func TokenStoreUpdateUser(cleanUser *model.User, originUser *model.User) {
  105. // WeChat Test Account
  106. // The fields of cleanUser may be incomplete!
  107. if cleanUser.WeChatTestAccountId == originUser.WeChatTestAccountId {
  108. cleanUser.WeChatTestAccountId = ""
  109. }
  110. if cleanUser.WeChatTestAccountSecret == originUser.WeChatTestAccountSecret {
  111. cleanUser.WeChatTestAccountSecret = ""
  112. }
  113. // This means the user updated those fields.
  114. if cleanUser.WeChatTestAccountId != "" || cleanUser.WeChatTestAccountSecret != "" {
  115. oldWeChatTestAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
  116. AppID: originUser.WeChatTestAccountId,
  117. AppSecret: originUser.WeChatTestAccountSecret,
  118. }
  119. // Yeah, it's a deep copy.
  120. newWeChatTestAccountTokenStoreItem := oldWeChatTestAccountTokenStoreItem
  121. if cleanUser.WeChatTestAccountId != "" {
  122. newWeChatTestAccountTokenStoreItem.AppID = cleanUser.WeChatTestAccountId
  123. }
  124. if cleanUser.WeChatTestAccountSecret != "" {
  125. newWeChatTestAccountTokenStoreItem.AppSecret = cleanUser.WeChatTestAccountSecret
  126. }
  127. if !oldWeChatTestAccountTokenStoreItem.IsShared() {
  128. TokenStoreRemoveItem(&oldWeChatTestAccountTokenStoreItem)
  129. }
  130. TokenStoreAddItem(&newWeChatTestAccountTokenStoreItem)
  131. }
  132. // WeChat Corp Account
  133. if cleanUser.WeChatCorpAccountId == originUser.WeChatCorpAccountId {
  134. cleanUser.WeChatCorpAccountId = ""
  135. }
  136. if cleanUser.WeChatCorpAccountAgentId == originUser.WeChatCorpAccountAgentId {
  137. cleanUser.WeChatCorpAccountAgentId = ""
  138. }
  139. if cleanUser.WeChatCorpAccountAgentSecret == originUser.WeChatCorpAccountAgentSecret {
  140. cleanUser.WeChatCorpAccountAgentSecret = ""
  141. }
  142. if cleanUser.WeChatCorpAccountId != "" || cleanUser.WeChatCorpAccountAgentId != "" || cleanUser.WeChatCorpAccountAgentSecret != "" {
  143. oldWeChatCorpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
  144. CorpId: originUser.WeChatCorpAccountId,
  145. AgentSecret: originUser.WeChatCorpAccountAgentSecret,
  146. AgentId: originUser.WeChatCorpAccountAgentId,
  147. }
  148. newWeChatCorpAccountTokenStoreItem := oldWeChatCorpAccountTokenStoreItem
  149. if cleanUser.WeChatCorpAccountId != "" {
  150. newWeChatCorpAccountTokenStoreItem.CorpId = cleanUser.WeChatCorpAccountId
  151. }
  152. if cleanUser.WeChatCorpAccountAgentSecret != "" {
  153. newWeChatCorpAccountTokenStoreItem.AgentSecret = cleanUser.WeChatCorpAccountAgentSecret
  154. }
  155. if cleanUser.WeChatCorpAccountAgentId != "" {
  156. newWeChatCorpAccountTokenStoreItem.AgentId = cleanUser.WeChatCorpAccountAgentId
  157. }
  158. if !oldWeChatCorpAccountTokenStoreItem.IsShared() {
  159. TokenStoreRemoveItem(&oldWeChatCorpAccountTokenStoreItem)
  160. }
  161. TokenStoreAddItem(&newWeChatCorpAccountTokenStoreItem)
  162. }
  163. }
  164. // TokenStoreRemoveUser
  165. // user must be filled.
  166. // It's okay to delete a user that don't have an item here.
  167. func TokenStoreRemoveUser(user *model.User) {
  168. testAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
  169. AppID: user.WeChatTestAccountId,
  170. AppSecret: user.WeChatTestAccountSecret,
  171. }
  172. if !testAccountTokenStoreItem.IsShared() {
  173. TokenStoreRemoveItem(&testAccountTokenStoreItem)
  174. }
  175. corpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
  176. CorpId: user.WeChatCorpAccountId,
  177. AgentSecret: user.WeChatCorpAccountAgentSecret,
  178. AgentId: user.WeChatCorpAccountAgentId,
  179. }
  180. if !corpAccountTokenStoreItem.IsShared() {
  181. TokenStoreRemoveItem(&corpAccountTokenStoreItem)
  182. }
  183. }
  184. func TokenStoreGetToken(key string) string {
  185. s.Mutex.RLock()
  186. defer s.Mutex.RUnlock()
  187. item, ok := s.Map[key]
  188. if ok {
  189. return (*item).Token()
  190. }
  191. common.SysError("token for " + key + " is blank!")
  192. return ""
  193. }