storj/cmd/statreceiver/file.go
JT Olio 362f447d9f
cmd/statreceiver: lua-scriptable stat receiver (#636)
* cmd/statreceiver: lua-scriptable stat receiver

Change-Id: I3ce0fe3f1ef4b1f4f27eed90bac0e91cfecf22d7

* some updates

Change-Id: I7c3485adcda1278fce01ae077b4761b3ddb9fb7a

* more comments

Change-Id: I0bb22993cd934c3d40fc1da80d07e49e686b80dd

* linter fixes

Change-Id: Ied014304ecb9aadcf00a6b66ad28f856a428d150

* catch errors

Change-Id: I6e1920f1fd941e66199b30bc427285c19769fc70

* review feedback

Change-Id: I9d4051851eab18970c5f5ddcf4ff265508e541d3

* errorgroup improvements

Change-Id: I4699dda3022f0485fbb50c9dafe692d3921734ff

* too tricky

the previous thing was better for memory with lots of errors at a time
but https://play.golang.org/p/RweTMRjoSCt is too much of a foot gun

Change-Id: I23f0b3d77dd4288fcc20b3756a7110359576bf44
2018-12-11 11:24:31 -07:00

76 lines
1.4 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"bufio"
"encoding/gob"
"io"
"os"
"sync"
"time"
)
// FileSource reads packets from a file
type FileSource struct {
mtx sync.Mutex
path string
decoder *gob.Decoder
}
// NewFileSource creates a FileSource
func NewFileSource(path string) *FileSource {
return &FileSource{path: path}
}
// Next implements the Source interface
func (f *FileSource) Next() ([]byte, time.Time, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
if f.decoder == nil {
fh, err := os.Open(f.path)
if err != nil {
return nil, time.Time{}, err
}
f.decoder = gob.NewDecoder(bufio.NewReader(fh))
}
var p Packet
err := f.decoder.Decode(&p)
if err != nil {
return nil, time.Time{}, err
}
return p.Data, p.TS, nil
}
// FileDest sends packets to a file for later processing. FileDest preserves
// the timestamps.
type FileDest struct {
mtx sync.Mutex
path string
fh io.Closer
encoder *gob.Encoder
}
// NewFileDest creates a FileDest
func NewFileDest(path string) *FileDest {
return &FileDest{path: path}
}
// Packet implements PacketDest
func (f *FileDest) Packet(data []byte, ts time.Time) error {
f.mtx.Lock()
defer f.mtx.Unlock()
if f.encoder == nil {
fh, err := os.Create(f.path)
if err != nil {
return err
}
f.fh = fh
f.encoder = gob.NewEncoder(bufio.NewWriter(fh))
}
return f.encoder.Encode(Packet{Data: data, TS: ts})
}