-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Roman Khimov <roman@nspcc.ru>
- Loading branch information
1 parent
279e52e
commit 62f5a64
Showing
4 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dbft | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const rttLength = 7 * 10 | ||
|
||
type rtt struct { | ||
times [rttLength]time.Duration | ||
idx int | ||
avg time.Duration | ||
} | ||
|
||
func (r *rtt) addTime(new time.Duration) { | ||
var old = r.times[r.idx] | ||
|
||
if old != 0 { | ||
new = min(new, 2*old) // Too long delays should be normalized, we don't want to overshoot. | ||
} | ||
|
||
r.avg = r.avg + (new-old)/time.Duration(len(r.times)) | ||
r.avg = max(0, r.avg) // Can't be less than zero. | ||
r.times[r.idx] = new | ||
r.idx = (r.idx + 1) % len(r.times) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters