message.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package a2s
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/golang/protobuf/proto"
  10. "github.com/nats-io/nats.go"
  11. )
  12. const (
  13. ID_LENGTH = 32
  14. )
  15. type (
  16. Bytes []byte
  17. //观察结果
  18. WatchResponse struct {
  19. lock *sync.RWMutex
  20. cache map[string]chan Bytes
  21. }
  22. //坚听返回队列
  23. WatchNatsQueue struct {
  24. lock *sync.RWMutex
  25. cache map[string]bool
  26. }
  27. )
  28. var (
  29. noice = "abcdefghijklmnopqrstuvwxyz0123456789"
  30. nc *nats.Conn
  31. w = &WatchResponse{new(sync.RWMutex), make(map[string]chan Bytes)}
  32. wnq = &WatchNatsQueue{new(sync.RWMutex), make(map[string]bool)}
  33. )
  34. // id
  35. func id() string {
  36. sb := new(strings.Builder)
  37. for i := 0; i < ID_LENGTH; i++ {
  38. sb.WriteByte(noice[rand.Intn(36)])
  39. }
  40. return sb.String()
  41. }
  42. // Add 方法调用时,添加观察
  43. func (wr *WatchResponse) Add(msgId string, ch chan Bytes) {
  44. wr.lock.Lock()
  45. defer wr.lock.Unlock()
  46. wr.cache[msgId] = ch
  47. }
  48. // Del 超时/成功时,删除观察
  49. func (wr *WatchResponse) Del(msgId string) {
  50. wr.lock.Lock()
  51. defer wr.lock.Unlock()
  52. if v, ok := wr.cache[msgId]; ok {
  53. close(v)
  54. delete(wr.cache, msgId)
  55. }
  56. }
  57. // Put 写入channel,select观察会有响应
  58. func (wr *WatchResponse) Put(msgId string, data Bytes) {
  59. wr.lock.Lock()
  60. defer wr.lock.Unlock()
  61. if v, ok := wr.cache[msgId]; ok {
  62. v <- data
  63. }
  64. }
  65. // Watch
  66. func (wnq *WatchNatsQueue) Watch(topic string) {
  67. wnq.lock.Lock()
  68. defer wnq.lock.Unlock()
  69. if _, ok := wnq.cache[topic]; ok {
  70. return
  71. } else {
  72. wnq.cache[topic] = true
  73. rawTopic := fmt.Sprintf("%s_resp", topic)
  74. nc.QueueSubscribe(rawTopic, rawTopic, func(msg *nats.Msg) {
  75. obj := new(NatsResponse)
  76. err := proto.Unmarshal(msg.Data, obj)
  77. if err == nil {
  78. w.Put(obj.GetMsgId(), obj.GetData())
  79. }
  80. })
  81. }
  82. }
  83. // ConnectNats
  84. func ConnectNats(addr string) error {
  85. var err error
  86. natsAddr := fmt.Sprintf("nats://%s", addr)
  87. log.Println(natsAddr)
  88. nc, err = nats.Connect(natsAddr)
  89. return err
  90. }
  91. // SendMessage2Nats
  92. func SendMessage2Nats(topic string,
  93. timeout int64,
  94. data []byte) (string, <-chan Bytes, error) {
  95. rawTopic := fmt.Sprintf("%s_req", topic)
  96. //
  97. msgId := id()
  98. rawData := &NatsRequest{
  99. MsgId: msgId,
  100. Timestamp: time.Now().Unix(),
  101. Timeout: timeout,
  102. Data: data,
  103. }
  104. //
  105. bs, err := proto.Marshal(rawData)
  106. if err != nil {
  107. return "", nil, err
  108. }
  109. //发布消息到指定队列
  110. err = nc.Publish(rawTopic, bs)
  111. if err != nil {
  112. return "", nil, err
  113. }
  114. ch := make(chan Bytes, 1)
  115. //
  116. wnq.Watch(topic)
  117. //
  118. w.Add(msgId, ch)
  119. return msgId, ch, nil
  120. }