Class: Modal::ApiClient
- Inherits:
-
Object
- Object
- Modal::ApiClient
- Defined in:
- lib/modal/api_client.rb
Constant Summary collapse
- RETRYABLE_GRPC_STATUS_CODES =
Set.new([ GRPC::Core::StatusCodes::DEADLINE_EXCEEDED, GRPC::Core::StatusCodes::UNAVAILABLE, GRPC::Core::StatusCodes::CANCELLED, GRPC::Core::StatusCodes::INTERNAL, GRPC::Core::StatusCodes::UNKNOWN ])
Instance Method Summary collapse
- #call(method_name, request_pb, options = {}) ⇒ Object
-
#initialize ⇒ ApiClient
constructor
A new instance of ApiClient.
Constructor Details
#initialize ⇒ ApiClient
Returns a new instance of ApiClient.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/modal/api_client.rb', line 14 def initialize @profile = Config.profile target, credentials = parse_server_url(@profile[:server_url]) @stub = Modal::Client::ModalClient::Stub.new( target, credentials, channel_args: { "grpc.max_receive_message_length" => 100 * 1024 * 1024, # 100 MiB "grpc.max_send_message_length" => 100 * 1024 * 1024 # 100 MiB } ) end |
Instance Method Details
#call(method_name, request_pb, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/modal/api_client.rb', line 28 def call(method_name, request_pb, = {}) retries = [:retries] || 3 base_delay = [:base_delay] || 0.1 # seconds max_delay = [:max_delay] || 1.0 # seconds delay_factor = [:delay_factor] || 2 timeout = [:timeout] # milliseconds idempotency_key = SecureRandom.uuid attempt = 0 loop do = { "x-modal-client-type" => Modal::Client::ClientType::CLIENT_TYPE_LIBMODAL.to_s, # TODO: libmodal_rb!!! "x-modal-client-version" => "1.0.0", "x-modal-token-id" => @profile[:token_id], "x-modal-token-secret" => @profile[:token_secret], "x-idempotency-key" => idempotency_key, "x-retry-attempt" => attempt.to_s } ["x-retry-delay"] = base_delay.to_s if attempt > 0 = {metadata: } [:deadline] = Time.now + timeout / 1000.0 if timeout begin response = @stub.send(method_name, request_pb, ) return response rescue GRPC::BadStatus => e if RETRYABLE_GRPC_STATUS_CODES.include?(e.code) && attempt < retries puts "Retrying #{method_name} due to #{e.code} (attempt #{attempt + 1}/#{retries})" sleep(base_delay) base_delay = [base_delay * delay_factor, max_delay].min attempt += 1 else raise convert_grpc_error(e) end rescue => e raise e end end end |