Class: EventStoreClient::Connection::UrlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store_client/connection/url_parser.rb

Defined Under Namespace

Classes: ParsedUrl

Constant Summary collapse

SCHEME_REGEXP =

It is used to detect if string starts from some scheme. E.g. “esdb://”, “esdb+discover://”, “http://”, “https://” and so on

%r{\A[\w|+]*://}.freeze
FIRST_URL_RULES =

Define a set of rules to translate connection string into EventStoreClient::Connection::Url options.

“First url” means first extracted from the connection string url. It may contain schema, discover flag, user name and password. Example. Let’s say a developer provided “esdb+discover://admin:some-password@localhost:2112,localhost:2113” connection string. Then, during parsing, the first url will be ‘esdb+discover://admin:some-password@localhost:2112’

{
  dns_discover: ->(parsed_url) { parsed_url.scheme&.include?('+discover') },
  username: ->(parsed_url) { parsed_url.user },
  password: ->(parsed_url) { parsed_url.password }
}.freeze
LAST_URL_RULES =

“Last url” means the latest extracted url from the connections string. It contains params. So LAST_URL_RULES rules defines rules how to translate params into EventStoreClient::Connection::Url options. Example. Let’s say a developer provided “esdb+discover://admin:some-password@localhost:2112,localhost:2113/?tls=false” connection string. Then, during parsing, last url will be ‘localhost:2113/?tls=false’.

{
  throw_on_append_failure: lambda { |parsed_url|
    boolean_param(parsed_url.params['throwOnAppendFailure'])
  },
  tls: ->(parsed_url) { boolean_param(parsed_url.params['tls']) },
  tls_verify_cert: ->(parsed_url) { boolean_param(parsed_url.params['tlsVerifyCert']) },
  tls_ca_file: ->(parsed_url) { parsed_url.params['tlsCAFile'] },
  gossip_timeout: ->(parsed_url) { parsed_url.params['gossipTimeout']&.to_i },
  discover_interval: ->(parsed_url) { parsed_url.params['discoverInterval']&.to_i },
  max_discover_attempts: ->(parsed_url) { parsed_url.params['maxDiscoverAttempts']&.to_i },
  ca_lookup_interval: ->(parsed_url) { parsed_url.params['caLookupInterval']&.to_i },
  ca_lookup_attempts: ->(parsed_url) { parsed_url.params['caLookupAttempts']&.to_i },
  node_preference: lambda { |parsed_url|
    value = parsed_url.params['nodePreference']&.dup
    if value
      value[0] = value[0]&.upcase
      value = value.to_sym
    end
    value if Url::NODE_PREFERENCES.include?(value)
  },
  timeout: ->(parsed_url) { parsed_url.params['timeout']&.to_i },
  grpc_retry_attempts: ->(parsed_url) { parsed_url.params['grpcRetryAttempts']&.to_i },
  grpc_retry_interval: ->(parsed_url) { parsed_url.params['grpcRetryInterval']&.to_i }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.boolean_param(value) ⇒ Object



7
8
9
10
11
# File 'lib/event_store_client/connection/url_parser.rb', line 7

def boolean_param(value)
  return unless %w[true false].include?(value)

  value == 'true'
end

Instance Method Details

#call(connection_str) ⇒ EventStoreClient::Connection::Url

Parameters:

  • connection_str (String)

    EventStore DB connection string

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/event_store_client/connection/url_parser.rb', line 65

def call(connection_str)
  urls = connection_str.split(',')
  return Url.new if urls.empty?

  first_url, *other, last_url = urls

  es_url = Url.new
  options_from_first(es_url, first_url)
  if last_url.nil? # We are dealing with one node in the url
    options_from_last(es_url, first_url)
  else
    options_from_other(es_url, other)
    options_from_last(es_url, last_url)
  end

  es_url
end