zmq.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "context"
  17. "fmt"
  18. zmq "github.com/pebbe/zmq4"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. )
  22. type zmqSource struct {
  23. subscriber *zmq.Socket
  24. srv string
  25. topic string
  26. cancel context.CancelFunc
  27. }
  28. func (s *zmqSource) Configure(topic string, props map[string]interface{}) error {
  29. s.topic = topic
  30. srv, ok := props["server"]
  31. if !ok {
  32. return fmt.Errorf("zmq source is missing property server")
  33. }
  34. s.srv = srv.(string)
  35. var err error
  36. s.subscriber, err = zmq.NewSocket(zmq.SUB)
  37. if err != nil {
  38. return fmt.Errorf("zmq source fails to create socket: %v", err)
  39. }
  40. err = s.subscriber.Connect(s.srv)
  41. if err != nil {
  42. return fmt.Errorf("zmq source fails to connect to %s: %v", s.srv, err)
  43. }
  44. return nil
  45. }
  46. func (s *zmqSource) Open(ctx api.StreamContext, consumer chan<- api.SourceTuple, errCh chan<- error) {
  47. logger := ctx.GetLogger()
  48. s.subscriber.SetSubscribe(s.topic)
  49. logger.Debugf("zmq source subscribe to topic %s", s.topic)
  50. exeCtx, cancel := ctx.WithCancel()
  51. s.cancel = cancel
  52. logger.Debugf("start to listen")
  53. for {
  54. msgs, err := s.subscriber.RecvMessageBytes(0)
  55. if err != nil {
  56. id, err := s.subscriber.GetIdentity()
  57. errCh <- fmt.Errorf("zmq source getting message %s error: %v", id, err)
  58. } else {
  59. rcvTime := conf.GetNow()
  60. logger.Debugf("zmq source receive %v", msgs)
  61. var m []byte
  62. for i, msg := range msgs {
  63. if i == 0 && s.topic != "" {
  64. continue
  65. }
  66. m = append(m, msg...)
  67. }
  68. meta := make(map[string]interface{})
  69. if s.topic != "" {
  70. meta["topic"] = string(msgs[0])
  71. }
  72. results, e := ctx.DecodeIntoList(m)
  73. if e != nil {
  74. logger.Errorf("Invalid data format, cannot decode %v with error %s", m, e)
  75. } else {
  76. for _, result := range results {
  77. consumer <- api.NewDefaultSourceTupleWithTime(result, meta, rcvTime)
  78. }
  79. }
  80. }
  81. select {
  82. case <-exeCtx.Done():
  83. logger.Infof("zmq source done")
  84. if s.subscriber != nil {
  85. s.subscriber.Close()
  86. }
  87. return
  88. default:
  89. // do nothing
  90. }
  91. }
  92. }
  93. func (s *zmqSource) Close(ctx api.StreamContext) error {
  94. if s.cancel != nil {
  95. s.cancel()
  96. }
  97. return nil
  98. }
  99. func Zmq() api.Source {
  100. return &zmqSource{}
  101. }