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:
parent
6b252e8024
commit
c2080ef75f
@ -63,6 +63,7 @@ namespace CryptoExchange.Net
|
||||
log = new Log(name);
|
||||
log.UpdateWriters(options.LogWriters);
|
||||
log.Level = options.LogLevel;
|
||||
options.OnLoggingChanged += HandleLogConfigChange;
|
||||
|
||||
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>
|
||||
/// Dispose
|
||||
/// </summary>
|
||||
public virtual void Dispose()
|
||||
{
|
||||
log.Write(LogLevel.Debug, "Disposing client");
|
||||
ClientOptions.OnLoggingChanged -= HandleLogConfigChange;
|
||||
foreach (var client in ApiClients)
|
||||
client.Dispose();
|
||||
}
|
||||
|
@ -36,10 +36,6 @@ namespace CryptoExchange.Net
|
||||
/// </summary>
|
||||
protected internal readonly SemaphoreSlim semaphoreSlim = new(1);
|
||||
/// <summary>
|
||||
/// The max amount of concurrent socket connections
|
||||
/// </summary>
|
||||
protected int MaxSocketConnections { get; set; } = 9999;
|
||||
/// <summary>
|
||||
/// Keep alive interval for websocket connection
|
||||
/// </summary>
|
||||
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;
|
||||
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
|
||||
return result;
|
||||
|
@ -26,6 +26,8 @@ namespace CryptoExchange.Net.Logging
|
||||
/// </summary>
|
||||
public string ClientName { get; set; }
|
||||
|
||||
private readonly object _lock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
@ -42,7 +44,8 @@ namespace CryptoExchange.Net.Logging
|
||||
/// <param name="textWriters"></param>
|
||||
public void UpdateWriters(List<ILogger> textWriters)
|
||||
{
|
||||
writers = textWriters;
|
||||
lock (_lock)
|
||||
writers = textWriters;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -56,16 +59,19 @@ namespace CryptoExchange.Net.Logging
|
||||
return;
|
||||
|
||||
var logMessage = $"{ClientName,-10} | {message}";
|
||||
foreach (var writer in writers.ToList())
|
||||
lock (_lock)
|
||||
{
|
||||
try
|
||||
foreach (var writer in writers)
|
||||
{
|
||||
writer.Log(logLevel, logMessage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// 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());
|
||||
try
|
||||
{
|
||||
writer.Log(logLevel, logMessage);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,15 +14,35 @@ namespace CryptoExchange.Net.Objects
|
||||
/// </summary>
|
||||
public class BaseOptions
|
||||
{
|
||||
internal event Action OnLoggingChanged;
|
||||
|
||||
private LogLevel _logLevel = LogLevel.Information;
|
||||
/// <summary>
|
||||
/// The minimum log level to output
|
||||
/// </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>
|
||||
/// The log writers
|
||||
/// </summary>
|
||||
public List<ILogger> LogWriters { get; set; } = new List<ILogger> { new DebugLogger() };
|
||||
public List<ILogger> LogWriters
|
||||
{
|
||||
get => _logWriters;
|
||||
set
|
||||
{
|
||||
_logWriters = value;
|
||||
OnLoggingChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
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>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
@ -213,12 +238,13 @@ namespace CryptoExchange.Net.Objects
|
||||
SocketResponseTimeout = baseOptions.SocketResponseTimeout;
|
||||
SocketNoDataTimeout = baseOptions.SocketNoDataTimeout;
|
||||
SocketSubscriptionsCombineTarget = baseOptions.SocketSubscriptionsCombineTarget;
|
||||
MaxSocketConnections = baseOptions.MaxSocketConnections;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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}";
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user