Skip to content

Commit

Permalink
add async io writer
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jun 14, 2024
1 parent e101692 commit 126e49e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
- `AsyncWriter`, *asynchronously & performant*
- `MultiLevelWriter`, *multiple level dispatch*
- `SyslogWriter`, *memory efficient syslog*
file - `JournalWriter`, *linux systemd logging*
- `EventlogWriter`, *windows system event*
* Stdlib Log Adapter
- `Logger.Std`, *transform to std log instances*
- `Logger.Slog`, *transform to log/slog instances*
Expand Down
19 changes: 18 additions & 1 deletion async.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"unsafe"
)

// AsyncWriter is an Writer that writes asynchronously.
// AsyncWriter is a Writer that writes asynchronously.
type AsyncWriter struct {
// Writer specifies the writer of output.
Writer Writer
Expand Down Expand Up @@ -42,6 +42,22 @@ func (w *AsyncWriter) Close() (err error) {

var ErrAsyncWriterFull = errors.New("async writer is full")

var eepool = sync.Pool{
New: func() any {
return new(Entry)
},
}

// Write implements io.Writer.
func (w *AsyncWriter) Write(p []byte) (n int, err error) {
e := eepool.Get().(*Entry)
e.buf = p
n, err = w.Writer.WriteEntry(e)
e.buf = nil
eepool.Put(e)
return
}

// WriteEntry implements Writer.
func (w *AsyncWriter) WriteEntry(e *Entry) (int, error) {
w.once.Do(func() {
Expand Down Expand Up @@ -87,3 +103,4 @@ func (w *AsyncWriter) writer() {
}

var _ Writer = (*AsyncWriter)(nil)
var _ io.Writer = (*AsyncWriter)(nil)
22 changes: 22 additions & 0 deletions async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,25 @@ func BenchmarkAsyncFileWriter(b *testing.B) {
}
})
}

func BenchmarkAsyncIOWriter(b *testing.B) {
logger := TSVLogger{
Writer: &AsyncWriter{
ChannelSize: 4096,
WritevDisabled: false,
DiscardOnFull: false,
Writer: &FileWriter{
Filename: "async_file_test3.log",
},
},
}
defer logger.Writer.(io.Closer).Close()

b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(b *testing.PB) {
for b.Next() {
logger.New().Str("hello file writer").Msg()
}
})
}

0 comments on commit 126e49e

Please sign in to comment.