Skip to content

Modern Docker Management, Designed for Everyone

2.1 KB
Raw Blame
package ws

import (
	"context"
	"encoding/json/v2"
	"time"
)

type LogMessage struct {
	Seq         uint64 `json:"seq"`
	Level       string `json:"level,omitempty"`
	Message     string `json:"message"`
	Timestamp   string `json:"timestamp"` // RFC3339(9) string
	Service     string `json:"service,omitempty"`
	ContainerID string `json:"containerId,omitempty"`
}

// ForwardLines forwards plain text lines to the hub.
func ForwardLines(ctx context.Context, hub *Hub, lines <-chan string) {
	defer trackWorkerGoroutine()()

	for {
		select {
		case <-ctx.Done():
			return
		case line, ok := <-lines:
			if !ok {
				return
			}
			hub.Publish(ctx, []byte(line))
		}
	}
}

// ForwardLogJSON sends each LogMessage as its own JSON object frame.
func ForwardLogJSON(ctx context.Context, hub *Hub, logs <-chan LogMessage) {
	defer trackWorkerGoroutine()()

	for {
		select {
		case <-ctx.Done():
			return
		case m, ok := <-logs:
			if !ok {
				return
			}
			if m.Timestamp == "" {
				m.Timestamp = NowRFC3339()
			}
			if b, err := json.Marshal(m); err == nil {
				hub.Publish(ctx, b)
			}
		}
	}
}

// ForwardLogJSONBatched batches log messages into a JSON array frame to reduce frame count.
// Flushes when maxBatch reached or flushInterval elapsed.
func ForwardLogJSONBatched(ctx context.Context, hub *Hub, logs <-chan LogMessage, maxBatch int, flushInterval time.Duration) {
	if maxBatch <= 1 {
		// Delegates to ForwardLogJSON, which does its own accounting.
		ForwardLogJSON(ctx, hub, logs)
		return
	}
	defer trackWorkerGoroutine()()

	ticker := time.NewTicker(flushInterval)
	defer ticker.Stop()

	buf := make([]LogMessage, 0, maxBatch)

	flush := func(publishCtx context.Context) {
		if len(buf) == 0 {
			return
		}
		b, err := json.Marshal(buf)
		if err == nil {
			hub.Publish(publishCtx, b)
		}
		buf = buf[:0]
	}

	for {
		select {
		case <-ctx.Done():
			flush(context.WithoutCancel(ctx))
			return
		case m, ok := <-logs:
			if !ok {
				flush(ctx)
				return
			}
			if m.Timestamp == "" {
				m.Timestamp = NowRFC3339()
			}
			buf = append(buf, m)
			if len(buf) >= maxBatch {
				flush(ctx)
			}
		case <-ticker.C:
			flush(ctx)
		}
	}
}