Skip to content

Commit

Permalink
fix: flush gzip writes to disk periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Sep 6, 2024
1 parent a2aa3d4 commit dad0621
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions pkg/sessions/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sessions
import (
"compress/gzip"
"fmt"
"io"
"os"

P "github.com/cfoust/cy/pkg/io/protocol"
Expand All @@ -23,9 +24,38 @@ type SessionWriter interface {
Close() error
}

type flushWriter struct {
gz *gzip.Writer
bytesWritten int
}

var _ io.Writer = (*flushWriter)(nil)

const (
// GZIP_BUFFER_SIZE is the number of bytes to write before flushing
// to disk. Terminal sessions are surprisingly small, so flushing
// every 128k feels safe, but we may need to play with this.
GZIP_BUFFER_SIZE = 128 * 1024
)

func (f *flushWriter) Write(p []byte) (n int, err error) {
n, err = f.gz.Write(p)
f.bytesWritten += n

if f.bytesWritten > GZIP_BUFFER_SIZE {
if err := f.gz.Flush(); err != nil {
return 0, err
}
f.bytesWritten = 0
}

return
}

type sessionWriter struct {
file *os.File
gz *gzip.Writer
flush *flushWriter
handle *codec.MsgpackHandle
encoder *codec.Encoder
}
Expand Down Expand Up @@ -54,10 +84,6 @@ func (s *sessionWriter) Write(event Event) error {
}

func (s *sessionWriter) Close() error {
if err := s.gz.Flush(); err != nil {
return err
}

if err := s.gz.Close(); err != nil {
return err
}
Expand All @@ -73,11 +99,13 @@ func Create(filename string) (SessionWriter, error) {

handle := new(codec.MsgpackHandle)
gz := gzip.NewWriter(f)
encoder := codec.NewEncoder(gz, handle)
flush := &flushWriter{gz: gz}
encoder := codec.NewEncoder(flush, handle)

writer := sessionWriter{
handle: handle,
gz: gz,
flush: flush,
encoder: encoder,
file: f,
}
Expand Down

0 comments on commit dad0621

Please sign in to comment.