public LongRunningConnectionFactory(TransportConfig config, KeyspaceConfig keyspaceConfig, ILogger logger, IInstrumentation instrumentation) { _config = config; _keyspaceConfig = keyspaceConfig; _logger = logger; _instrumentation = instrumentation; }
public LongRunningConnection(IPAddress address, TransportConfig config, KeyspaceConfig keyspaceConfig, ILogger logger, IInstrumentation instrumentation) { try { for (byte streamId = 0; streamId < MAX_STREAMID; ++streamId) { _availableStreamIds.Push(streamId); } _config = config; _keyspaceConfig = keyspaceConfig; _logger = logger; _instrumentation = instrumentation; Endpoint = address; DefaultConsistencyLevel = config.DefaultConsistencyLevel; DefaultExecutionFlags = config.DefaultExecutionFlags; _tcpClient = new TcpClient { ReceiveTimeout = _config.ReceiveTimeout, SendTimeout = _config.SendTimeout, NoDelay = true, LingerState = { Enabled = true, LingerTime = 0 }, }; _tcpClient.Connect(address, _config.Port); _socket = _tcpClient.Client; _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, _config.KeepAlive); if (_config.KeepAlive && 0 != _config.KeepAliveTime) { SetTcpKeepAlive(_socket, _config.KeepAliveTime, 1000); } _pushResult = _config.ReceiveBuffering ? (Action <QueryInfo, IFrameReader, bool>)((qi, fr, a) => Task.Factory.StartNew(() => PushResult(qi, fr, a))) : PushResult; _responseWorker = Task.Factory.StartNew(() => RunWorker(ReadResponse), TaskCreationOptions.LongRunning); _queryWorker = Task.Factory.StartNew(() => RunWorker(SendQuery), TaskCreationOptions.LongRunning); // readify the connection _logger.Debug("Readyfying connection for {0}", Endpoint); //GetOptions(); ReadifyConnection(); _logger.Debug("Connection to {0} is ready", Endpoint); } catch (Exception ex) { Dispose(); _logger.Error("Failed building connection {0}", ex); throw; } }
public ICluster GetCluster(ClusterConfig clusterConfig) { clusterConfig.CheckArgumentNotNull("clusterConfig"); clusterConfig.Endpoints.CheckArgumentNotNull("clusterConfig.Endpoints"); TransportConfig transportConfig = clusterConfig.Transport ?? new TransportConfig(); IRecoveryService recoveryService = GetRecoveryService(transportConfig.Recoverable); KeyspaceConfig keyspaceConfig = clusterConfig.DefaultKeyspace ?? new KeyspaceConfig(); // create endpoints IEndpointSnitch snitch = ServiceActivator <Factory> .Create <IEndpointSnitch>(clusterConfig.Endpoints.Snitch, _logger); IEnumerable <IPAddress> endpoints = clusterConfig.Endpoints.Servers.Select(Network.Find).Where(x => null != x).ToArray(); if (!endpoints.Any()) { throw new ArgumentException("Expecting at least one valid endpoint"); } // create required services IEndpointStrategy endpointsManager = ServiceActivator <EndpointStrategy.Factory> .Create <IEndpointStrategy>(clusterConfig.Endpoints.Strategy, endpoints, snitch, _logger, clusterConfig.Endpoints); IConnectionFactory connectionFactory = ServiceActivator <Transport.Factory> .Create <IConnectionFactory>(transportConfig.Type, transportConfig, keyspaceConfig, _logger, _instrumentation); IPartitioner partitioner = ServiceActivator <Partitioner.Factory> .Create <IPartitioner>(clusterConfig.Partitioner); // create the cluster now ICluster cluster = ServiceActivator <Cluster.Factory> .Create <ICluster>(clusterConfig.Type, endpointsManager, _logger, connectionFactory, recoveryService, partitioner, clusterConfig); IDiscoveryService discoveryService = ServiceActivator <Discovery.Factory> .Create <IDiscoveryService>(clusterConfig.Endpoints.Discovery.Type, clusterConfig.Endpoints.Discovery, _logger, cluster); discoveryService.OnTopologyUpdate += endpointsManager.Update; cluster.OnClosed += discoveryService.SafeDispose; return(cluster); }
public LongRunningConnection(IPAddress address, TransportConfig config, KeyspaceConfig keyspaceConfig, ILogger logger, IInstrumentation instrumentation) { try { for (ushort streamId = 0; streamId < MAX_STREAMID; ++streamId) { _availableStreamIds.Push(streamId); } _config = config; _keyspaceConfig = keyspaceConfig; _logger = logger; _instrumentation = instrumentation; Endpoint = address; DefaultConsistencyLevel = config.DefaultConsistencyLevel; DefaultExecutionFlags = config.DefaultExecutionFlags; _tcpClient = new TcpClient { ReceiveTimeout = _config.ReceiveTimeout, SendTimeout = _config.SendTimeout, NoDelay = true, LingerState = { Enabled = true, LingerTime = 0 }, }; if (0 < _config.ConnectionTimeout) { // TODO: refactor this and this probably is not robust in front of error IAsyncResult asyncResult = _tcpClient.BeginConnect(address, _config.Port, null, null); bool success = asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(_config.ConnectionTimeout), true); if (!success) { throw new InvalidOperationException("Connection timeout occured."); } if (!_tcpClient.Connected) { _tcpClient.Close(); throw new InvalidOperationException("Can't connect to node."); } _tcpClient.EndConnect(asyncResult); } else { _tcpClient.Connect(address, _config.Port); } _socket = _tcpClient.Client; _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, _config.KeepAlive); if (_config.KeepAlive && 0 != _config.KeepAliveTime) { SetTcpKeepAlive(_socket, _config.KeepAliveTime, 1000); } _pushResult = _config.ReceiveBuffering ? (Action <QueryInfo, IFrameReader, bool>)((qi, fr, a) => Task.Factory.StartNew(() => PushResult(qi, fr, a))) : PushResult; _responseWorker = Task.Factory.StartNew(() => RunWorker(ReadResponse), TaskCreationOptions.LongRunning); _queryWorker = Task.Factory.StartNew(() => RunWorker(SendQuery), TaskCreationOptions.LongRunning); // readify the connection _logger.Debug("Readyfying connection for {0}", Endpoint); //GetOptions(); ReadifyConnection(); _logger.Debug("Connection to {0} is ready", Endpoint); } catch (Exception ex) { Dispose(); _logger.Error("Failed building connection {0}", ex); throw; } }