1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Made MaxSocketConnections a setting, added support for changing log settings after creating client

This commit is contained in:
JKorf 2022-06-11 13:31:39 +02:00
parent 6b252e8024
commit c2080ef75f
4 changed files with 56 additions and 17 deletions

View File

@ -63,6 +63,7 @@ namespace CryptoExchange.Net
log = new Log(name); log = new Log(name);
log.UpdateWriters(options.LogWriters); log.UpdateWriters(options.LogWriters);
log.Level = options.LogLevel; log.Level = options.LogLevel;
options.OnLoggingChanged += HandleLogConfigChange;
ClientOptions = options; ClientOptions = options;
@ -282,12 +283,22 @@ namespace CryptoExchange.Net
} }
} }
/// <summary>
/// Handle a change in the client options log config
/// </summary>
private void HandleLogConfigChange()
{
log.UpdateWriters(ClientOptions.LogWriters);
log.Level = ClientOptions.LogLevel;
}
/// <summary> /// <summary>
/// Dispose /// Dispose
/// </summary> /// </summary>
public virtual void Dispose() public virtual void Dispose()
{ {
log.Write(LogLevel.Debug, "Disposing client"); log.Write(LogLevel.Debug, "Disposing client");
ClientOptions.OnLoggingChanged -= HandleLogConfigChange;
foreach (var client in ApiClients) foreach (var client in ApiClients)
client.Dispose(); client.Dispose();
} }

View File

