Module: EventStore::HTTP::Session

Included in:
AnyMember, Leader
Defined in:
lib/event_store/http/session.rb,
lib/event_store/http/session/read.rb,
lib/event_store/http/session/leader.rb,
lib/event_store/http/session/factory.rb,
lib/event_store/http/session/defaults.rb,
lib/event_store/http/session/log_text.rb,
lib/event_store/http/session/any_member.rb

Defined Under Namespace

Modules: Defaults, LogText, Read Classes: AnyMember, Factory, Leader

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(settings = nil, namespace: nil, type: nil) ⇒ Object



16
17
18
# File 'lib/event_store/http/session.rb', line 16

def self.build(settings=nil, namespace: nil, type: nil)
  Factory.(settings, namespace: namespace, type: type)
end

.configure(receiver, settings = nil, attr_name: nil, session: nil, **arguments) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/event_store/http/session.rb', line 20

def self.configure(receiver, settings=nil, attr_name: nil, session: nil, **arguments)
  attr_name ||= :session

  if session.nil?
    session = build settings, **arguments
  end

  receiver.public_send "#{attr_name}=", session
  session
end

.included(cls) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/event_store/http/session.rb', line 4

def self.included(cls)
  cls.class_exec do
    include Log::Dependency

    dependency :connect, Connect
    dependency :data_logger, Log::Data
    dependency :retry, Retry

    attr_writer :net_http
  end
end

Instance Method Details

#configure_retry(request) ⇒ Object



108
109
110
# File 'lib/event_store/http/session.rb', line 108

def configure_retry(request)
  request.retry = self.retry
end

#establish_connection(ip_address = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/event_store/http/session.rb', line 94

def establish_connection(ip_address=nil)
  data_logger.trace { "Establishing connection (IPAddress: #{ip_address || '(none)'})" }

  net_http = self.retry.() do
    connect.(ip_address).tap &:start
  end

  self.net_http = net_http

  data_logger.debug { "Connection established (IPAddress: #{net_http.ip_address}, Host: #{net_http.address}, Port: #{net_http.port})" }

  net_http
end

#net_httpObject



84
85
86
# File 'lib/event_store/http/session.rb', line 84

def net_http
  @net_http ||= establish_connection
end

#reconnect(ip_address = nil) ⇒ Object



88
89
90
91
92
# File 'lib/event_store/http/session.rb', line 88

def reconnect(ip_address=nil)
  net_http.finish if net_http.active?

  establish_connection ip_address
end

#request(request) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/event_store/http/session.rb', line 33

def request(request)
  logger.trace { "Issuing request (#{LogText.request request})" }

  data_logger.trace {
    <<~TEXT
    Issuing request
    
    #{request.method} #{request.path}
    #{LogText.header_data request}

    #{LogText.body_data request}
    TEXT
  }

  self.retry.() do |_retry|
    begin
      response = net_http.request request
    rescue SystemCallError, IOError => error
      logger.warn "Connection error during request; reconnecting (ErrorClass: #{error.class}, ErrorMessage: #{error.message})"
      reconnect

      reset_request request

      _retry.failed error
    end

    logger.debug { "Request completed (#{LogText.request request, response})" }

    data_logger.debug {
      <<~TEXT
      Request completed
      
      #{request.method} #{request.path}
      #{LogText.header_data request}
      #{LogText.body_data request}

      HTTP/#{response.http_version} #{response.code} #{response.message}
      #{LogText.header_data response}
      #{LogText.body_data response}
      TEXT
    }

    if Net::HTTPServerError === response
      logger.warn { "Server error (#{LogText.request request, response})" }
      _retry.failed
    end

    response
  end
end

#reset_request(request) ⇒ Object



112
113
114
115
# File 'lib/event_store/http/session.rb', line 112

def reset_request(request)
  request['host'] = nil
  request['connection'] = nil
end