Module: AnyCable::GRPC::Config

Included in:
Config
Defined in:
lib/anycable/grpc/config.rb

Instance Method Summary collapse

Instance Method Details

#enhance_grpc_server_args(opts) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/anycable/grpc/config.rb', line 60

def enhance_grpc_server_args(opts)
  return opts if opts.key?("grpc.max_connection_age_ms")
  return opts unless rpc_max_connection_age.to_i > 0

  opts["grpc.max_connection_age_ms"] = rpc_max_connection_age.to_i * 1000
  opts
end

#log_grpcObject Also known as: log_grpc?



23
24
25
# File 'lib/anycable/grpc/config.rb', line 23

def log_grpc
  debug? || super
end

#normalized_grpc_server_argsObject



50
51
52
53
54
55
56
57
58
# File 'lib/anycable/grpc/config.rb', line 50

def normalized_grpc_server_args
  val = rpc_server_args
  return {} unless val.is_a?(Hash)

  val.transform_keys do |key|
    skey = key.to_s
    skey.start_with?("grpc.") ? skey : "grpc.#{skey}"
  end
end

#tls_credentialsObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/anycable/grpc/config.rb', line 68

def tls_credentials
  cert_path_or_content = rpc_tls_cert # Assign to local variable to make steep happy
  key_path_or_content = rpc_tls_key # Assign to local variable to make steep happy
  return {} if cert_path_or_content.nil? || key_path_or_content.nil?

  cert = File.exist?(cert_path_or_content) ? File.read(cert_path_or_content) : cert_path_or_content
  pkey = File.exist?(key_path_or_content) ? File.read(key_path_or_content) : key_path_or_content

  {cert: cert, pkey: pkey}
end

#to_grpc_paramsObject

Build gRPC server parameters



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/anycable/grpc/config.rb', line 31

def to_grpc_params
  {
    pool_size: rpc_pool_size,
    max_waiting_requests: rpc_max_waiting_requests,
    poll_period: rpc_poll_period,
    pool_keep_alive: rpc_pool_keep_alive,
    tls_credentials: tls_credentials,
    server_args: enhance_grpc_server_args(normalized_grpc_server_args).tap do |sargs|
      # Provide keepalive defaults unless explicitly set.
      # They must MATCH the corresponding Go client defaults:
      # https://github.com/anycable/anycable-go/blob/62e77e7f759aa9253c2bd23812dd59ec8471db86/rpc/rpc.go#L512-L515
      #
      # See also https://github.com/grpc/grpc/blob/master/doc/keepalive.md and https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
      sargs["grpc.keepalive_permit_without_calls"] ||= 1
      sargs["grpc.http2.min_recv_ping_interval_without_data_ms"] ||= 10_000
    end
  }
end