Sha256: 44ebf3eeab9fb2451815d19c26237011a96c5d66cd4f3b50381383f9fd558987

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Aggro
  module Handler
    # Private: Handler for incoming query requests.
    class Query < Struct.new(:message, :server)
      def call
        queryee_local? ? handle_local : handle_foreign
      end

      private

      def channel
        Aggro.channels[queryable_id]
      end

      def query
        @query ||= message.to_query
      end

      def queryable_id
        message.queryable_id
      end

      def query_known?
        !query.nil?
      end

      def queryee_local?
        comandee_locator.local?
      end

      def comandee_locator
        @comandee_locator ||= Locator.new(queryable_id)
      end

      def handle_foreign
        comandee_locator.primary_node.client.post message
      end

      def handle_known
        if channel
          if channel.handles_query?(query)
            handle_supported
          else
            Message::UnhandledOperation.new
          end
        else
          Message::InvalidTarget.new
        end
      end

      def handle_local
        query_known? ? handle_known : handle_unknown
      end

      def handle_unknown
        Message::UnknownOperation.new
      end

      def handle_supported
        result = channel.run_query(query)

        result.wait(5)

        if result.fulfilled?
          Message::Result.new result.value
        else
          Message::Result.new Aggro::QueryError.new('Query timed out')
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aggro-0.0.4 lib/aggro/handler/query.rb