Ejemplo n.º 1
0
 /// <summary>
 /// It callbacks all operations already sent / or to be written, that do not have a response.
 /// </summary>
 internal void CancelPending(Exception ex, SocketError?socketError = null)
 {
     //Multiple IO worker threads may been notifying that the socket is closing/in error
     lock (_cancelLock)
     {
         _isCanceled = true;
         if (socketError != null)
         {
             _logger.Verbose("Socket error " + socketError.Value);
         }
         _logger.Info("Canceling pending operations " + _pendingOperations.Count + " and write queue " + _writeQueue.Count);
         if (_pendingOperations.Count == 0 && _writeQueue.Count == 0)
         {
             return;
         }
         if (ex == null)
         {
             if (socketError != null)
             {
                 ex = new SocketException((int)socketError.Value);
             }
             else
             {
                 //It is closing
                 ex = new SocketException((int)SocketError.NotConnected);
             }
         }
         if (_writeQueue.Count > 0)
         {
             //Callback all the items in the write queue
             OperationState state = null;
             while (_writeQueue.TryDequeue(out state))
             {
                 state.InvokeCallback(ex);
             }
         }
         if (_pendingOperations.Count > 0)
         {
             //Callback for every pending operation
             foreach (var item in _pendingOperations)
             {
                 item.Value.InvokeCallback(ex);
             }
             _pendingOperations.Clear();
         }
         if (_pendingWaitHandle != null)
         {
             _pendingWaitHandle.Set();
         }
     }
 }
Ejemplo n.º 2
0
        private void SendQueueProcessItem(OperationState state)
        {
            short streamId;

            //Check if Cassandra can process a new operation
            if (!_freeOperations.TryPop(out streamId))
            {
                //Queue it up for later.
                //When receiving the next complete message, we can process it.
                _writeQueue.Enqueue(state);
                _logger.Info("Enqueued: {0}, if this message is recurrent consider configuring more connections per host or lower the pressure", _writeQueue.Count);
                Interlocked.Exchange(ref _canWriteNext, 1);
                return;
            }
            //We have a valid stream id
            //Only 1 thread at a time can be here.
            _logger.Verbose("Sending #" + streamId + " for " + state.Request.GetType().Name);
            _pendingOperations.AddOrUpdate(streamId, state, (k, oldValue) => state);
            try
            {
                var frameStream = state.Request.GetFrame(streamId).Stream;
                //We will not use the request any more, stop reference it.
                state.Request = null;
                //Start sending it
                _tcpSocket.Write(frameStream);
                //Closure state variable
                var delegateState = state;
                if (Configuration.SocketOptions.ReadTimeoutMillis > 0 && Configuration.Timer != null)
                {
                    state.Timeout = Configuration.Timer.NewTimeout(() => OnTimeout(delegateState), Configuration.SocketOptions.ReadTimeoutMillis);
                }
            }
            catch (Exception ex)
            {
                //There was an error while serializing or begin sending
                _logger.Error(ex);
                //The request was not written, clear it from pending operations
                RemoveFromPending(streamId);
                //Callback with the Exception
                state.InvokeCallback(ex);
            }
        }
Ejemplo n.º 3
0
 private void SendQueueProcessItem(OperationState state)
 {
     short streamId;
     //Check if Cassandra can process a new operation
     if (!_freeOperations.TryPop(out streamId))
     {
         //Queue it up for later.
         //When receiving the next complete message, we can process it.
         _writeQueue.Enqueue(state);
         _logger.Info("Enqueued: {0}, if this message is recurrent consider configuring more connections per host or lower the pressure", _writeQueue.Count);
         Interlocked.Exchange(ref _canWriteNext, 1);
         return;
     }
     //We have a valid stream id
     //Only 1 thread at a time can be here.
     _logger.Verbose("Sending #" + streamId + " for " + state.Request.GetType().Name);
     _pendingOperations.AddOrUpdate(streamId, state, (k, oldValue) => state);
     try
     {
         var frameStream = state.Request.GetFrame(streamId).Stream;
         //We will not use the request any more, stop reference it.
         state.Request = null;
         //Start sending it
         _tcpSocket.Write(frameStream);
         //Closure state variable
         var delegateState = state;
         if (Configuration.SocketOptions.ReadTimeoutMillis > 0 && Configuration.Timer != null)
         {
             state.Timeout = Configuration.Timer.NewTimeout(() => OnTimeout(delegateState), Configuration.SocketOptions.ReadTimeoutMillis);   
         }
     }
     catch (Exception ex)
     {
         //There was an error while serializing or begin sending
         _logger.Error(ex);
         //The request was not written, clear it from pending operations
         RemoveFromPending(streamId);
         //Callback with the Exception
         state.InvokeCallback(ex);
     }
 }