Skip to content

Commit

Permalink
refactor: drop ensurewriter wrapper (#42062) (#42174)
Browse files Browse the repository at this point in the history
* refactor: drop ensurewriter wrapper

stdlib properly handles EAGAIN and EINTR so there is no
need for the extra wrapper and duplicate handling

* Update util.go

* Update util.go

* Update util.go

* Update util.go

(cherry picked from commit 95a32ed)

Co-authored-by: kruskall <99559985+kruskall@users.noreply.github.com>
  • Loading branch information
mergify[bot] and kruskall authored Dec 29, 2024
1 parent 910b0a0 commit 614e060
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 110 deletions.
6 changes: 3 additions & 3 deletions libbeat/statestore/backend/memlog/diskstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (s *diskstore) tryOpenLog() error {
ok = true
s.logNeedsTruncate = false
s.logFile = f
s.logBuf = bufio.NewWriterSize(&ensureWriter{s.logFile}, s.bufferSize)
s.logBuf = bufio.NewWriterSize(s.logFile, s.bufferSize)
return nil
}

Expand Down Expand Up @@ -346,7 +346,7 @@ func (s *diskstore) checkpointTmpFile(tempfile string, states map[string]entry)
f.Close()
})

writer := bufio.NewWriterSize(&ensureWriter{f}, s.bufferSize)
writer := bufio.NewWriterSize(f, s.bufferSize)
enc := newJSONEncoder(writer)
if _, err = writer.Write([]byte{'['}); err != nil {
return "", err
Expand Down Expand Up @@ -650,7 +650,7 @@ func writeMetaFile(home string, mode os.FileMode) error {
f.Close()
})

enc := newJSONEncoder(&ensureWriter{f})
enc := newJSONEncoder(f)
err = enc.Encode(storeMeta{
Version: storeVersion,
})
Expand Down
30 changes: 4 additions & 26 deletions libbeat/statestore/backend/memlog/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,13 @@
package memlog

import (
"errors"
"io"
"os"
"runtime"
"syscall"
)

// ensureWriter writes the buffer to the underlying writer
// for as long as w returns a retryable error (e.g. EAGAIN)
// or the input buffer has been exhausted.
//
// XXX: this code was written and tested with go1.13 and go1.14, which does not
// handled EINTR. Some users report EINTR getting triggered more often in
// go1.14 due to changes in the signal handling for implementing
// preemption.
// In future versions EINTR will be handled by go for us.
// See: https://github.com/golang/go/issues/38033
type ensureWriter struct {
w io.Writer
}

// countWriter keeps track of the amount of bytes written over time.
type countWriter struct {
n uint64
Expand All @@ -50,20 +37,10 @@ func (c *countWriter) Write(p []byte) (int, error) {
return n, err
}

func (e *ensureWriter) Write(p []byte) (int, error) {
var N int
for len(p) > 0 {
n, err := e.w.Write(p)
N, p = N+n, p[n:]
if err != nil && !isRetryErr(err) {
return N, err
}
}
return N, nil
}
var _ = isRetryErr

func isRetryErr(err error) bool {
return err == syscall.EINTR || err == syscall.EAGAIN
return errors.Is(err, syscall.EINTR) || errors.Is(err, syscall.EAGAIN)
}

// trySyncPath provides a best-effort fsync on path (directory). The fsync is required by some
Expand All @@ -75,6 +52,7 @@ func trySyncPath(path string) {
return // ignore error, sync on dir must not be necessarily supported by the FS
}
defer f.Close()
//nolint:errcheck // ignore error
syncFile(f)
}

Expand Down
81 changes: 0 additions & 81 deletions libbeat/statestore/backend/memlog/util_test.go

This file was deleted.

0 comments on commit 614e060

Please sign in to comment.