/// <summary>获取Storage连接 /// </summary> public IConnection GetStorageConnection(ConnectionAddress connectionAddress) { if (!_storageConnectionPools.TryGetValue(connectionAddress, out IConnectionPool connectionPool)) { lock (_storageSyncObject) { if (!_storageConnectionPools.TryGetValue(connectionAddress, out connectionPool)) { var connectionPoolOption = new ConnectionPoolOption() { ConnectionAddress = connectionAddress, ConnectionLifeTime = _option.ConnectionLifeTime, ConnectionConcurrentThread = _option.ConnectionConcurrentThread, MaxConnection = _option.StorageMaxConnection, ScanTimeoutConnectionInterval = _option.ScanTimeoutConnectionInterval }; connectionPool = _connectionPoolFactory.CreateConnectionPool(connectionPoolOption); if (!_storageConnectionPools.TryAdd(connectionAddress, connectionPool)) { _logger.LogWarning("Fail to add connection pool to storage connection pools! ConnectionAddress:{0}", connectionAddress); } } } } if (connectionPool == null) { throw new ArgumentException($"Can't find any connection pools for {connectionAddress}"); } return(connectionPool.GetConnection()); }
/// <summary>Get tracker connection /// </summary> public IConnection GetTrackerConnection() { var rd = new Random(); var index = rd.Next(_trackerConnectionAddresses.Count); var connectionAddress = _trackerConnectionAddresses[index]; if (!_trackerConnectionPools.TryGetValue(connectionAddress, out IConnectionPool connectionPool)) { lock (_trackerSyncObject) { if (!_trackerConnectionPools.TryGetValue(connectionAddress, out connectionPool)) { var connectionPoolOption = new ConnectionPoolOption() { ConnectionAddress = connectionAddress, ConnectionLifeTime = _option.ConnectionLifeTime, ConnectionConcurrentThread = _option.ConnectionConcurrentThread, MaxConnection = _option.TrackerMaxConnection, ScanTimeoutConnectionInterval = _option.ScanTimeoutConnectionInterval }; connectionPool = _connectionPoolFactory.CreateConnectionPool(connectionPoolOption); if (!_trackerConnectionPools.TryAdd(connectionAddress, connectionPool)) { _logger.LogWarning("Fail to add connection pool to tracker connection pools! ConnectionAddress:{0}", connectionAddress); } } } } return(connectionPool.GetConnection()); }
public IConnectionPool CreateConnectionPool(ConnectionPoolOption option) { using (var scope = _serviceProvider.CreateScope()) { var injectOption = scope.ServiceProvider.GetService <ConnectionPoolOption>(); injectOption.ConnectionAddress = option.ConnectionAddress; injectOption.MaxConnection = option.MaxConnection; injectOption.ConnectionConcurrentThread = option.ConnectionConcurrentThread; injectOption.ConnectionLifeTime = option.ConnectionLifeTime; injectOption.ScanTimeoutConnectionInterval = option.ScanTimeoutConnectionInterval; return(scope.ServiceProvider.GetService <IConnectionPool>()); } }
public DefaultConnectionPool(ILogger <DefaultConnectionPool> logger, ConnectionPoolOption option, IConnectionBuilder connectionFactory) { _logger = logger; _option = option; _connectionFactory = connectionFactory; Name = Guid.NewGuid().ToString(); _connectionCount = 0; _cts = new CancellationTokenSource(); _maxConnectionCount = _option.MaxConnection; _semaphoreSlim = new SemaphoreSlim(_option.ConnectionConcurrentThread); _connectionStack = new ConcurrentStack <IConnection>(); _connectionDict = new ConcurrentDictionary <string, IConnection>(); }