Skip to content

Commit

Permalink
fix(logger): use bufio reader rather than scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 26, 2021
1 parent a025d5f commit 68d5186
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/logger/cmd/test_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func log(w *bufio.Writer, event []byte, meta []byte, data []byte) {
entry := append(bytes.Join([][]byte{event, meta, data}, []byte("\t")), byte('\n'))
fmt.Printf("log: entry = %s", string(entry))
fmt.Printf("log: entry = '%s'\n", string(entry))
_, err := w.Write(entry)
if err != nil {
panic(err)
Expand Down
18 changes: 13 additions & 5 deletions src/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"database/sql"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand All @@ -18,6 +19,7 @@ import (
const (
QUEUE_SIZE int = 1024
BUFFER_LEN int = 128
PIPE_BUF int = 512 // POSIX
)

func main() {
Expand All @@ -27,11 +29,17 @@ func main() {

go worker(db, queue)

r := bufio.NewReaderSize(fh, PIPE_BUF)

for {
scanner := bufio.NewScanner(fh)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
enqueue(queue, scanner.Bytes())
line, err := r.ReadBytes('\n')
for err == nil {
enqueue(queue, line)
line, err = r.ReadBytes('\n')
}
if err != io.EOF {
fmt.Println(err)
return
}
}
}
Expand Down Expand Up @@ -120,7 +128,7 @@ func parse(entry []byte) ([]byte, []byte, []byte) {
// NOTE: Tab characters are not allowed to appear unescaped in JSON.
split := bytes.Split(entry, []byte("\t"))
if len(split) != 3 {
panic(fmt.Sprintf("parse: failed to split entry: %s", string(entry)))
panic(fmt.Sprintf("parse: failed to split entry: '%s'\n", string(entry)))
}
return split[0], split[1], split[2]
}
Expand Down

0 comments on commit 68d5186

Please sign in to comment.