Documentation
¶
Overview ¶
Package logstream provides a thread-safe log broadcasting system with a ring buffer for log history and SSE-based streaming to subscribers.
Index ¶
Constants ¶
const ( // DefaultBufferSize is the default number of log lines to keep in the ring buffer. DefaultBufferSize = 1000 // DefaultSubscriberBuffer is the buffer size for each subscriber's channel. DefaultSubscriberBuffer = 100 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub manages log broadcasting to subscribers with a ring buffer for history.
func NewHub ¶
NewHub creates a new Hub with the specified buffer size. If size <= 0, DefaultBufferSize is used.
func (*Hub) History ¶
History returns the last n lines from the ring buffer. If n <= 0 or n > count, returns all available lines.
func (*Hub) Subscribe ¶
func (h *Hub) Subscribe(ctx context.Context) *Subscriber
Subscribe creates a new subscriber that receives log lines. The returned Subscriber should be unsubscribed when done.
func (*Hub) SubscriberCount ¶
SubscriberCount returns the current number of subscribers.
func (*Hub) Unsubscribe ¶
func (h *Hub) Unsubscribe(sub *Subscriber)
Unsubscribe removes a subscriber and closes its channel.
type Subscriber ¶
type Subscriber struct {
// contains filtered or unexported fields
}
Subscriber represents a log stream subscriber with a buffered channel.
func (*Subscriber) Channel ¶
func (s *Subscriber) Channel() <-chan string
Channel returns the subscriber's log line channel.
func (*Subscriber) Done ¶
func (s *Subscriber) Done() <-chan struct{}
Done returns the subscriber's context done channel.
type SwitchableWriter ¶
type SwitchableWriter struct {
// contains filtered or unexported fields
}
SwitchableWriter is an io.Writer that allows atomic swapping of the underlying writer. It also captures complete log lines and broadcasts them to a Hub.
func NewSwitchableWriter ¶
func NewSwitchableWriter(initial io.Writer, hub *Hub) *SwitchableWriter
NewSwitchableWriter creates a new SwitchableWriter with the given initial writer and hub.
func (*SwitchableWriter) GetHub ¶
func (sw *SwitchableWriter) GetHub() *Hub
GetHub returns the Hub associated with this writer.
func (*SwitchableWriter) Swap ¶
Swap atomically replaces the underlying writer and returns the old closer (if any). The caller is responsible for closing the returned closer after the swap.
func (*SwitchableWriter) Write ¶
func (sw *SwitchableWriter) Write(p []byte) (n int, err error)
Write writes data to the underlying writer and captures complete lines for the hub. Note: We don't wrap the error here since SwitchableWriter implements io.Writer and callers expect standard Write semantics.