Class: Kameleoon::Network::SyncNetProvider

Inherits:
NetProvider
  • Object
show all
Defined in:
lib/kameleoon/network/net_provider.rb

Overview

Performs requests over kept-alive connections managed by Net::HTTP::Persistent.

Net::HTTP::Persistent timeouts are per client, while the SDK timeout is per request, so one client (with its own connection pool) is kept per distinct timeout value. In practice that is the default timeout plus the few values callers pass explicitly.

Constant Summary collapse

IDLE_TIMEOUT =

Idle connections are reconnected before use after this long; keeps a request from hitting a connection the server has already dropped.

15
MAX_CLIENTS =

Guards against a caller passing a different timeout on every request (each value would keep its own idle connections). Requests with further timeout values are sent on a one-off connection with their own timeout.

16

Instance Method Summary collapse

Constructor Details

#initialize ⇒ SyncNetProvider

Returns a new instance of SyncNetProvider.



50
51
52
53
54
55
56
# File 'lib/kameleoon/network/net_provider.rb', line 50

def initialize
  super()
  @clients = {}
  @clients_mutex = Mutex.new
  @overflow_warned = false
  @closed = false
end

Instance Method Details

#close ⇒ Object



72
73
74
75
76
77
78
# File 'lib/kameleoon/network/net_provider.rb', line 72

def close
  @clients_mutex.synchronize do
    @closed = true
    @clients.each_value(&:shutdown)
    @clients.clear
  end
end

#make_request(request) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kameleoon/network/net_provider.rb', line 58

def make_request(request)
  for _ in 0..8 do
    response = make_single_request(request)
    break if (response.code != 307) && (response.code != 308)

    # supports only absolute urls
    new_loc = response.headers['location'][0]
    break unless new_loc

    request.redirect(new_loc)
  end
  response
end