Skip to content

Commit

Permalink
fix read length error: EOF #67
Browse files Browse the repository at this point in the history
  • Loading branch information
chengshiwen committed Aug 6, 2024
1 parent 32a0386 commit 245bcf1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 5 additions & 2 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package backend

import (
"bytes"
"errors"
"io"
"log"
"net/url"
Expand Down Expand Up @@ -215,6 +216,7 @@ func (ib *Backend) RewriteIdle() {
}

func (ib *Backend) RewriteLoop() {
defer ib.SetRewriting(false)
for ib.fb.IsData() {
if !ib.IsRunning() {
return
Expand All @@ -229,13 +231,14 @@ func (ib *Backend) RewriteLoop() {
continue
}
}
ib.SetRewriting(false)
}

func (ib *Backend) Rewrite() (err error) {
b, err := ib.fb.Read()
if err != nil {
log.Print("rewrite read file error: ", err)
if !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
log.Print("rewrite read file error: ", err)
}
return
}
if b == nil {
Expand Down
21 changes: 18 additions & 3 deletions backend/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package backend

import (
"encoding/binary"
"errors"
"io"
"log"
"os"
Expand Down Expand Up @@ -91,6 +92,12 @@ func (fb *FileBackend) IsData() bool {
return fb.dataflag
}

func (fb *FileBackend) setData(flag bool) {
fb.lock.Lock()
defer fb.lock.Unlock()
fb.dataflag = flag
}

func (fb *FileBackend) Read() (p []byte, err error) {
if !fb.IsData() {
return nil, nil
Expand All @@ -99,14 +106,22 @@ func (fb *FileBackend) Read() (p []byte, err error) {

err = binary.Read(fb.consumer, binary.BigEndian, &length)
if err != nil {
log.Print("read length error: ", err)
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
fb.setData(false)
} else {
log.Print("read length error: ", err)
}
return
}
p = make([]byte, length)

_, err = io.ReadFull(fb.consumer, p)
if err != nil {
log.Print("read error: ", err)
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
fb.setData(false)
} else {
log.Print("readfull error: ", err)
}
return
}
return
Expand All @@ -125,7 +140,7 @@ func (fb *FileBackend) RollbackMeta() (err error) {
var offset int64
err = binary.Read(fb.meta, binary.BigEndian, &offset)
if err != nil {
if err != io.EOF {
if !errors.Is(err, io.EOF) {
log.Printf("read meta error: %s %s", fb.filename, err)
}
return
Expand Down

0 comments on commit 245bcf1

Please sign in to comment.