Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-01-23 12:35:57 -08:00
parent cb35625a53
commit 6b42a78c3f
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
2 changed files with 30 additions and 20 deletions

View File

@ -253,7 +253,6 @@ func (bs *BlockSync) sendRequestToPeer(ctx context.Context, p peer.ID, req *Bloc
var res BlockSyncResponse var res BlockSyncResponse
r := incrt.New(s, 50<<10, 5*time.Second) r := incrt.New(s, 50<<10, 5*time.Second)
if err := cborutil.ReadCborRPC(bufio.NewReader(r), &res); err != nil { if err := cborutil.ReadCborRPC(bufio.NewReader(r), &res); err != nil {
bs.syncPeers.logFailure(p, time.Since(start)) bs.syncPeers.logFailure(p, time.Since(start))
return nil, err return nil, err

View File

@ -35,28 +35,39 @@ func New(rd ReaderDeadline, minSpeed int64, maxWait time.Duration) io.Reader {
} }
} }
func (crt *incrt) Read(buf []byte) (n int, err error) { type errNoWait struct{}
func (err errNoWait) Error() string {
return "wait time exceeded"
}
func (err errNoWait) Timeout() bool {
return true
}
func (crt *incrt) Read(buf []byte) (int, error) {
start := now() start := now()
err = crt.rd.SetReadDeadline(start.Add(crt.wait)) if crt.wait == 0 {
if err != nil { return 0, errNoWait{}
return 0, err
} }
defer func() { err := crt.rd.SetReadDeadline(start.Add(crt.wait))
crt.rd.SetReadDeadline(time.Time{}) if err != nil {
if err == nil { log.Warn("unable to set daedline")
dur := now().Sub(start) }
crt.wait -= dur
crt.wait += time.Duration(n) * crt.waitPerByte
if crt.wait < 0 {
crt.wait = 0
}
if crt.wait > crt.maxWait {
crt.wait = crt.maxWait
}
}
}()
n, err = crt.rd.Read(buf) n, err := crt.rd.Read(buf)
crt.rd.SetReadDeadline(time.Time{})
if err == nil {
dur := now().Sub(start)
crt.wait -= dur
crt.wait += time.Duration(n) * crt.waitPerByte
if crt.wait < 0 {
crt.wait = 0
}
if crt.wait > crt.maxWait {
crt.wait = crt.maxWait
}
}
return n, err return n, err
} }