File size: 992 Bytes
857a91b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Package logs provides a bounded log buffer and an event broadcast bus used
// for streaming job lifecycle events over SSE.
package logs

import "sync"

// Ring is a byte-bounded buffer that keeps the most recent log output. Once the
// configured limit is exceeded, the oldest bytes are dropped.
type Ring struct {
	mu    sync.Mutex
	buf   []byte
	limit int
}

// NewRing creates a Ring that retains at most limit bytes.
func NewRing(limit int) *Ring {
	if limit <= 0 {
		limit = 1024 * 1024
	}
	return &Ring{limit: limit}
}

// Write appends p, dropping the oldest bytes if the limit is exceeded. It never
// returns an error so it satisfies io.Writer.
func (r *Ring) Write(p []byte) (int, error) {
	r.mu.Lock()
	defer r.mu.Unlock()
	r.buf = append(r.buf, p...)
	if len(r.buf) > r.limit {
		r.buf = r.buf[len(r.buf)-r.limit:]
	}
	return len(p), nil
}

// String returns the retained log contents.
func (r *Ring) String() string {
	r.mu.Lock()
	defer r.mu.Unlock()
	return string(r.buf)
}