Class: Statsig::Network
- Inherits:
-
Object
- Object
- Statsig::Network
- Defined in:
- lib/network.rb
Instance Method Summary collapse
- #download_config_specs(since_time) ⇒ Object
- #get(url, retries = 0, backoff = 1) ⇒ Object
- #get_id_lists ⇒ Object
-
#initialize(server_secret, options, backoff_mult = 10) ⇒ Network
constructor
A new instance of Network.
- #post(url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) ⇒ Object
- #post_logs(events, error_boundary) ⇒ Object
- #request(method, url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) ⇒ Object
Constructor Details
#initialize(server_secret, options, backoff_mult = 10) ⇒ Network
Returns a new instance of Network.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/network.rb', line 21 def initialize(server_secret, , backoff_mult = 10) super() @options = @server_secret = server_secret @local_mode = .local_mode @timeout = .network_timeout @backoff_multiplier = backoff_mult @post_logs_retry_backoff = .post_logs_retry_backoff @post_logs_retry_limit = .post_logs_retry_limit @session_id = SecureRandom.uuid @connection_pool = ConnectionPool.new(size: 3) do = Statsig. client = HTTP.use(:auto_inflate).headers( { 'STATSIG-API-KEY' => @server_secret, 'STATSIG-SERVER-SESSION-ID' => @session_id, 'Content-Type' => 'application/json; charset=UTF-8', 'STATSIG-SDK-TYPE' => ['sdkType'], 'STATSIG-SDK-VERSION' => ['sdkVersion'], 'STATSIG-SDK-LANGUAGE-VERSION' => ['languageVersion'], 'Accept-Encoding' => 'gzip' } ).accept(:json) if @timeout client = client.timeout(@timeout) end client end end |
Instance Method Details
#download_config_specs(since_time) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/network.rb', line 53 def download_config_specs(since_time) url = @options.download_config_specs_url dcs_url = "#{url}#{@server_secret}.json" if since_time.positive? dcs_url += "?sinceTime=#{since_time}" end get(dcs_url) end |
#get(url, retries = 0, backoff = 1) ⇒ Object
89 90 91 |
# File 'lib/network.rb', line 89 def get(url, retries = 0, backoff = 1) request(:GET, url, nil, retries, backoff) end |
#get_id_lists ⇒ Object
84 85 86 87 |
# File 'lib/network.rb', line 84 def get_id_lists url = @options.get_id_lists_url post(url, JSON.generate({ 'statsigMetadata' => Statsig. })) end |
#post(url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) ⇒ Object
93 94 95 |
# File 'lib/network.rb', line 93 def post(url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) request(:POST, url, body, retries, backoff, zipped, event_count) end |
#post_logs(events, error_boundary) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/network.rb', line 62 def post_logs(events, error_boundary) url = @options.log_event_url event_count = events.length json_body = JSON.generate({ events: events, statsigMetadata: Statsig. }) gzip = Zlib::GzipWriter.new(StringIO.new) gzip << json_body response, e = post(url, gzip.close.string, @post_logs_retry_limit, 1, true, event_count) # Consume response body to ensure connection can be closed. response&.flush unless e == nil = "Failed to log #{event_count} events after #{@post_logs_retry_limit} retries" puts "[Statsig]: #{}" error_boundary.log_exception(e, tag: 'statsig::log_event_failed', extra: { eventCount: event_count, error: }, force: true) return end rescue StandardError end |
#request(method, url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/network.rb', line 97 def request(method, url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) if @local_mode return nil, nil end backoff_adjusted = backoff > 10 ? backoff += Random.rand(10) : backoff # to deter overlap if @post_logs_retry_backoff if @post_logs_retry_backoff.is_a? Integer backoff_adjusted = @post_logs_retry_backoff else backoff_adjusted = @post_logs_retry_backoff.call(retries) end end begin res = @connection_pool.with do |conn| request = conn.headers( 'STATSIG-CLIENT-TIME' => (Time.now.to_f * 1000).to_i.to_s, 'CONTENT-ENCODING' => zipped ? 'gzip' : nil, 'STATSIG-EVENT-COUNT' => event_count == 0 ? nil : event_count.to_s ) case method when :GET request.get(url) when :POST request.post(url, body: body) end end rescue StandardError => e ## network error retry return nil, e unless retries.positive? sleep backoff_adjusted return request(method, url, body, retries - 1, backoff * @backoff_multiplier, zipped, event_count) end return res, nil if res.status.success? unless retries.positive? && RETRY_CODES.include?(res.code) return res, NetworkError.new("Got an exception when making request to #{url}: #{res.to_s}", res.status.to_i) end ## status code retry sleep backoff_adjusted request(method, url, body, retries - 1, backoff * @backoff_multiplier, zipped, event_count) end |