Beispiel #1
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _httpClient?.Dispose();
                RateLimiter?.Dispose();
            }

            _disposed = true;
        }
Beispiel #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="timestampProvider">The timestamp provider.</param>
        /// <param name="rateLimiter">The rate limiter (auto configured).</param>
        /// <param name="options">The options.</param>
        /// <param name="logger">The logger.</param>
        internal BinanceHttpClient(ITimestampProvider timestampProvider = null, IApiRateLimiter rateLimiter = null, IOptions <BinanceApiOptions> options = null, ILogger <BinanceHttpClient> logger = null)
            : base(logger)
        {
            TimestampProvider = timestampProvider ?? new TimestampProvider();
            RateLimiter       = rateLimiter ?? new ApiRateLimiter();
            var apiOptions = options?.Value ?? new BinanceApiOptions();

            DefaultRecvWindow = apiOptions.RecvWindowDefault ?? default;

            TimestampProvider.TimestampOffsetRefreshPeriod = TimeSpan.FromMinutes(apiOptions.TimestampOffsetRefreshPeriodMinutes);

            try
            {
                // Configure request rate limiter.
                RateLimiter.Configure(TimeSpan.FromMinutes(apiOptions.RequestRateLimit.DurationMinutes), apiOptions.RequestRateLimit.Count);
                // Configure request burst rate limiter.
                RateLimiter.Configure(TimeSpan.FromSeconds(apiOptions.RequestRateLimit.BurstDurationSeconds), apiOptions.RequestRateLimit.BurstCount);
            }
            catch (Exception e)
            {
                var message = $"{nameof(BinanceHttpClient)}: Failed to configure request rate limiter.";
                Logger?.LogError(e, message);
                throw new BinanceApiException(message, e);
            }

            var uri = new Uri(EndpointUrl);

            try
            {
                _httpClient = new HttpClient
                {
                    BaseAddress = uri,
                    Timeout     = TimeSpan.FromSeconds(apiOptions.HttpClientTimeoutDefaultSeconds)
                };
            }
            catch (Exception e)
            {
                var message = $"{nameof(BinanceHttpClient)}: Failed to create HttpClient.";
                Logger?.LogError(e, message);
                throw new BinanceApiException(message, e);
            }

            if (apiOptions.ServicePointManagerConnectionLeaseTimeoutMilliseconds > 0)
            {
                try
                {
                    // FIX: Singleton HttpClient doesn't respect DNS changes.
                    // https://github.com/dotnet/corefx/issues/11224
                    var sp = ServicePointManager.FindServicePoint(uri);
                    sp.ConnectionLeaseTimeout = apiOptions.ServicePointManagerConnectionLeaseTimeoutMilliseconds;
                }
                catch (Exception e)
                {
                    var message = $"{nameof(BinanceHttpClient)}: Failed to set {nameof(ServicePointManager)}.ConnectionLeaseTimeout.";
                    Logger?.LogError(e, message);
                    throw new BinanceApiException(message, e);
                }
            }

            try
            {
                var version = GetType().Assembly.GetName().Version;

                var versionString = $"{version.Major}.{version.Minor}.{version.Build}{(version.Revision > 0 ? $".{version.Revision}" : string.Empty)}";

                _httpClient.DefaultRequestHeaders.Add("User-Agent", $"Binance/{versionString} (.NET; +https://github.com/sonvister/Binance)");
            }
            catch (Exception e)
            {
                var message = $"{nameof(BinanceHttpClient)}: Failed to set User-Agent.";
                Logger?.LogError(e, message);
                throw new BinanceApiException(message, e);
            }
        }