Skip to content

Commit

Permalink
Merge pull request #62 from honeycombio/ian.window-size
Browse files Browse the repository at this point in the history
Fixes a deadlock occasionally tripped by TestAddSendRaces, and uses the new zstd WithWindowSize option to reduce the pre-allocated encoding buffer from 8MB per CPU to 128KB per CPU. With our typically small payloads this should have a negligible impact on compression ratio.
  • Loading branch information
ianwilkes authored Sep 16, 2019
2 parents 4dccc56 + 9b89e1f commit c8c127c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ workflows:
goversion: "12"
requires:
- setup
- test_libhoney:
goversion: "13"
requires:
- setup
14 changes: 9 additions & 5 deletions libhoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,14 @@ func (e *Event) SendPresampled() (err error) {
e.client.logger.Printf("Send enqueued event: %+v", e)
}
}()
e.lock.RLock()
defer e.lock.RUnlock()

// Lock the sent bool before taking the event lock, to match the order in
// the Add methods.
e.sendLock.Lock()
defer e.sendLock.Unlock()

e.fieldHolder.lock.RLock()
defer e.fieldHolder.lock.RUnlock()
if len(e.data) == 0 {
return errors.New("No metrics added to event. Won't send empty event.")
}
Expand All @@ -788,9 +794,7 @@ func (e *Event) SendPresampled() (err error) {
return errors.New("No Dataset for Honeycomb. Can't send datasetless.")
}

// lock the sent bool and then mark the event as sent. No more changes!
e.sendLock.Lock()
defer e.sendLock.Unlock()
// Mark the event as sent, no more field changes will be applied.
e.sent = true

e.client.ensureTransmission()
Expand Down
11 changes: 9 additions & 2 deletions transmission/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,12 +572,19 @@ func (r *pooledReader) Close() error {
}

// Instantiating a new encoder is expensive, so use a global one.
// The docs say EncodeAll() is concurrency-safe.
// EncodeAll() is concurrency-safe.
var zstdEncoder *zstd.Encoder

func init() {
var err error
zstdEncoder, err = zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(2)))
zstdEncoder, err = zstd.NewWriter(
nil,
// Compression level 2 gives a good balance of speed and compression.
zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(2)),
// zstd allocates 2 * GOMAXPROCS * window size, so use a small window.
// Most honeycomb messages are smaller than this.
zstd.WithWindowSize(1<<16),
)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit c8c127c

Please sign in to comment.