@ -36,10 +36,6 @@ namespace CryptoExchange.Net
/// </summary> /// </summary>
protected internal readonly SemaphoreSlim semaphoreSlim = new(1); protected internal readonly SemaphoreSlim semaphoreSlim = new(1);
/// <summary> /// <summary>
/// The max amount of concurrent socket connections
/// </summary>
protected int MaxSocketConnections { get; set; } = 9999;
/// <summary>
/// Keep alive interval for websocket connection /// Keep alive interval for websocket connection
/// </summary> /// </summary>
protected TimeSpan KeepAliveInterval { get; set; } = TimeSpan.FromSeconds(10); protected TimeSpan KeepAliveInterval { get; set; } = TimeSpan.FromSeconds(10);
@ -520,7 +516,7 @@ namespace CryptoExchange.Net
var result = socketResult.Equals(default(KeyValuePair<int, SocketConnection>)) ? null : socketResult.Value; var result = socketResult.Equals(default(KeyValuePair<int, SocketConnection>)) ? null : socketResult.Value;
if (result != null) if (result != null)
{ {
if (result.SubscriptionCount < ClientOptions.SocketSubscriptionsCombineTarget || (socketConnections.Count >= MaxSocketConnections && socketConnections.All(s => s.Value.SubscriptionCount >= ClientOptions.SocketSubscriptionsCombineTarget))) if (result.SubscriptionCount < ClientOptions.SocketSubscriptionsCombineTarget || (socketConnections.Count >= ClientOptions.MaxSocketConnections && socketConnections.All(s => s.Value.SubscriptionCount >= ClientOptions.SocketSubscriptionsCombineTarget)))
{ {
// Use existing socket if it has less than target connections OR it has the least connections and we can't make new // Use existing socket if it has less than target connections OR it has the least connections and we can't make new
return result; return result;

View File

@ -26,6 +26,8 @@ namespace CryptoExchange.Net.Logging
/// </summary> /// </summary>
public string ClientName { get; set; } public string ClientName { get; set; }
private readonly object _lock = new object();
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>
@ -42,7 +44,8 @@ namespace CryptoExchange.Net.Logging
/// <param name="textWriters"></param> /// <param name="textWriters"></param>
public void UpdateWriters(List<ILogger> textWriters) public void UpdateWriters(List<ILogger> textWriters)
{ {
writers = textWriters; lock (_lock)
writers = textWriters;
} }
/// <summary> /// <summary>
@ -56,16 +59,19 @@ namespace CryptoExchange.Net.Logging
return; return;
var logMessage = $"{ClientName,-10} | {message}"; var logMessage = $"{ClientName,-10} | {message}";
foreach (var writer in writers.ToList()) lock (_lock)
{ {
try foreach (var writer in writers)
{ {
writer.Log(logLevel, logMessage); try
} {
catch (Exception e) writer.Log(logLevel, logMessage);
{ }
// Can't write to the logging so where else to output.. catch (Exception e)
Trace.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | Warning | Failed to write log to writer {writer.GetType()}: " + e.ToLogString()); {
// Can't write to the logging so where else to output..
Trace.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | Warning | Failed to write log to writer {writer.GetType()}: " + e.ToLogString());
}
} }
} }
} }

View File

@ -14,15 +14,35 @@ namespace CryptoExchange.Net.Objects
/// </summary> /// </summary>
public class BaseOptions public class BaseOptions
{ {
internal event Action OnLoggingChanged;
private LogLevel _logLevel = LogLevel.Information;
/// <summary> /// <summary>
/// The minimum log level to output /// The minimum log level to output
/// </summary> /// </summary>
public LogLevel LogLevel { get; set; } = LogLevel.Information; public LogLevel LogLevel
{
get => _logLevel;
set
{
_logLevel = value;
OnLoggingChanged?.Invoke();
}
}
private List<ILogger> _logWriters = new List<ILogger> { new DebugLogger() };
/// <summary> /// <summary>
/// The log writers /// The log writers
/// </summary> /// </summary>
public List<ILogger> LogWriters { get; set; } = new List<ILogger> { new DebugLogger() }; public List<ILogger> LogWriters
{
get => _logWriters;
set
{
_logWriters = value;
OnLoggingChanged?.Invoke();
}
}
/// <summary> /// <summary>
/// If true, the CallResult and DataEvent objects will also include the originally received json data in the OriginalData property /// If true, the CallResult and DataEvent objects will also include the originally received json data in the OriginalData property
@ -189,6 +209,11 @@ namespace CryptoExchange.Net.Objects
/// </summary> /// </summary>
public int? SocketSubscriptionsCombineTarget { get; set; } public int? SocketSubscriptionsCombineTarget { get; set; }
/// <summary>
/// The max amount of connections to make to the server. Can be used for API's which only allow a certain number of connections. Changing this to a high value might cause issues.
/// </summary>
public int? MaxSocketConnections { get; set; }
/// <summary> /// <summary>
/// ctor /// ctor
/// </summary> /// </summary>
@ -213,12 +238,13 @@ namespace CryptoExchange.Net.Objects
SocketResponseTimeout = baseOptions.SocketResponseTimeout; SocketResponseTimeout = baseOptions.SocketResponseTimeout;
SocketNoDataTimeout = baseOptions.SocketNoDataTimeout; SocketNoDataTimeout = baseOptions.SocketNoDataTimeout;
SocketSubscriptionsCombineTarget = baseOptions.SocketSubscriptionsCombineTarget; SocketSubscriptionsCombineTarget = baseOptions.SocketSubscriptionsCombineTarget;
MaxSocketConnections = baseOptions.MaxSocketConnections;
} }
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {
return $"{base.ToString()}, AutoReconnect: {AutoReconnect}, ReconnectInterval: {ReconnectInterval}, MaxReconnectTries: {MaxReconnectTries}, MaxResubscribeTries: {MaxResubscribeTries}, MaxConcurrentResubscriptionsPerSocket: {MaxConcurrentResubscriptionsPerSocket}, SocketResponseTimeout: {SocketResponseTimeout:c}, SocketNoDataTimeout: {SocketNoDataTimeout}, SocketSubscriptionsCombineTarget: {SocketSubscriptionsCombineTarget}"; return $"{base.ToString()}, AutoReconnect: {AutoReconnect}, ReconnectInterval: {ReconnectInterval}, MaxReconnectTries: {MaxReconnectTries}, MaxResubscribeTries: {MaxResubscribeTries}, MaxConcurrentResubscriptionsPerSocket: {MaxConcurrentResubscriptionsPerSocket}, SocketResponseTimeout: {SocketResponseTimeout:c}, SocketNoDataTimeout: {SocketNoDataTimeout}, SocketSubscriptionsCombineTarget: {SocketSubscriptionsCombineTarget}, MaxSocketConnections: {MaxSocketConnections}";
} }
} }