Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed: error: bufio.Scanner token too long #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
Target Resource
Silent bool
TTL bool
MaxBuf int
}

// exit will exit and print the usage.
Expand All @@ -36,7 +37,7 @@ func exit(e error) {

// validate makes sure from and to are Redis URIs or file paths,
// and generates the final Config.
func validate(from, to string, silent, ttl bool) (Config, error) {
func validate(from, to string, silent, ttl bool, maxBuf int) (Config, error) {
cfg := Config{
Source: Resource{
URI: from,
Expand All @@ -46,6 +47,7 @@ func validate(from, to string, silent, ttl bool) (Config, error) {
},
Silent: silent,
TTL: ttl,
MaxBuf: maxBuf,
}

if strings.HasPrefix(from, "redis://") {
Expand Down Expand Up @@ -76,10 +78,10 @@ func Parse() Config {
to := flag.String("to", "", example)
silent := flag.Bool("silent", false, "optional, no verbose output")
ttl := flag.Bool("ttl", false, "optional, enable ttl sync")

maxBuf := flag.Int("buffer", 64*1024, "the size of the buffer used when reading the file, uint:byte")
flag.Parse()

cfg, err := validate(*from, *to, *silent, *ttl)
cfg, err := validate(*from, *to, *silent, *ttl, *maxBuf)
if err != nil {
// we exit here instead of returning so that we can show
// the usage examples in case of an error.
Expand Down
6 changes: 5 additions & 1 deletion pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type File struct {
Bus message.Bus
Silent bool
TTL bool
MaxBuf int
}

// splitCross is a double-cross (✝✝) custom Scanner Split.
Expand All @@ -38,12 +39,13 @@ func splitCross(data []byte, atEOF bool) (advance int, token []byte, err error)
}

// New creates the File struct, to be used for reading/writing.
func New(path string, bus message.Bus, silent, ttl bool) *File {
func New(path string, bus message.Bus, silent, ttl bool, maxBuf int) *File {
return &File{
Path: path,
Bus: bus,
Silent: silent,
TTL: ttl,
MaxBuf: maxBuf,
}
}

Expand All @@ -67,6 +69,8 @@ func (f *File) Read(ctx context.Context) error {

// Scan file, split by double-cross separator
scanner := bufio.NewScanner(d)
buf := make([]byte, 0, bufio.MaxScanTokenSize)
scanner.Buffer(buf, f.MaxBuf)
scanner.Split(splitCross)

// Scan line by line
Expand Down
4 changes: 2 additions & 2 deletions pkg/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Run(cfg config.Config) {
return source.Read(gctx)
})
} else {
source := file.New(cfg.Source.URI, ch, cfg.Silent, cfg.TTL)
source := file.New(cfg.Source.URI, ch, cfg.Silent, cfg.TTL, cfg.MaxBuf)

g.Go(func() error {
return source.Read(gctx)
Expand All @@ -70,7 +70,7 @@ func Run(cfg config.Config) {
return target.Write(gctx)
})
} else {
target := file.New(cfg.Target.URI, ch, cfg.Silent, cfg.TTL)
target := file.New(cfg.Target.URI, ch, cfg.Silent, cfg.TTL, cfg.MaxBuf)

g.Go(func() error {
defer cancel()
Expand Down