fix(go): stream durable spool batches
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -23,9 +24,9 @@ type DurableSink struct {
|
||||
delegate Sink
|
||||
dir string
|
||||
|
||||
mu sync.Mutex
|
||||
seq uint64
|
||||
rawPending map[string]struct{}
|
||||
mu sync.Mutex
|
||||
seq uint64
|
||||
rawPending map[string]struct{}
|
||||
replayBatchSize int
|
||||
}
|
||||
|
||||
@@ -232,13 +233,59 @@ func readDurableRecord(path string) (durableRecord, error) {
|
||||
}
|
||||
|
||||
func durableFiles(dir string, limit int) ([]string, error) {
|
||||
if limit > 0 {
|
||||
handle, err := os.Open(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer handle.Close()
|
||||
return durableFilesFromReader(dir, limit, handle)
|
||||
}
|
||||
files, err := filepath.Glob(filepath.Join(dir, "*.json"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Strings(files)
|
||||
if limit > 0 && len(files) > limit {
|
||||
files = files[:limit]
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
type durableNameReader interface {
|
||||
Readdirnames(int) ([]string, error)
|
||||
}
|
||||
|
||||
func durableFilesFromReader(dir string, limit int, reader durableNameReader) ([]string, error) {
|
||||
if limit <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
files := make([]string, 0, limit)
|
||||
for len(files) < limit {
|
||||
names, err := reader.Readdirnames(limit - len(files))
|
||||
for _, name := range names {
|
||||
if !strings.HasSuffix(name, ".json") {
|
||||
continue
|
||||
}
|
||||
files = append(files, filepath.Join(dir, name))
|
||||
if len(files) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if errorsIsEOF(err) {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if len(names) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
sort.Strings(files)
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func errorsIsEOF(err error) bool {
|
||||
return err == io.EOF
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user