Class: RightScale::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
ProtocolVersionMixin
Defined in:
lib/right_agent/dispatcher.rb

Overview

Dispatching of payload to specified actor

Defined Under Namespace

Classes: DuplicateRequest, InvalidRequestType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ProtocolVersionMixin

#can_always_handle_hash_payload?, #can_handle_http?, #can_handle_msgpack_result?, #can_handle_multicast_result?, #can_handle_non_delivery_result?, #can_handle_non_nanite_ids?, #can_handle_request_retries?, #can_put_version_in_packet?, #can_route_to_response_queue?, #can_use_router_query_tags?

Constructor Details

#initialize(agent, dispatched_cache = nil) ⇒ Dispatcher

Initialize dispatcher

Parameters

agent(Agent)

Agent using this dispatcher; uses its identity and registry

dispatched_cache(DispatchedCache|nil)

Cache for dispatched requests that is used for detecting

duplicate requests, or nil if duplicate checking is disabled


53
54
55
56
57
58
59
60
# File 'lib/right_agent/dispatcher.rb', line 53

def initialize(agent, dispatched_cache = nil)
  @agent = agent
  @registry = @agent.registry
  @identity = @agent.identity
  @dispatched_cache = dispatched_cache
  reset_stats
  @@instance = self
end

Instance Attribute Details

#identityObject (readonly)

(String) Identity of associated agent



37
38
39
# File 'lib/right_agent/dispatcher.rb', line 37

def identity
  @identity
end

#registryObject (readonly)

(ActorRegistry) Registry for actors



34
35
36
# File 'lib/right_agent/dispatcher.rb', line 34

def registry
  @registry
end

Class Method Details

.instanceObject

For direct access to current dispatcher

Return

(Dispatcher)

This dispatcher instance if defined, otherwise nil



43
44
45
# File 'lib/right_agent/dispatcher.rb', line 43

def self.instance
  @@instance if defined?(@@instance)
end

Instance Method Details

#dispatch(request) ⇒ Object

Route request to appropriate actor for servicing Reject requests whose TTL has expired or that are duplicates of work already dispatched

Parameters

request(Request|Push)

Packet containing request

header(AMQP::Frame::Header|nil)

Request header containing ack control

Return

(Result|nil)

Result of request, or nil if there is no result because request is a Push

Raise

InvalidRequestType

If the request cannot be routed to an actor

DuplicateRequest

If request rejected because it has already been processed



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/right_agent/dispatcher.rb', line 86

def dispatch(request)
  token = request.token
  actor, method, idempotent = route(request)
  received_at = @request_stats.update(method, (token if request.is_a?(Request)))
  if (dup = duplicate?(request, method, idempotent))
    raise DuplicateRequest, dup
  end
  unless (result = expired?(request, method))
    result = perform(request, actor, method, idempotent)
  end
  if request.is_a?(Request)
    duration = @request_stats.finish(received_at, token)
    Result.new(token, request.reply_to, result, @identity, request.from, request.tries, request.persistent, duration)
  end
end

#routable?(actor) ⇒ Boolean

Determine whether able to route requests to specified actor

Parameters

actor(String)

Actor name

Return

(Boolean)

true if can route to actor, otherwise false

Returns:

  • (Boolean)


69
70
71
# File 'lib/right_agent/dispatcher.rb', line 69

def routable?(actor)
  !!@registry.actor_for(actor)
end

#stats(reset = false) ⇒ Object

Get dispatcher statistics

Parameters

reset(Boolean)

Whether to reset the statistics after getting the current ones

Return

stats(Hash)

Current statistics:

“dispatched cache”(Hash|nil)

Number of dispatched requests cached and age of youngest and oldest,

  or nil if empty
"dispatch failures"(Hash|nil):: Dispatch failure activity stats with keys "total", "percent", "last", and "rate"
  with percentage breakdown per failure type, or nil if none
"rejects"(Hash|nil):: Request reject activity stats with keys "total", "percent", "last", and "rate"
  with percentage breakdown per reason ("duplicate (<method>)", "retry duplicate (<method>)", or
  "stale (<method>)"), or nil if none
"requests"(Hash|nil):: Request activity stats with keys "total", "percent", "last", and "rate"
  with percentage breakdown per request type, or nil if none


118
119
120
121
122
123
124
125
126
127
# File 'lib/right_agent/dispatcher.rb', line 118

def stats(reset = false)
  stats = {
    "dispatched cache"  => (@dispatched_cache.stats if @dispatched_cache),
    "dispatch failures" => @dispatch_failure_stats.all,
    "rejects"           => @reject_stats.all,
    "requests"          => @request_stats.all
  }
  reset_stats if reset
  stats
end