send_manager.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(sm.lingerInterval)
  61. defer ticker.Stop()
  62. for {
  63. select {
  64. case <-ctx.Done():
  65. return
  66. case d := <-sm.bufferCh:
  67. sm.buffer[sm.currIndex] = d
  68. sm.currIndex++
  69. case <-ticker.C:
  70. sm.send()
  71. }
  72. }
  73. }
  74. func (sm *SendManager) runWithBatchSize(ctx context.Context) {
  75. for {
  76. select {
  77. case <-ctx.Done():
  78. return
  79. case d := <-sm.bufferCh:
  80. sm.buffer[sm.currIndex] = d
  81. sm.currIndex++
  82. if sm.currIndex >= sm.batchSize {
  83. sm.send()
  84. }
  85. }
  86. }
  87. }
  88. func (sm *SendManager) runWithTickerAndBatchSize(ctx context.Context) {
  89. ticker := conf.GetTicker(sm.lingerInterval)
  90. defer ticker.Stop()
  91. for {
  92. select {
  93. case <-ctx.Done():
  94. return
  95. case d := <-sm.bufferCh:
  96. sm.buffer[sm.currIndex] = d
  97. sm.currIndex++
  98. if sm.currIndex >= sm.batchSize {
  99. sm.send()
  100. }
  101. case <-ticker.C:
  102. sm.send()
  103. }
  104. }
  105. }
  106. func (sm *SendManager) send() {
  107. if sm.currIndex < 1 {
  108. return
  109. }
  110. list := make([]map[string]interface{}, sm.currIndex)
  111. for i := 0; i < sm.currIndex; i++ {
  112. list[i] = sm.buffer[i]
  113. }
  114. sm.currIndex = 0
  115. sm.outputCh <- list
  116. }
  117. func (sm *SendManager) GetOutputChan() <-chan []map[string]interface{} {
  118. return sm.outputCh
  119. }
  120. func (sm *SendManager) finish() {
  121. sm.finished = true
  122. }