func (l *rlReadWriter) readPacket(b []byte) (n int, err error) {
	// Get the current max bandwidth.
	rl := l.rl
	bps := time.Duration(atomic.LoadInt64(&rl.atomicReadBPS))

	// If bps is 0 there is no limit.
	if bps == 0 {
		return l.ReadWriter.Read(b)
	}

	rl.rmu.Lock()
	// Calculate how long we can take for our read.
	timeForRead := time.Second / bps * time.Duration(len(b))

	// If the readBlock is in the past we reset it to time.Now() +
	// timeForRead. Otherwise we just add to the timestamp.
	wb := rl.readBlock
	if rl.readBlock.After(time.Now()) {
		rl.readBlock = rl.readBlock.Add(timeForRead)
	} else {
		rl.readBlock = time.Now().Add(timeForRead)
	}
	rl.rmu.Unlock()

	// Sleep until it is safe to read.
	select {
	case <-time.After(time.Until(wb)):
	case <-l.ctx.Done():
		return 0, errors.New("read cancelled due to interrupt")
	}
	return l.ReadWriter.Read(b)
}
// readPacket is a helper function that reads up to a single packet worth of
// data.
readPacket is a helper function that reads up to a single packet worth of
data.
// readPacket is a helper function that reads up to a single packet worth of // data .
func (l *rlReadWriter) readPacket(b []byte) (n int, err error) {
	rl := l.rl
	bps := time.Duration(atomic.LoadInt64(&rl.atomicReadBPS))
	if bps == 0 {
		return l.ReadWriter.Read(b)
	}
	rl.rmu.Lock()
	timeForRead := time.Second / bps * time.Duration(len(b))
	wb := rl.readBlock
	if rl.readBlock.After(time.Now()) {
		rl.readBlock = rl.readBlock.Add(timeForRead)
	} else {
		rl.readBlock = time.Now().Add(timeForRead)
	}
	rl.rmu.Unlock()
	select {
	case <-time.After(time.Until(wb)):
	case <-l.ctx.Done():
		return 0, errors.New("read cancelled due to interrupt")
	}
	return l.ReadWriter.Read(b)
}
func ( l * rlReadWriter ) readPacket ( b [ ] byte ) ( n int , err error ) { rl := l . rl bps := time . Duration ( atomic . LoadInt64 ( & rl . atomicReadBPS ) ) if bps == 0 { return l . ReadWriter . Read ( b ) } rl . rmu . Lock ( ) timeForRead := time . Second / bps * time . Duration ( len ( b ) ) wb := rl . readBlock if rl . readBlock . After ( time . Now ( ) ) { rl . readBlock = rl . readBlock . Add ( timeForRead ) } else { rl . readBlock = time . Now ( ) . Add ( timeForRead ) } rl . rmu . Unlock ( ) select { case <- time . After ( time . Until ( wb ) ) : case <- l . ctx . Done ( ) : return 0 , errors . New ( " " ) } return l . ReadWriter . Read ( b ) }
readPacket is a helper function that reads up to a single packet worth of
data.
readPacket is a helper function that reads up to a single packet worth of data .
// Get the current max bandwidth. // If bps is 0 there is no limit. // Calculate how long we can take for our read. // If the readBlock is in the past we reset it to time.Now() + // timeForRead. Otherwise we just add to the timestamp. // Sleep until it is safe to read.