Sha256: 45f231da40fbfc61b440de1b6d729b104519df2066191fbe1b92dc8b47b886d5

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Prefab
  class CancellableInterceptor < GRPC::ClientInterceptor
    WAIT_SEC = 3

    def initialize(base_client)
      @base_client = base_client
    end

    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

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

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

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

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

    def shared(call)
      @call = call
      yield
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prefab-cloud-ruby-0.21.0 lib/prefab/cancellable_interceptor.rb