Module: Semian::GRPC

Includes:
Adapter
Defined in:
lib/semian/grpc.rb

Defined Under Namespace

Classes: SemianConfigurationChangedError

Constant Summary collapse

ResourceBusyError =
::GRPC::ResourceBusyError
CircuitOpenError =
::GRPC::CircuitOpenError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Adapter

#clear_semian_resource, #semian_resource

Class Attribute Details

.semian_configurationObject

Returns the value of attribute semian_configuration.



39
40
41
# File 'lib/semian/grpc.rb', line 39

def semian_configuration
  @semian_configuration
end

Class Method Details

.retrieve_semian_configuration(host) ⇒ Object



49
50
51
# File 'lib/semian/grpc.rb', line 49

def retrieve_semian_configuration(host)
  @semian_configuration.call(host) if @semian_configuration.respond_to?(:call)
end

Instance Method Details

#acquire_semian_resource_grpc(scope:) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/semian/grpc.rb', line 110

def acquire_semian_resource_grpc(scope:)
  acquire_semian_resource(adapter: :grpc, scope: scope) do
    result = yield
    handle_operation(result, scope) if result.is_a?(::GRPC::ActiveCall::Operation)
    result
  end
end

#bidi_streamerObject



104
105
106
107
108
# File 'lib/semian/grpc.rb', line 104

def bidi_streamer(*, **)
  return super if disabled?

  acquire_semian_resource_grpc(scope: :bidi_streamer) { super }
end

#client_streamerObject



92
93
94
95
96
# File 'lib/semian/grpc.rb', line 92

def client_streamer(*, **)
  return super if disabled?

  acquire_semian_resource_grpc(scope: :client_streamer) { super }
end

#disabled?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/semian/grpc.rb', line 82

def disabled?
  raw_semian_options.nil?
end

#handle_operation(operation, scope) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/semian/grpc.rb', line 118

def handle_operation(operation, scope)
  execute = operation.singleton_method(:execute)
  operation.instance_variable_set(:@semian, self)
  operation.define_singleton_method(:execute) do
    @semian.send(:acquire_semian_resource, adapter: :grpc, scope: scope) { execute.call }
  end
end

#raw_semian_optionsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/semian/grpc.rb', line 55

def raw_semian_options
  @raw_semian_options ||= begin
    # If the host is empty, it's possible that the adapter was initialized
    # with the channel. Therefore, we look into the channel to find the host
    host = if @host.empty?
      @ch.target
    else
      @host
    end
    @raw_semian_options = Semian::GRPC.retrieve_semian_configuration(host)
    @raw_semian_options = @raw_semian_options.dup unless @raw_semian_options.nil?
  end
end

#request_responseObject



86
87
88
89
90
# File 'lib/semian/grpc.rb', line 86

def request_response(*, **)
  return super if disabled?

  acquire_semian_resource_grpc(scope: :request_response) { super }
end

#resource_exceptionsObject



73
74
75
76
77
78
79
80
# File 'lib/semian/grpc.rb', line 73

def resource_exceptions
  [
    ::GRPC::DeadlineExceeded,
    ::GRPC::ResourceExhausted,
    ::GRPC::Unavailable,
    ::GRPC::Unknown,
  ]
end

#semian_identifierObject



69
70
71
# File 'lib/semian/grpc.rb', line 69

def semian_identifier
  @semian_identifier ||= raw_semian_options[:name]
end

#server_streamerObject



98
99
100
101
102
# File 'lib/semian/grpc.rb', line 98

def server_streamer(*, **)
  return super if disabled?

  acquire_semian_resource_grpc(scope: :server_streamer) { super }
end