Skip to content

Commit 3ed49da

Browse files
committed
Cache results to prevent miscalculations
The code assumes that the status bar will only be updated every status-interval seconds In practice status-interval is a maximum value, not a minimum value. So if the status bar is actually updated every second even though the configuration is for 5 seconds all of the calculations are off and the bitrate will be calculated as a fifth of the real rate. See tmux-plugins/tmux-cpu#15 for a similar issue with a different plugin, except that here it is much worse because it actually leads to a WRONG RESULT and not just a faster update. This commit implements @BrainMaestro 's suggestion from that thread.
1 parent 58abb61 commit 3ed49da

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

scripts/download_speed.sh

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ sum_download_speed()
1212
main()
1313
{
1414
# TODO make configurable
15-
#local file=$(get_tmux_option $DOWNLOAD_FILE)
16-
local file=$DOWNLOAD_FILE
17-
local old_val=$(read_file $file)
18-
local new_val=$(sum_download_speed)
15+
if ! is_update_needed $DOWNLOAD_TIME_FILE; then
16+
local vel=$(read_file $DOWNLOAD_CACHE_FILE)
17+
else
18+
local file=$DOWNLOAD_FILE
19+
local old_val=$(read_file $file)
20+
local new_val=$(sum_download_speed)
1921

20-
write_file $file $new_val
21-
local vel=$(get_velocity $new_val $old_val)
22+
write_file $file $new_val
23+
local vel=$(get_velocity $new_val $old_val)
24+
25+
write_file $DOWNLOAD_TIME_FILE $(date +%s)
26+
write_file $DOWNLOAD_CACHE_FILE "$vel"
27+
fi
2228

2329
## Format output
2430
local format=$(get_tmux_option @download_speed_format "%s")

scripts/helpers.sh

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
##
66
DOWNLOAD_FILE="/tmp/tmux_net_speed.download"
77
UPLOAD_FILE="/tmp/tmux_net_speed.upload"
8+
DOWNLOAD_CACHE_FILE="/tmp/tmux_net_speed.download.cache"
9+
UPLOAD_CACHE_FILE="/tmp/tmux_net_speed.upload.cache"
10+
DOWNLOAD_TIME_FILE="/tmp/tmux_net_speed.download.time"
11+
UPLOAD_TIME_FILE="/tmp/tmux_net_speed.upload.time"
812

913
get_tmux_option() {
1014
local option=$1
@@ -24,6 +28,19 @@ set_tmux_option() {
2428
tmux set-option -gq "$option" "$value"
2529
}
2630

31+
is_update_needed()
32+
{
33+
local update_file=$1
34+
35+
local interval=$(get_tmux_option 'status-interval' 5)
36+
local update_time=$(read_file $update_file)
37+
local cur_time=$(date +%s)
38+
if [ $((update_time + interval)) -gt $cur_time ]; then
39+
return 1;
40+
fi;
41+
return 0;
42+
}
43+
2744
get_velocity()
2845
{
2946
local new_value=$1

scripts/upload_speed.sh

+12-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ main()
1313
{
1414
# TODO make configurable
1515
#local upload_file=$(get_tmux_option $UPLOAD_FILE)
16-
local file=$UPLOAD_FILE
17-
local old_val=$(read_file $file)
18-
local new_val=$(sum_upload_speed)
16+
if ! is_update_needed $UPLOAD_TIME_FILE; then
17+
local vel=$(read_file $UPLOAD_CACHE_FILE)
18+
else
19+
local file=$UPLOAD_FILE
20+
local old_val=$(read_file $file)
21+
local new_val=$(sum_upload_speed)
1922

20-
write_file $file $new_val
21-
local vel=$(get_velocity $new_val $old_val)
23+
write_file $file $new_val
24+
local vel=$(get_velocity $new_val $old_val)
25+
26+
write_file $UPLOAD_TIME_FILE $(date +%s)
27+
write_file $UPLOAD_CACHE_FILE "$vel"
28+
fi
2229

2330
## Format output
2431
local format=$(get_tmux_option @upload_speed_format "%s")

0 commit comments

Comments
 (0)