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.



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

def initialize(base_client)
  @base_client = base_client
end

Instance Method Details

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



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

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

#cancelObject



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

def cancel
  @call.instance_variable_get('@wrapped').instance_variable_get('@call').cancel
  i = 0
  while i < WAIT_SEC
    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



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

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

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



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

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

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



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

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

#shared(call) ⇒ Object



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

def shared(call)
  @call = call
  yield
end