drone/livelog/sub.go
2023-08-17 12:15:08 +01:00

39 lines
729 B
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package livelog
import (
"sync"
)
type subscriber struct {
sync.Mutex
handler chan *Line
closec chan struct{}
closed bool
}
func (s *subscriber) publish(line *Line) {
select {
case <-s.closec:
case s.handler <- line:
default:
// lines are sent on a buffered channel. If there
// is a slow consumer that is not processing events,
// the buffered channel will fill and newer messages
// are ignored.
}
}
func (s *subscriber) close() {
s.Lock()
if !s.closed {
close(s.closec)
s.closed = true
}
s.Unlock()
}