Skip to content

Commit

Permalink
make the scanner's buffer be 1MiB, to support longer lines
Browse files Browse the repository at this point in the history
closes #53
  • Loading branch information
aybabtme authored and Antoine Grondin committed Jan 22, 2023
1 parent a7354d3 commit fc1c489
Show file tree
Hide file tree
Showing 58 changed files with 23,047 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func newApp() *cli.App {
handlerOpts := humanlog.HandlerOptionsFrom(*cfg)

log.Print("reading stdin...")
if err := humanlog.Scanner(ctx, os.Stdin, sink, handlerOpts); err != nil {
if err := humanlog.Scan(ctx, os.Stdin, sink, handlerOpts); err != nil {
log.Fatalf("scanning caught an error: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestHarness(t *testing.T) {
t.Fatalf("errs=%v", errs)
}
s := stdiosink.NewStdio(gotw, sinkOpts)
err = Scanner(ctx, bytes.NewReader(input), s, HandlerOptionsFrom(cfg))
err = Scan(ctx, bytes.NewReader(input), s, HandlerOptionsFrom(cfg))
if err != nil {
t.Fatalf("scanning input: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ require (
github.com/kr/logfmt v0.0.0-20210122060352-19f9bcb100e6
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.16
github.com/stretchr/testify v1.8.1
github.com/urfave/cli v1.22.10
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ github.com/cli/safeexec v1.0.1/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
Expand All @@ -35,7 +37,13 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/urfave/cli v1.22.10 h1:p8Fspmz3iTctJstry1PYS3HVdllxnEzTEsgIgtxTrCk=
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -48,6 +56,9 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34 changes: 34 additions & 0 deletions internal/pkg/sink/bufsink/sized.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bufsink

import (
"context"

"github.com/humanlogio/humanlog/internal/pkg/model"
"github.com/humanlogio/humanlog/internal/pkg/sink"
)

type SizedBuffer struct {
Buffered []model.Event
flush sink.BatchSink
}

var _ sink.Sink = (*SizedBuffer)(nil)

func NewSizedBufferedSink(size int, flush sink.BatchSink) *SizedBuffer {
return &SizedBuffer{
Buffered: make([]model.Event, 0, size),
flush: flush,
}
}

func (sn *SizedBuffer) Receive(ctx context.Context, ev *model.Event) error {
sn.Buffered = append(sn.Buffered, *ev)
if len(sn.Buffered) == cap(sn.Buffered) {
if err := sn.flush.ReceiveBatch(ctx, sn.Buffered); err != nil {
sn.Buffered = sn.Buffered[:len(sn.Buffered)-1]
return err
}
sn.Buffered = sn.Buffered[:0]
}
return nil
}
12 changes: 10 additions & 2 deletions internal/pkg/sink/sink.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package sink

import "github.com/humanlogio/humanlog/internal/pkg/model"
import (
"context"

"github.com/humanlogio/humanlog/internal/pkg/model"
)

type Sink interface {
Receive(*model.Event) error
Receive(ctx context.Context, ev *model.Event) error
}

type BatchSink interface {
ReceiveBatch(ctx context.Context, evs []model.Event) error
}
3 changes: 2 additions & 1 deletion internal/pkg/sink/stdiosink/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stdiosink

import (
"bytes"
"context"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -117,7 +118,7 @@ func NewStdio(w io.Writer, opts StdioOpts) *Stdio {
}
}

func (std *Stdio) Receive(ev *model.Event) error {
func (std *Stdio) Receive(ctx context.Context, ev *model.Event) error {
if ev.Structured == nil {
std.lastRaw = true
std.lastLevel = ""
Expand Down
7 changes: 4 additions & 3 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/humanlogio/humanlog/internal/pkg/sink"
)

// Scanner reads JSON-structured lines from src and prettify them onto dst. If
// Scan reads JSON-structured lines from src and prettify them onto dst. If
// the lines aren't JSON-structured, it will simply write them out with no
// prettification.
func Scanner(ctx context.Context, src io.Reader, sink sink.Sink, opts *HandlerOptions) error {
func Scan(ctx context.Context, src io.Reader, sink sink.Sink, opts *HandlerOptions) error {
in := bufio.NewScanner(src)
in.Buffer(make([]byte, 1024*1024), 1024*1024)
in.Split(bufio.ScanLines)

var line uint64
Expand Down Expand Up @@ -58,7 +59,7 @@ func Scanner(ctx context.Context, src io.Reader, sink sink.Sink, opts *HandlerOp
default:
ev.Structured = nil
}
if err := sink.Receive(ev); err != nil {
if err := sink.Receive(ctx, ev); err != nil {
return err
}
select {
Expand Down
24 changes: 24 additions & 0 deletions scanner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package humanlog

import (
"context"
"strings"
"testing"

"github.com/humanlogio/humanlog/internal/pkg/model"
"github.com/humanlogio/humanlog/internal/pkg/sink/bufsink"
"github.com/stretchr/testify/require"
)

func TestScannerLongLine(t *testing.T) {
data := `{"msg":"` + strings.Repeat("a", 1023*1024) + `"}`
ctx := context.Background()
src := strings.NewReader(data)
want := []model.Event{
{Raw: []byte(data), Structured: &model.Structured{Msg: strings.Repeat("a", 1023*1024)}},
}
sink := bufsink.NewSizedBufferedSink(100, nil)
err := Scan(ctx, src, sink, DefaultOptions)
require.NoError(t, err, "got %#v", err)
require.Equal(t, want, sink.Buffered)
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc1c489

Please sign in to comment.