62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package sink
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/lixenwraith/logwisp/internal/core"
|
|
)
|
|
|
|
// Sink represents an output data stream.
|
|
type Sink interface {
|
|
// Capabilities returns a slice of supported Source capabilities
|
|
Capabilities() []core.Capability
|
|
|
|
// Input returns the channel for sending transport events to this sink.
|
|
Input() chan<- core.TransportEvent
|
|
|
|
// Start begins processing transport events.
|
|
Start(ctx context.Context) error
|
|
|
|
// Stop gracefully shuts down the sink.
|
|
Stop()
|
|
|
|
// GetStats returns sink statistics.
|
|
GetStats() SinkStats
|
|
}
|
|
|
|
// SinkStats contains statistics about a sink.
|
|
type SinkStats struct {
|
|
ID string
|
|
Type string
|
|
TotalProcessed uint64
|
|
ActiveConnections int64
|
|
StartTime time.Time
|
|
LastProcessed time.Time
|
|
Details map[string]any
|
|
}
|
|
|
|
// FlushBound is how long a network sink's Stop gives its clients to take
|
|
// what is queued: write_timeout_ms, at most core.SinkFlushTimeout, which a
|
|
// reload also waits out for a client that stopped reading.
|
|
func FlushBound(writeTimeout time.Duration) time.Duration {
|
|
if writeTimeout > 0 && writeTimeout < core.SinkFlushTimeout {
|
|
return writeTimeout
|
|
}
|
|
return core.SinkFlushTimeout
|
|
}
|
|
|
|
// Drain takes the payloads queued in input without waiting: a network sink's
|
|
// last events, which each client then writes on its own.
|
|
func Drain(input <-chan core.TransportEvent) [][]byte {
|
|
var tail [][]byte
|
|
for {
|
|
select {
|
|
case event := <-input:
|
|
tail = append(tail, event.Payload)
|
|
default:
|
|
return tail
|
|
}
|
|
}
|
|
}
|