sink.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021-2022 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 runtime
  15. import (
  16. context2 "context"
  17. "fmt"
  18. "go.nanomsg.org/mangos/v3"
  19. "github.com/lf-edge/ekuiper/sdk/go/api"
  20. "github.com/lf-edge/ekuiper/sdk/go/connection"
  21. )
  22. type sinkRuntime struct {
  23. s api.Sink
  24. ch connection.DataInChannel
  25. ctx api.StreamContext
  26. cancel context2.CancelFunc
  27. key string
  28. }
  29. func setupSinkRuntime(con *Control, s api.Sink) (*sinkRuntime, error) {
  30. ctx, err := parseContext(con)
  31. if err != nil {
  32. return nil, err
  33. }
  34. err = s.Configure(con.Config)
  35. if err != nil {
  36. return nil, err
  37. }
  38. ch, err := connection.CreateSinkChannel(ctx)
  39. if err != nil {
  40. return nil, err
  41. }
  42. ctx.GetLogger().Info("Setup message pipeline, start listening")
  43. ctx, cancel := ctx.WithCancel()
  44. return &sinkRuntime{
  45. s: s,
  46. ch: ch,
  47. ctx: ctx,
  48. cancel: cancel,
  49. key: fmt.Sprintf("%s_%s_%d_%s", con.Meta.RuleId, con.Meta.OpId, con.Meta.InstanceId, con.SymbolName),
  50. }, nil
  51. }
  52. func (s *sinkRuntime) run() {
  53. err := s.s.Open(s.ctx)
  54. if err != nil {
  55. _ = s.stop()
  56. return
  57. }
  58. for {
  59. var msg []byte
  60. // blocking read, must be interrupted when stopping
  61. // Will be stopped by closing the socket in stop
  62. msg, err = s.ch.Recv()
  63. switch err {
  64. case mangos.ErrClosed:
  65. break
  66. case mangos.ErrRecvTimeout:
  67. continue
  68. case nil:
  69. // do nothing
  70. default:
  71. s.ctx.GetLogger().Errorf("cannot receive from mangos Socket: %s", err.Error())
  72. _ = s.stop()
  73. return
  74. }
  75. err = s.s.Collect(s.ctx, msg)
  76. if err != nil {
  77. s.ctx.GetLogger().Errorf("collect error: %s", err.Error())
  78. _ = s.stop()
  79. return
  80. }
  81. }
  82. }
  83. func (s *sinkRuntime) stop() error {
  84. s.cancel()
  85. _ = s.s.Close(s.ctx)
  86. err := s.ch.Close()
  87. if err != nil {
  88. s.ctx.GetLogger().Info(err)
  89. }
  90. s.ctx.GetLogger().Info("closed sink data channel")
  91. reg.Delete(s.key)
  92. return nil
  93. }
  94. func (s *sinkRuntime) isRunning() bool {
  95. return s.ctx.Err() == nil
  96. }