forked from travis-ci/worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_writer.go
49 lines (44 loc) · 2.05 KB
/
log_writer.go
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
42
43
44
45
46
47
48
49
package worker
import (
"errors"
"io"
"time"
)
var (
// LogWriterTick is how often the buffer should be flushed out and sent to
// travis-logs.
LogWriterTick = 500 * time.Millisecond
// LogChunkSize is a bit of a magic number, calculated like this: The
// maximum Pusher payload is 10 kB (or 10 KiB, who knows, but let's go with
// 10 kB since that is smaller). Looking at the travis-logs source, the
// current message overhead (i.e. the part of the payload that isn't
// the content of the log part) is 42 bytes + the length of the JSON-
// encoded ID and the length of the JSON-encoded sequence number. A 64-
// bit number is up to 20 digits long, so that means (assuming we don't
// go over 64-bit numbers) the overhead is up to 82 bytes. That means
// we can send up to 9918 bytes of content. However, the JSON-encoded
// version of a string can be significantly longer than the raw bytes.
// Worst case that I could find is "<", which with the Go JSON encoder
// becomes "\u003c" (i.e. six bytes long). So, given a string of just
// left angle brackets, the string would become six times as long,
// meaning that the longest string we can take is 1653. We could still
// get errors if we go over 64-bit numbers, but I find the likeliness
// of that happening to both the sequence number, the ID, and us maxing
// out the worst-case logs to be quite unlikely, so I'm willing to live
// with that. --Sarah
LogChunkSize = 1653
// ErrWrotePastMaxLogLength is returned by LogWriter.Write if the write
// caused the number of written bytes to go over the maximum log length.
ErrWrotePastMaxLogLength = errors.New("wrote past max length")
)
// LogWriter is primarily an io.Writer that will send all bytes to travis-logs
// for processing, and also has some utility methods for timeouts and log length
// limiting. Each LogWriter is tied to a given job, and can be gotten by calling
// the LogWriter() method on a Job.
type LogWriter interface {
io.WriteCloser
WriteAndClose([]byte) (int, error)
Timeout() <-chan time.Time
SetMaxLogLength(int)
SetJobStarted()
}