Class: KubeMQ::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/kubemq/configuration.rb

Overview

Central configuration for KubeMQ client connections.

Resolved in precedence order:

  1. Constructor keyword arguments
  2. Global configure block
  3. Environment variables (+KUBEMQ_ADDRESS+, KUBEMQ_AUTH_TOKEN)
  4. Built-in defaults

Examples:

Full configuration

config = KubeMQ::Configuration.new(
  address: "broker.example.com:50000",
  client_id: "order-service",
  auth_token: ENV["KUBEMQ_AUTH_TOKEN"],
  tls: KubeMQ::TLSConfig.new(enabled: true, ca_file: "/certs/ca.pem"),
  log_level: :info
)
client = KubeMQ::PubSubClient.new(config: config)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address: nil, client_id: nil, auth_token: nil, tls: nil, keepalive: nil, reconnect_policy: nil, max_send_size: 104_857_600, max_receive_size: 104_857_600, log_level: :warn, default_timeout: 30) ⇒ Configuration

Creates a new configuration with the given options.

Unspecified options fall back to environment variables and then to built-in defaults.

Parameters:

  • address (String, nil) (defaults to: nil)

    broker host:port (default: ENV["KUBEMQ_ADDRESS"] or "localhost:50000")

  • client_id (String, nil) (defaults to: nil)

    unique client identifier (default: auto-generated)

  • auth_token (String, nil) (defaults to: nil)

    bearer authentication token

  • tls (TLSConfig, nil) (defaults to: nil)

    TLS/mTLS configuration (default: TLS disabled)

  • keepalive (KeepAliveConfig, nil) (defaults to: nil)

    gRPC keepalive settings (default: enabled)

  • reconnect_policy (ReconnectPolicy, nil) (defaults to: nil)

    auto-reconnect policy (default: enabled)

  • max_send_size (Integer) (defaults to: 104_857_600)

    maximum outbound message size in bytes (default: 100 MB)

  • max_receive_size (Integer) (defaults to: 104_857_600)

    maximum inbound message size in bytes (default: 100 MB)

  • log_level (Symbol) (defaults to: :warn)

    log level — :debug, :info, :warn, :error (default: :warn)

  • default_timeout (Integer) (defaults to: 30)

    default gRPC deadline in seconds (default: 30)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/kubemq/configuration.rb', line 200

def initialize(address: nil, client_id: nil, auth_token: nil,
               tls: nil, keepalive: nil, reconnect_policy: nil,
               max_send_size: 104_857_600, max_receive_size: 104_857_600,
               log_level: :warn, default_timeout: 30)
  @address = address || ENV.fetch('KUBEMQ_ADDRESS', 'localhost:50000')
  @client_id = client_id || "kubemq-ruby-#{SecureRandom.hex(4)}"
  @auth_token = auth_token || ENV.fetch('KUBEMQ_AUTH_TOKEN', nil)
  @tls = tls || TLSConfig.new
  @keepalive = keepalive || KeepAliveConfig.new
  @reconnect_policy = reconnect_policy || ReconnectPolicy.new
  @max_send_size = max_send_size
  @max_receive_size = max_receive_size
  @log_level = log_level
  @default_timeout = Integer(default_timeout)
end

Instance Attribute Details

#addressString

Returns broker host:port (default: ENV["KUBEMQ_ADDRESS"] or "localhost:50000").

Returns:

  • (String)

    broker host:port (default: ENV["KUBEMQ_ADDRESS"] or "localhost:50000")



180
181
182
# File 'lib/kubemq/configuration.rb', line 180

def address
  @address
end

#auth_tokenString?

Returns bearer authentication token.

Returns:

  • (String, nil)

    bearer authentication token



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#client_idString

Returns unique client identifier (default: auto-generated).

Returns:

  • (String)

    unique client identifier (default: auto-generated)



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#default_timeoutInteger

Returns default gRPC deadline in seconds (default: 30).

Returns:

  • (Integer)

    default gRPC deadline in seconds (default: 30)



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#keepaliveKeepAliveConfig

Returns gRPC keepalive settings.

Returns:



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#log_levelSymbol

Returns log level — :debug, :info, :warn, :error (default: :warn).

Returns:

  • (Symbol)

    log level — :debug, :info, :warn, :error (default: :warn)



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#max_receive_sizeInteger

Returns maximum inbound message size in bytes (default: 104_857_600 / 100 MB).

Returns:

  • (Integer)

    maximum inbound message size in bytes (default: 104_857_600 / 100 MB)



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#max_send_sizeInteger

Returns maximum outbound message size in bytes (default: 104_857_600 / 100 MB).

Returns:

  • (Integer)

    maximum outbound message size in bytes (default: 104_857_600 / 100 MB)



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#reconnect_policyReconnectPolicy

Returns auto-reconnect policy.

Returns:



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

#tlsTLSConfig

Returns TLS/mTLS configuration.

Returns:



180
181
182
183
# File 'lib/kubemq/configuration.rb', line 180

attr_accessor :address, :client_id, :auth_token,
:tls, :keepalive, :reconnect_policy,
:max_send_size, :max_receive_size,
:log_level, :default_timeout

Instance Method Details

#inspectString

Returns a developer-friendly string with the auth token redacted.

Returns:

  • (String)


219
220
221
222
# File 'lib/kubemq/configuration.rb', line 219

def inspect
  token_display = @auth_token ? '[REDACTED]' : 'nil'
  "#<#{self.class} address=#{@address.inspect} client_id=#{@client_id.inspect} auth_token=#{token_display}>"
end

#validate!Boolean

Validates this configuration and raises on invalid settings.

Checks that address is present and that TLS cert/key files are provided as a pair when TLS is enabled.

rubocop:disable Naming/PredicateMethod -- bang API: raises on invalid config or returns true

Returns:

  • (Boolean)

    true if the configuration is valid

Raises:



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/kubemq/configuration.rb', line 234

def validate!
  raise ConfigurationError, 'address is required' if @address.nil? || @address.empty?
  if @tls.enabled && @tls.cert_file && !@tls.key_file
    raise ConfigurationError, 'TLS key_file is required when cert_file is set'
  end
  if @tls.enabled && @tls.key_file && !@tls.cert_file
    raise ConfigurationError, 'TLS cert_file is required when key_file is set'
  end

  true
end