Class: Prefab::CancellableInterceptor

Inherits:
GRPC::ClientInterceptor
  • Object
show all
Defined in:
lib/prefab/cancellable_interceptor.rb

Constant Summary collapse

WAIT_SEC =
3

Instance Method Summary collapse

Constructor Details

#initialize(base_client) ⇒ CancellableInterceptor

Returns a new instance of CancellableInterceptor.



6
7
8
# File 'lib/prefab/cancellable_interceptor.rb', line 6

def initialize(base_client)
  @base_client = base_client
end

Instance Method Details

#bidi_streamer(requests:, call:, method:, metadata:, &block) ⇒ Object



39
40
41
# File 'lib/prefab/cancellable_interceptor.rb', line 39

def bidi_streamer(requests:, call:, method:, metadata:, &block)
  shared(call, &block)
end

#cancelObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/prefab/cancellable_interceptor.rb', line 10

def cancel
  @call.instance_variable_get("@wrapped").instance_variable_get("@call").cancel
  i = 0
  while (i < WAIT_SEC) do
    if @call.instance_variable_get("@wrapped").cancelled?
      @base_client.log_internal Logger::DEBUG, "Cancelled streaming."
      return
    else
      @base_client.log_internal Logger::DEBUG, "Unable to cancel streaming. Trying again"
      @call.instance_variable_get("@wrapped").instance_variable_get("@call").cancel
      i += 1
      sleep(1)
    end
  end
  @base_client.log_internal Logger::INFO, "Unable to cancel streaming."
end

#client_streamer(requests:, call:, method:, metadata:, &block) ⇒ Object



31
32
33
# File 'lib/prefab/cancellable_interceptor.rb', line 31

def client_streamer(requests:, call:, method:, metadata:, &block)
  shared(call, &block)
end

#request_response(request:, call:, method:, metadata:, &block) ⇒ Object



27
28
29
# File 'lib/prefab/cancellable_interceptor.rb', line 27

def request_response(request:, call:, method:, metadata:, &block)
  shared(call, &block)
end

#server_streamer(request:, call:, method:, metadata:, &block) ⇒ Object



35
36
37
# File 'lib/prefab/cancellable_interceptor.rb', line 35

def server_streamer(request:, call:, method:, metadata:, &block)
  shared(call, &block)
end

#shared(call) ⇒ Object



43
44
45
46
# File 'lib/prefab/cancellable_interceptor.rb', line 43

def shared(call)
  @call = call
  yield
end