Exemple #1
0
 def send_event(self, event: EventEnvelope, broadcast=False) -> None:
     if not isinstance(event, EventEnvelope):
         raise ValueError("event object must be an EventEnvelope class")
     # restore distributed tracing info from current thread
     trace_info = self.get_trace()
     if trace_info:
         event.set_trace(trace_info.get_id(), trace_info.get_path())
     # regulate rate for best performance
     self._seq += 1
     self._throttle.regulate_rate(self._seq)
     route = event.get_to()
     if broadcast:
         event.set_broadcast(True)
     reply_to = event.get_reply_to()
     if reply_to:
         target = reply_to[2:] if reply_to.startswith('->') else reply_to
         if route == target:
             raise ValueError("route and reply_to must not be the same")
     if route in self._function_queues:
         if event.is_broadcast() and self._cloud.is_connected():
             self._cloud.send_payload({
                 'type': 'event',
                 'event': event.to_map()
             })
         else:
             self._loop.call_soon_threadsafe(self._send, route,
                                             event.to_map())
     else:
         if self._cloud.is_connected():
             self._cloud.send_payload({
                 'type': 'event',
                 'event': event.to_map()
             })
         else:
             raise ValueError("route " + route + " not found")
Exemple #2
0
 def request(self, event: EventEnvelope, timeout_seconds: float):
     timeout_value = self.util.get_float(timeout_seconds)
     if timeout_value <= 0:
         raise ValueError(
             "timeout value in seconds must be positive number")
     if not isinstance(event, EventEnvelope):
         raise ValueError("event object must be an EventEnvelope")
     # restore distributed tracing info from current thread
     trace_info = self.get_trace()
     if trace_info:
         if trace_info.get_route() is not None and event.get_from() is None:
             event.set_from(trace_info.get_route())
         if trace_info.get_id() is not None and trace_info.get_path(
         ) is not None:
             event.set_trace(trace_info.get_id(), trace_info.get_path())
     # emulate RPC
     inbox = Inbox(self)
     temp_route = inbox.get_route()
     inbox_queue = inbox.get_queue()
     try:
         route = event.get_to()
         event.set_reply_to(temp_route, me=True)
         if route in self._function_queues:
             self._loop.call_soon_threadsafe(self._send, route,
                                             event.to_map())
         else:
             if self._cloud.is_connected():
                 self._cloud.send_payload({
                     'type': 'event',
                     'event': event.to_map()
                 })
             else:
                 raise ValueError("route " + route + " not found")
         # wait until response event is delivered to the inbox
         return inbox_queue.get(True, timeout_value)
     except Empty:
         raise TimeoutError('Route ' + event.get_to() + ' timeout for ' +
                            format(timeout_value, '.3f') + " seconds")
     finally:
         inbox.close()