Skip to content

Commit

Permalink
Optimize readmsg
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode committed Jan 4, 2023
1 parent 73ecb97 commit a878a14
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions node/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func (t *TcpCon) sendMsg() {

func (t *TcpCon) readMsg() {
var (
err error
n int
header = make([]byte, 4)
err error
n int
waittime int64
header = make([]byte, 4)
)
readBuf := readBufPool.Get().([]byte)
defer func() {
Expand All @@ -93,15 +94,22 @@ func (t *TcpCon) readMsg() {
close(t.recv)
readBufPool.Put(readBuf)
}()
for {
for !t.IsClose() {
// read until we get 4 bytes for the magic
_, err = io.ReadFull(t.conn, header)
if err != nil {
if err != io.EOF {
err = fmt.Errorf("initial read error: %v \n", err)
return
}
continue
if err == io.EOF {
waittime++
if waittime >= 10 {
return
}
time.Sleep(time.Second)
continue
}
}

if !bytes.Equal(header, HEAD_FILE) && !bytes.Equal(header, HEAD_FILLER) {
Expand Down

0 comments on commit a878a14

Please sign in to comment.