Added gateway latency getter to Shard

[ci skip]
This commit is contained in:
Szymon Uglis 2020-06-05 16:11:25 +02:00
parent 76d8f2ae82
commit bcd0c4179b
2 changed files with 24 additions and 1 deletions

View file

@ -57,6 +57,8 @@ class Commander {
this._loggerHandlerFunction = loggerHandlerFunction ?? _defaultLogger;
client.onMessageReceived.listen(_handleMessage);
this._logger.info("Commander ready!");
}
FutureOr<void> _defaultLogger(CommandContext ctx, String commandName, Logger logger) {

View file

@ -68,6 +68,12 @@ class Shard implements Disposable {
/// List of handled guild ids
final List<Snowflake> guilds = [];
/// Gets the latest gateway latency.
///
/// To calculate the gateway latency, nyxx measures the time it takes for Discord to answer the gateway
/// heartbeat packet with a heartbeat ack packet. Note this value is updated each time gateway responses to ack.
Duration get gatewayLatency => _gatewaylatency;
late final Isolate _shardIsolate; // Reference to isolate
late final Stream<dynamic> _receiveStream; // Broadcast stream on which data from isolate is received
late final ReceivePort _receivePort; // Port on which data from isolate is received
@ -79,6 +85,10 @@ class Shard implements Disposable {
late SendPort sendPort;
Duration _gatewaylatency = Duration();
late DateTime _lastHeartbeatSent;
bool _heartbeatAckReceived = false;
/// Isolate
Shard(this.id, this.manager, String gatewayUrl) {
this._receivePort = ReceivePort();
@ -156,7 +166,15 @@ class Shard implements Disposable {
}
void _heartbeat() {
this.send(OPCodes.heartbeat, _sequence);
this.send(OPCodes.heartbeat, _sequence == 0 ? null : _sequence);
this._lastHeartbeatSent = DateTime.now();
if(!this._heartbeatAckReceived) {
manager._logger.warning("Not received previous heartbeat ack");
return;
}
this._heartbeatAckReceived = false;
}
void _handleError(dynamic data) {
@ -214,6 +232,9 @@ class Shard implements Disposable {
switch (msg["op"] as int) {
case OPCodes.heartbeatAck:
this._heartbeatAckReceived = true;
this._gatewaylatency = DateTime.now().difference(this._lastHeartbeatSent);
break;
case OPCodes.hello:
if (this._sessionId == null || !resume) {