|
| 1 | +package components |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func EnqueueTimeout(timeout time.Duration) func(*BatchClientConfig) { |
| 11 | + return func(config *BatchClientConfig) { |
| 12 | + config.EnqueueTimeout = timeout |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +func FlushInterval(interval time.Duration) func(*BatchClientConfig) { |
| 17 | + return func(config *BatchClientConfig) { |
| 18 | + config.FlushInterval = interval |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func BatchSize(size int) func(*BatchClientConfig) { |
| 23 | + return func(config *BatchClientConfig) { |
| 24 | + config.BatchSize = size |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +type BatchClientConfig struct { |
| 29 | + EnqueueTimeout time.Duration |
| 30 | + FlushInterval time.Duration |
| 31 | + BatchSize int |
| 32 | +} |
| 33 | + |
| 34 | +var _ APIClient = (*BatchClient)(nil) |
| 35 | + |
| 36 | +type BatchClient struct { |
| 37 | + buffer chan Entry |
| 38 | + client APIClient |
| 39 | + cfg BatchClientConfig |
| 40 | +} |
| 41 | + |
| 42 | +func NewBatchClient(client APIClient, opts ...func(*BatchClientConfig)) *BatchClient { |
| 43 | + cfg := BatchClientConfig{ |
| 44 | + EnqueueTimeout: 5 * time.Second, |
| 45 | + FlushInterval: 5 * time.Second, |
| 46 | + BatchSize: 100, |
| 47 | + } |
| 48 | + for _, opt := range opts { |
| 49 | + opt(&cfg) |
| 50 | + } |
| 51 | + |
| 52 | + b := &BatchClient{ |
| 53 | + buffer: make(chan Entry, cfg.BatchSize*2), |
| 54 | + client: client, |
| 55 | + cfg: cfg, |
| 56 | + } |
| 57 | + |
| 58 | + return b |
| 59 | +} |
| 60 | + |
| 61 | +func (b *BatchClient) IngestLogs(ctx context.Context, entries []Entry) error { |
| 62 | + enqTimeout := time.After(b.cfg.EnqueueTimeout) |
| 63 | + for _, entry := range entries { |
| 64 | + select { |
| 65 | + case b.buffer <- entry: |
| 66 | + // Successfully enqueued. |
| 67 | + case <-ctx.Done(): |
| 68 | + return ctx.Err() |
| 69 | + case <-enqTimeout: |
| 70 | + return errors.New("timeout: buffer is full, cannot enqueue log entry") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func (b *BatchClient) Run(ctx context.Context) error { |
| 78 | + return b.run(ctx) |
| 79 | +} |
| 80 | + |
| 81 | +func (b *BatchClient) run(ctx context.Context) error { |
| 82 | + ticker := time.NewTicker(b.cfg.FlushInterval) |
| 83 | + defer ticker.Stop() |
| 84 | + |
| 85 | + entries := make([]Entry, 0, b.cfg.BatchSize) |
| 86 | + for { |
| 87 | + select { |
| 88 | + case entry := <-b.buffer: |
| 89 | + if len(entry.Message) == 0 { |
| 90 | + continue |
| 91 | + } |
| 92 | + entries = append(entries, entry) |
| 93 | + if len(entries) >= b.cfg.BatchSize { |
| 94 | + b.flush(ctx, entries) |
| 95 | + entries = entries[:0] |
| 96 | + } |
| 97 | + case <-ticker.C: |
| 98 | + b.flush(ctx, entries) |
| 99 | + entries = entries[:0] |
| 100 | + case <-ctx.Done(): |
| 101 | + b.drainBuffer(&entries) |
| 102 | + // Use a new context with timeout for graceful shutdown. |
| 103 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 104 | + defer cancel() |
| 105 | + b.flush(shutdownCtx, entries) |
| 106 | + return ctx.Err() |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func (b *BatchClient) drainBuffer(entries *[]Entry) { |
| 112 | + for { |
| 113 | + select { |
| 114 | + case entry := <-b.buffer: |
| 115 | + if len(entry.Message) > 0 { |
| 116 | + *entries = append(*entries, entry) |
| 117 | + } |
| 118 | + default: |
| 119 | + // Buffer is empty. |
| 120 | + return |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func (b *BatchClient) flush(ctx context.Context, e []Entry) { |
| 126 | + if len(e) == 0 { |
| 127 | + return |
| 128 | + } |
| 129 | + if err := b.client.IngestLogs(ctx, e); err != nil { |
| 130 | + log.Printf("failed to publish logs: %v", err) |
| 131 | + } |
| 132 | +} |
0 commit comments