Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend server client ResetStatistics() to reset all client statistics not only channel... #1587

Merged
merged 1 commit into from Nov 26, 2022

Conversation

damikun
Copy link
Contributor

@damikun damikun commented Nov 25, 2022

Currently the class MqttClientStatus => ResetStatistics() resets only channel related stats (BytesSent and BytesReceived) but object itself contains more statistic values..

The pull request extend behaviour to reset all contained statistic as SentPacketsCount,ReceivedPacketsCount, SentApplicationMessagesCount, ReceivedApplicationMessagesCount

So if you call ResetStatistics(); all stats for particular client gets reseted from its stat object....

Original MqttClientStatus object...

    public sealed class MqttClientStatus
    {
        public MqttClientStatus(MqttClient client);

        public MqttSessionStatus Session { get; set; }
        public long SentPacketsCount { get; }
        public long ReceivedPacketsCount { get; }
        public long SentApplicationMessagesCount { get; }
        public long ReceivedApplicationMessagesCount { get; }
        public DateTime LastNonKeepAlivePacketReceivedTimestamp { get; }
        public long BytesSent { get; }
        public DateTime LastPacketSentTimestamp { get; }
        public DateTime ConnectedTimestamp { get; }
        public MqttProtocolVersion ProtocolVersion { get; }
        public string Endpoint { get; }

        public string Id { get; }
        public DateTime LastPacketReceivedTimestamp { get; }
        public long BytesReceived { get; }

        public Task DisconnectAsync();
        public void ResetStatistics();
    }

@dnfadmin
Copy link

dnfadmin commented Nov 25, 2022

CLA assistant check
All CLA requirements met.

Comment on lines +79 to +80
Interlocked.Exchange(ref _sentPacketsCount,0);
Interlocked.Exchange(ref _receivedPacketsCount,0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be set to 1?
I'm asking because of

// Start with 1 because the CONNACK packet is not counted here.
long _receivedPacketsCount = 1;
// Start with 1 because the CONNECT packet is not counted here.
long _sentPacketsCount = 1;

BTW: there should be a space after the ,.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion 0 is fine because the user wants so to reset the entire statistics. The initial 1 is required to have correct values directly after start.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah as @chkr1011 said this is user reset.. 1 is only on init when client open connection...

@@ -73,7 +73,13 @@ public void HandleReceivedPacket(MqttPacket packet)
LastNonKeepAlivePacketReceivedTimestamp = LastPacketReceivedTimestamp;
}
}

public void ResetStatistics(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'd structure it like done in #1590 with an extra struct to hold the values.
And use Volatile.Read/Write instead of Interlocked.Exchange -- see PR for reason.

Copy link
Contributor Author

@damikun damikun Nov 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have a look and update... Inpull i just fit what was used in lib sources...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge the pull request because it fixes the bug in the first place. We can then discuss how it is done and how to optimize it in your pull request.

@chkr1011
Copy link
Collaborator

Thank you for your contribution. Good work 👍

@chkr1011 chkr1011 merged commit b5952a5 into dotnet:master Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants