send_manager.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 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 sink
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. )
  20. type SendManager struct {
  21. lingerInterval int
  22. batchSize int
  23. bufferCh chan map[string]interface{}
  24. buffer []map[string]interface{}
  25. outputCh chan []map[string]interface{}
  26. currIndex int
  27. finished bool
  28. }
  29. func NewSendManager(batchSize, lingerInterval int) (*SendManager, error) {
  30. if batchSize < 1 && lingerInterval < 1 {
  31. return nil, fmt.Errorf("either batchSize or lingerInterval should be larger than 0")
  32. }
  33. sm := &SendManager{
  34. batchSize: batchSize,
  35. lingerInterval: lingerInterval,
  36. }
  37. if batchSize == 0 {
  38. batchSize = 1024
  39. }
  40. sm.buffer = make([]map[string]interface{}, batchSize)
  41. sm.bufferCh = make(chan map[string]interface{})
  42. sm.outputCh = make(chan []map[string]interface{}, 16)
  43. return sm, nil
  44. }
  45. func (sm *SendManager) RecvData(d map[string]interface{}) {
  46. sm.bufferCh <- d
  47. }
  48. func (sm *SendManager) Run(ctx context.Context) {
  49. defer sm.finish()
  50. switch {
  51. case sm.batchSize > 0 && sm.lingerInterval > 0:
  52. sm.runWithTickerAndBatchSize(ctx)
  53. case sm.batchSize > 0 && sm.lingerInterval == 0:
  54. sm.runWithBatchSize(ctx)
  55. case sm.batchSize == 0 && sm.lingerInterval > 0:
  56. sm.runWithTicker(ctx)
  57. }
  58. }
  59. func (sm *SendManager) runWithTicker(ctx context.Context) {
  60. ticker := conf.GetTicker(int64(sm.lingerInterval))
  61. defer ticker.Stop()
  62. for {
  63. select {
  64. case <-ctx.Done():
  65. return
  66. case d := <-sm.bufferCh:
  67. sm.appendDataInBuffer(d, false)
  68. case <-ticker.C:
  69. sm.send()
  70. }
  71. }
  72. }
  73. func (sm *SendManager) runWithBatchSize(ctx context.Context) {
  74. for {
  75. select {
  76. case <-ctx.Done():
  77. return
  78. case d := <-sm.bufferCh:
  79. sm.appendDataInBuffer(d, true)
  80. }
  81. }
  82. }
  83. func (sm *SendManager) runWithTickerAndBatchSize(ctx context.Context) {
  84. ticker := conf.GetTicker(int64(sm.lingerInterval))
  85. defer ticker.Stop()
  86. for {
  87. select {
  88. case <-ctx.Done():
  89. return
  90. case d := <-sm.bufferCh:
  91. sm.appendDataInBuffer(d, true)
  92. case <-ticker.C:
  93. sm.send()
  94. }
  95. }
  96. }
  97. func (sm *SendManager) send() {
  98. if sm.currIndex < 1 {
  99. return
  100. }
  101. list := make([]map[string]interface{}, sm.currIndex)
  102. for i := 0; i < sm.currIndex; i++ {
  103. list[i] = sm.buffer[i]
  104. }
  105. sm.currIndex = 0
  106. sm.outputCh <- list
  107. }
  108. func (sm *SendManager) appendDataInBuffer(d map[string]interface{}, sendData bool) {
  109. if sm.currIndex >= len(sm.buffer) {
  110. // The buffer should be enlarged if the data length is larger than capacity during runWithTicker
  111. sm.buffer = append(sm.buffer, d)
  112. } else {
  113. sm.buffer[sm.currIndex] = d
  114. }
  115. sm.currIndex++
  116. if sendData && sm.currIndex >= sm.batchSize {
  117. sm.send()
  118. }
  119. }
  120. func (sm *SendManager) GetOutputChan() <-chan []map[string]interface{} {
  121. return sm.outputCh
  122. }
  123. func (sm *SendManager) finish() {
  124. sm.finished = true
  125. }