Skip to content

Commit

Permalink
Added websocket reconnect ratelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Jun 12, 2024
1 parent 60857e1 commit fc6530f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions Mexc.Net/Clients/SpotApi/MexcSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal MexcSocketClientSpotApi(ILogger logger, MexcSocketOptions options) :
base(logger, options.Environment.SpotSocketAddress, options, options.SpotOptions)
{
AddSystemSubscription(new MexcErrorSubscription(_logger));
RateLimiter = MexcExchange.RateLimiter.SpotSocket;
}

#endregion
Expand Down
15 changes: 15 additions & 0 deletions Mexc.Net/Mexc.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion Mexc.Net/MexcExchange.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace Mexc.Net
using CryptoExchange.Net.RateLimiting;
using CryptoExchange.Net.RateLimiting.Filters;
using CryptoExchange.Net.RateLimiting.Guards;
using CryptoExchange.Net.RateLimiting.Interfaces;

namespace Mexc.Net
{
/// <summary>
/// Mexc exchange information and configuration
Expand All @@ -21,5 +26,38 @@ public static class MexcExchange
public static string[] ApiDocsUrl { get; } = new[] {
"https://mexcdevelop.github.io/apidocs/spot_v3_en/#introduction"
};

/// <summary>
/// Rate limiter configuration for the Mexc API
/// </summary>
public static MexcRateLimiters RateLimiter { get; } = new MexcRateLimiters();
}

/// <summary>
/// Rate limiter configuration for the Mexc API
/// </summary>
public class MexcRateLimiters
{
/// <summary>
/// Event for when a rate limit is triggered
/// </summary>
public event Action<RateLimitEvent> RateLimitTriggered;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
internal MexcRateLimiters()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
Initialize();
}

private void Initialize()
{
SpotSocket = new RateLimitGate("Spot Socket")
.AddGuard(new RateLimitGuard(RateLimitGuard.PerHost, new IGuardFilter[] { new LimitItemTypeFilter(RateLimitItemType.Connection) }, 100, TimeSpan.FromSeconds(1), RateLimitWindowType.Fixed)); // 100 connections per second per host

SpotSocket.RateLimitTriggered += (x) => RateLimitTriggered?.Invoke(x);
}

internal IRateLimitGate SpotSocket { get; private set; }
}
}

0 comments on commit fc6530f

Please sign in to comment.