chunkexecutor.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package executors
  2. import "time"
  3. const defaultChunkSize = 1024 * 1024 // 1M
  4. type (
  5. ChunkOption func(options *chunkOptions)
  6. ChunkExecutor struct {
  7. executor *PeriodicalExecutor
  8. container *chunkContainer
  9. }
  10. chunkOptions struct {
  11. chunkSize int
  12. flushInterval time.Duration
  13. }
  14. )
  15. func NewChunkExecutor(execute Execute, opts ...ChunkOption) *ChunkExecutor {
  16. options := newChunkOptions()
  17. for _, opt := range opts {
  18. opt(&options)
  19. }
  20. container := &chunkContainer{
  21. execute: execute,
  22. maxChunkSize: options.chunkSize,
  23. }
  24. executor := &ChunkExecutor{
  25. executor: NewPeriodicalExecutor(options.flushInterval, container),
  26. container: container,
  27. }
  28. return executor
  29. }
  30. func (ce *ChunkExecutor) Add(task interface{}, size int) error {
  31. ce.executor.Add(chunk{
  32. val: task,
  33. size: size,
  34. })
  35. return nil
  36. }
  37. func (ce *ChunkExecutor) Flush() {
  38. ce.executor.Flush()
  39. }
  40. func (ce *ChunkExecutor) Wait() {
  41. ce.executor.Wait()
  42. }
  43. func WithChunkBytes(size int) ChunkOption {
  44. return func(options *chunkOptions) {
  45. options.chunkSize = size
  46. }
  47. }
  48. func WithFlushInterval(duration time.Duration) ChunkOption {
  49. return func(options *chunkOptions) {
  50. options.flushInterval = duration
  51. }
  52. }
  53. func newChunkOptions() chunkOptions {
  54. return chunkOptions{
  55. chunkSize: defaultChunkSize,
  56. flushInterval: defaultFlushInterval,
  57. }
  58. }
  59. type chunkContainer struct {
  60. tasks []interface{}
  61. execute Execute
  62. size int
  63. maxChunkSize int
  64. }
  65. func (bc *chunkContainer) AddTask(task interface{}) bool {
  66. ck := task.(chunk)
  67. bc.tasks = append(bc.tasks, ck.val)
  68. bc.size += ck.size
  69. return bc.size >= bc.maxChunkSize
  70. }
  71. func (bc *chunkContainer) Execute(tasks interface{}) {
  72. vals := tasks.([]interface{})
  73. bc.execute(vals)
  74. }
  75. func (bc *chunkContainer) RemoveAll() interface{} {
  76. tasks := bc.tasks
  77. bc.tasks = nil
  78. bc.size = 0
  79. return tasks
  80. }
  81. type chunk struct {
  82. val interface{}
  83. size int
  84. }