104 lines
4.5 KiB
C#
104 lines
4.5 KiB
C#
using bts.api.core;
|
|
using bts.metrics;
|
|
using mbts;
|
|
using mbts.config;
|
|
using mbts.ui;
|
|
using System;
|
|
using System.ComponentModel.Composition;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace bts.perf {
|
|
[Export, Export(typeof(IShellDaemon))]
|
|
class ShellLatencyDaemon : IShellDaemon, IDisposable {
|
|
readonly ShellLatencyService _service;
|
|
readonly string _hostname;
|
|
readonly string _version;
|
|
readonly string _username;
|
|
IDisposable _subs;
|
|
Ping _pinger = new Ping();
|
|
|
|
[ImportingConstructor]
|
|
public ShellLatencyDaemon(ShellLatencyService service, UserTuple user) {
|
|
_service = service;
|
|
_version = Configuration.get<bts.config.System>().version;
|
|
_username = user.name;
|
|
_hostname = Environment.MachineName;
|
|
|
|
_forever_ping_host(_pinger);
|
|
_subs = Disposable.Create(
|
|
_pinger,
|
|
_service.subscribei_clock().Subscribe(on_tick)
|
|
);
|
|
}
|
|
|
|
void on_tick(Update<TickImmutable, int> update) {
|
|
if (update.added.Any()) {
|
|
var tick = update.added.Last();
|
|
_service.report_async(new Report() {
|
|
HostName = _hostname,
|
|
Version = _version,
|
|
User = _username,
|
|
Timestamp = tick.Timestamp
|
|
});
|
|
}
|
|
}
|
|
|
|
async void _forever_ping_host(Ping pinger) {
|
|
var config = Configuration.get<bts.config.ShellPingOptions>();
|
|
string server_ip = Configuration.get<bts.config.System>().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 received 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() {
|
|
Disposable.dispose(ref _subs);
|
|
}
|
|
}
|
|
} |