From df6b3e351cd0b6be507769ab8d9e0f84d3a14772 Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Sat, 11 Dec 2021 07:51:10 -0700 Subject: [PATCH 1/3] Use a single rate gate per type of api --- src/ExchangeSharp/API/Common/BaseAPI.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ExchangeSharp/API/Common/BaseAPI.cs b/src/ExchangeSharp/API/Common/BaseAPI.cs index f5f46fcf..68b3e170 100644 --- a/src/ExchangeSharp/API/Common/BaseAPI.cs +++ b/src/ExchangeSharp/API/Common/BaseAPI.cs @@ -11,6 +11,7 @@ The above copyright notice and this permission notice shall be included in all c */ #nullable enable using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -176,10 +177,16 @@ public IAPIRequestMaker RequestMaker /// public System.Security.SecureString? Passphrase { get; set; } + private static readonly ConcurrentDictionary rateLimiters = new ConcurrentDictionary(); /// /// Rate limiter - set this to a new limit if you are seeing your ip get blocked by the API + /// One rate limiter is created per type of api /// - public RateGate RateLimit { get; set; } = new RateGate(5, TimeSpan.FromSeconds(15.0d)); + public RateGate RateLimit + { + get => rateLimiters.GetOrAdd(GetType(), v => new RateGate(5, TimeSpan.FromSeconds(15.0d))); + set => rateLimiters[GetType()] = value; + } /// /// Default request method From 58fbb1a03782409a8199e0d16360df4abe885c08 Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Sat, 11 Dec 2021 08:10:07 -0700 Subject: [PATCH 2/3] Store rate gate to limit dictionary calls --- src/ExchangeSharp/API/Common/BaseAPI.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ExchangeSharp/API/Common/BaseAPI.cs b/src/ExchangeSharp/API/Common/BaseAPI.cs index 68b3e170..f1112367 100644 --- a/src/ExchangeSharp/API/Common/BaseAPI.cs +++ b/src/ExchangeSharp/API/Common/BaseAPI.cs @@ -178,14 +178,15 @@ public IAPIRequestMaker RequestMaker public System.Security.SecureString? Passphrase { get; set; } private static readonly ConcurrentDictionary rateLimiters = new ConcurrentDictionary(); + private RateGate rateGate; /// /// Rate limiter - set this to a new limit if you are seeing your ip get blocked by the API /// One rate limiter is created per type of api /// public RateGate RateLimit { - get => rateLimiters.GetOrAdd(GetType(), v => new RateGate(5, TimeSpan.FromSeconds(15.0d))); - set => rateLimiters[GetType()] = value; + get => rateGate ??= rateLimiters.GetOrAdd(GetType(), v => new RateGate(5, TimeSpan.FromSeconds(15.0d))); + set => rateLimiters[GetType()] = rateGate = value; } /// From 0290eadf3f8a0bb46fb0e1aa0df6a625a6839a50 Mon Sep 17 00:00:00 2001 From: Jeff Johnson Date: Sat, 11 Dec 2021 11:42:11 -0700 Subject: [PATCH 3/3] Upgrade to net 6, make get type case insensitive --- .../ExchangeSharpWinForms/ExchangeSharpWinForms.csproj | 6 ++---- src/ExchangeSharp.Forms/ExchangeSharp.Forms.csproj | 7 ++++++- src/ExchangeSharp.Forms/Forms/PlotForm.Designer.cs | 2 +- src/ExchangeSharp.Forms/Forms/PlotForm.cs | 2 +- src/ExchangeSharp/API/Exchanges/_Base/ExchangeName.cs | 2 +- src/ExchangeSharpConsole/ExchangeSharpConsole.csproj | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/ExchangeSharpWinForms/ExchangeSharpWinForms.csproj b/examples/ExchangeSharpWinForms/ExchangeSharpWinForms.csproj index 5ac68abc..980b84cc 100644 --- a/examples/ExchangeSharpWinForms/ExchangeSharpWinForms.csproj +++ b/examples/ExchangeSharpWinForms/ExchangeSharpWinForms.csproj @@ -2,7 +2,7 @@ {571623F9-1652-4669-8E17-A6FAD1426181} WinExe - net472 + net6.0-windows true ExchangeSharpWinForms ExchangeSharpWinForms @@ -21,9 +21,7 @@ - - Form - + MainForm.cs diff --git a/src/ExchangeSharp.Forms/ExchangeSharp.Forms.csproj b/src/ExchangeSharp.Forms/ExchangeSharp.Forms.csproj index 1be8a213..f6be83ea 100644 --- a/src/ExchangeSharp.Forms/ExchangeSharp.Forms.csproj +++ b/src/ExchangeSharp.Forms/ExchangeSharp.Forms.csproj @@ -1,10 +1,11 @@  - net472 + net6.0-windows HAS_WINDOWS_FORMS en 8 + true @@ -19,6 +20,10 @@ + + + + diff --git a/src/ExchangeSharp.Forms/Forms/PlotForm.Designer.cs b/src/ExchangeSharp.Forms/Forms/PlotForm.Designer.cs index 97fe6ebf..39f8aad1 100644 --- a/src/ExchangeSharp.Forms/Forms/PlotForm.Designer.cs +++ b/src/ExchangeSharp.Forms/Forms/PlotForm.Designer.cs @@ -1,4 +1,4 @@ -#if HAS_WINDOWS_FORMS +#if HAS_WINDOWS_FORMS namespace ExchangeSharp { diff --git a/src/ExchangeSharp.Forms/Forms/PlotForm.cs b/src/ExchangeSharp.Forms/Forms/PlotForm.cs index 8a1f7add..5ebc81e2 100644 --- a/src/ExchangeSharp.Forms/Forms/PlotForm.cs +++ b/src/ExchangeSharp.Forms/Forms/PlotForm.cs @@ -1,4 +1,4 @@ -/* +/* MIT LICENSE Copyright 2017 Digital Ruby, LLC - http://www.digitalruby.com diff --git a/src/ExchangeSharp/API/Exchanges/_Base/ExchangeName.cs b/src/ExchangeSharp/API/Exchanges/_Base/ExchangeName.cs index 117069ba..059248a7 100644 --- a/src/ExchangeSharp/API/Exchanges/_Base/ExchangeName.cs +++ b/src/ExchangeSharp/API/Exchanges/_Base/ExchangeName.cs @@ -47,7 +47,7 @@ internal static Type GetExchangeType(string exchangeName) try { // make sure we have a valid type for the name - Type type = Type.GetType($"ExchangeSharp.Exchange{exchangeName}API"); + Type type = Type.GetType($"ExchangeSharp.Exchange{exchangeName}API", true, true); // we had better have a type sub-classing from ExchangeAPI if (type is null || !type.IsSubclassOf(exchangeApiType)) diff --git a/src/ExchangeSharpConsole/ExchangeSharpConsole.csproj b/src/ExchangeSharpConsole/ExchangeSharpConsole.csproj index bc8bc23c..6583ea10 100644 --- a/src/ExchangeSharpConsole/ExchangeSharpConsole.csproj +++ b/src/ExchangeSharpConsole/ExchangeSharpConsole.csproj @@ -3,7 +3,7 @@ Exe exchange-sharp - netcoreapp3.1 + net6.0 en 8 0.9.1