41 lines
750 B
Go
41 lines
750 B
Go
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||
|
// Use of this source code is governed by the Drone Non-Commercial License
|
||
|
// that can be found in the LICENSE file.
|
||
|
|
||
|
package livelog
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"github.com/drone/drone/core"
|
||
|
)
|
||
|
|
||
|
type subscriber struct {
|
||
|
sync.Mutex
|
||
|
|
||
|
handler chan *core.Line
|
||
|
closec chan struct{}
|
||
|
closed bool
|
||
|
}
|
||
|
|
||
|
func (s *subscriber) publish(line *core.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()
|
||
|
}
|