Sha256: 454e712bcd10db9d433ccb6b25264ce15ce6fd56774cd2ff1b20915320468683

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'socket'
require 'timeout'

module Dalli
  module Protocol
    ##
    # Manages the buffer for responses from memcached.
    ##
    class ResponseBuffer
      def initialize(io_source, response_processor)
        @io_source = io_source
        @response_processor = response_processor
      end

      def read
        @buffer << @io_source.read_nonblock
      end

      # Attempts to process a single response from the buffer.  Starts
      # by advancing the buffer to the specified start position
      def process_single_getk_response
        bytes, status, cas, key, value = @response_processor.getk_response_from_buffer(@buffer)
        advance(bytes)
        [status, cas, key, value]
      end

      # Advances the internal response buffer by bytes_to_advance
      # bytes.  The
      def advance(bytes_to_advance)
        return unless bytes_to_advance.positive?

        @buffer = @buffer.byteslice(bytes_to_advance..-1)
      end

      # Resets the internal buffer to an empty state,
      # so that we're ready to read pipelined responses
      def reset
        @buffer = ''.b
      end

      # Clear the internal response buffer
      def clear
        @buffer = nil
      end

      def in_progress?
        !@buffer.nil?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dalli-3.1.6 lib/dalli/protocol/response_buffer.rb
dalli-3.1.5 lib/dalli/protocol/response_buffer.rb
dalli-3.1.4 lib/dalli/protocol/response_buffer.rb