diff --git a/src/client/spark/Daemons/ShellLatencyDaemon.cs b/src/client/spark/Daemons/ShellLatencyDaemon.cs index 827e80d49..0f98fb35a 100644 --- a/src/client/spark/Daemons/ShellLatencyDaemon.cs +++ b/src/client/spark/Daemons/ShellLatencyDaemon.cs @@ -1,11 +1,13 @@ using bts.api.core; using bts.metrics; +using mbts; using mbts.config; using mbts.ui; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.Linq; +using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; @@ -18,6 +20,7 @@ namespace bts.perf { readonly string _version; readonly string _username; IDisposable _subs; + Ping _pinger = new Ping(); [ImportingConstructor] public ShellLatencyDaemon(ShellLatencyService service, [Import("username")] string user) { @@ -25,7 +28,12 @@ namespace bts.perf { _version = Configuration.get().version; _username = user; _hostname = Environment.MachineName; - _subs = _service.subscribei_clock().Subscribe(on_tick); + + _forever_ping_host(_pinger); + _subs = Disposable.Create( + _pinger, + _service.subscribei_clock().Subscribe(on_tick) + ); } void on_tick(Update update) { @@ -40,8 +48,60 @@ namespace bts.perf { } } + async void _forever_ping_host(Ping pinger) { + var config = Configuration.get(); + string server_ip = Configuration.get().config_ip; + int delay = config.workloop_milliseconds_interval; + int _current_missed_ping_count = 0; + + if (config.shell_ping_is_enabled && pinger != null) { + while (true) { + try { + await Task.Delay(delay); + var response = await pinger.SendPingAsync(server_ip, config.timeout_milliseconds_threshold); + if (response.Status == IPStatus.Success) { + _current_missed_ping_count = 0; + if (response.RoundtripTime > config.logging_milliseconds_threshold) { + Log.Ui.warn($"Ping response not recieved in a timely manner:\n" + + $"\tAddress:\t{response.Address}\n" + + $"\tRoundTrip:\t{response.RoundtripTime} ms"); + } + // potentially reset delay to initial work period (no-op otherwise) + delay = config.workloop_milliseconds_interval; + } else { + _current_missed_ping_count++; + delay = _get_exp_delay(_current_missed_ping_count, config.workloop_milliseconds_interval, config.maximum_backoff_milliseconds); + Log.Ui.warn($"Ping was not successful. Ping response: {response.Status}. Missed ping count: {_current_missed_ping_count}. Backing off for {delay} milliseconds."); + } + } catch (PingException e) { + _current_missed_ping_count++; + delay = _get_exp_delay(_current_missed_ping_count, config.workloop_milliseconds_interval, config.maximum_backoff_milliseconds); + Log.Ui.error($"Ping exception. {e}\nBacking off for {delay} milliseconds."); + } + } // end while + } + + // since we're returning milliseconds here up to a 1 minute delay + // we don't need to go crazy with the power, else we will get + // an ArgumentOutOfRangeExecption. + // Int.MaxValue = 2^31, so we need to be below that + // Also, even if we're under the 2^31 int.maxvalue, we don't want to be multed over it + // so that is why we have checked() here. + int _get_exp_delay(int retry_num, int min_interval, int max_delay) { + // add some jitter up to half the max delay so we don't + // have a ping spike from all clients at once + var jitter_amount = new Random().Next(max_delay / 2); + int power_factor = (int)Math.Pow(2, Math.Min(retry_num, 32) - 1); + try { + return Math.Min(checked(min_interval * power_factor + jitter_amount), max_delay + jitter_amount); + } catch (OverflowException) { + return max_delay + jitter_amount; + } + } + } + public void Dispose() { - throw new NotImplementedException(); + Disposable.dispose(ref _subs); } } } diff --git a/src/client/spark/view/Spark/SparkWindow.cs b/src/client/spark/view/Spark/SparkWindow.cs index a65b7d983..07774e7c7 100644 --- a/src/client/spark/view/Spark/SparkWindow.cs +++ b/src/client/spark/view/Spark/SparkWindow.cs @@ -19,6 +19,7 @@ namespace bts.spark { TITLE_PANEL_HEIGHT = 30, COLUMN_HEADER_HEIGHT = 27, SPARK_MENU_ITEM_HEIGHT = 45, + GRID_FOOTER_HEIGHT = 30, ABSOLUTE_MIN_HEIGHT = 230; protected static readonly TimeSpan CONTENT_MEASURE_WAIT_PERIOD = TimeSpan.FromSeconds(0.3); @@ -60,7 +61,7 @@ namespace bts.spark { public abstract UIElement Content { get; } public virtual IEnumerable Commands => Enumerable.Empty(); public virtual double MinWidth => 400; - public virtual double MinHeight => Math.Max(ABSOLUTE_MIN_HEIGHT, TITLE_PANEL_HEIGHT + COLUMN_HEADER_HEIGHT + Commands.Count() * SPARK_MENU_ITEM_HEIGHT); + public virtual double MinHeight => Math.Max(ABSOLUTE_MIN_HEIGHT, TITLE_PANEL_HEIGHT + COLUMN_HEADER_HEIGHT + GRID_FOOTER_HEIGHT + Commands.Count() * SPARK_MENU_ITEM_HEIGHT); public ShellWindowUI GetContent() => new ShellWindowUI(new SparkWindowView(_bus, Commands, Content)); diff --git a/src/lib/config/protos/UI.proto b/src/lib/config/protos/UI.proto index 489b55328..55ab87a4b 100644 --- a/src/lib/config/protos/UI.proto +++ b/src/lib/config/protos/UI.proto @@ -56,4 +56,14 @@ message LadderKnobs { optional uint32 seconds_to_recenter = 3 [default = 300]; optional uint32 max_open_ladders = 4 [default = 75]; optional uint32 security_timeout_secs = 5 [default = 60]; +} + +message ShellPingOptions { + optional bool shell_ping_is_enabled = 1 [default = true]; + optional int32 workloop_milliseconds_interval = 2 [default = 5000]; + // 4000 is default Windows ping timeout as specified here: + // https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490968(v=technet.10) + optional int32 timeout_milliseconds_threshold = 3 [default = 4000]; + optional int32 logging_milliseconds_threshold = 4 [default = 25]; + optional int32 maximum_backoff_milliseconds = 5 [default = 60000]; } \ No newline at end of file