Class: Statsig::Network

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

Constant Summary collapse

CONFIG_SYNC_RETRY_LIMIT =

Background config-spec sync retries transient failures with a short, bounded exponential backoff, matching statsig-server-core's background sync strategy (3 retries; 2^n * 100ms => 200ms, 400ms, 800ms). These are fixed rather than user-configurable, mirroring server-core where the values are hardcoded.

3
CONFIG_SYNC_RETRY_BACKOFF_SECONDS =
0.2
CONFIG_SYNC_RETRY_BACKOFF_MULTIPLIER =
2

Instance Method Summary collapse

Constructor Details

#initialize(server_secret, options, backoff_mult = 10) ⇒ Network

Returns a new instance of Network.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/network.rb', line 30

def initialize(server_secret, options, backoff_mult = 10)
  super()
  @options = options
  @server_secret = server_secret
  @local_mode = options.local_mode
  @timeout = options.network_timeout
  @backoff_multiplier = backoff_mult
  @post_logs_retry_backoff = options.post_logs_retry_backoff
  @post_logs_retry_limit = options.post_logs_retry_limit
  @session_id = SecureRandom.uuid
  @connection_pools = {}
  @connection_pools_mutex = Mutex.new
end

Instance Method Details

#download_config_specs(since_time, context) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/network.rb', line 44

def download_config_specs(since_time, context)
  url = @options.download_config_specs_url
  dcs_url = "#{url}#{@server_secret}.json"
  if since_time.positive?
    dcs_url += "?sinceTime=#{since_time}"
  end
  if context == 'initialize'
    return get(dcs_url, @options.initialize_retry_limit)
  end
  get(dcs_url, CONFIG_SYNC_RETRY_LIMIT, CONFIG_SYNC_RETRY_BACKOFF_SECONDS, CONFIG_SYNC_RETRY_BACKOFF_MULTIPLIER)
end

#download_config_specs_fallback(since_time, context) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/network.rb', line 56

def download_config_specs_fallback(since_time, context)
  dcs_url = "#{STATSIG_CDN_DCS_BASE}/download_config_specs/#{@server_secret}.json"
  if since_time.positive?
    dcs_url += "?sinceTime=#{since_time}"
  end
  get(dcs_url)
end

#download_id_list(url, start_byte = 0) ⇒ Object



91
92
93
# File 'lib/network.rb', line 91

def download_id_list(url, start_byte = 0)
  request(:GET, url, nil, 0, 1, false, 0, { 'Range' => "bytes=#{start_byte}-" }, false)
end

#get(url, retries = 0, backoff = 1, backoff_multiplier = @backoff_multiplier) ⇒ Object



95
96
97
# File 'lib/network.rb', line 95

def get(url, retries = 0, backoff = 1, backoff_multiplier = @backoff_multiplier)
  request(:GET, url, nil, retries, backoff, false, 0, {}, true, backoff_multiplier)
end

#get_id_lists(context) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/network.rb', line 83

def get_id_lists(context)
  url = @options.get_id_lists_url
  if context == 'initialize'
    return post(url, JSON.generate({ 'statsigMetadata' => Statsig. }), @options.initialize_retry_limit)
  end
  post(url, JSON.generate({ 'statsigMetadata' => Statsig. }))
end

#post(url, body, retries = 0, backoff = 1, zipped = false, event_count = 0) ⇒ Object



99
100
101
# File 'lib/network.rb', line 99

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



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

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)

  unless e == nil
    message = "Failed to log #{event_count} events after #{@post_logs_retry_limit} retries"
    puts "[Statsig]: #{message}"
    error_boundary.log_exception(e, tag: 'statsig::log_event_failed', extra: { eventCount: event_count, error: message }, force: true)
    return
  end
rescue StandardError

end

#request(method, url, body, retries = 0, backoff = 1, zipped = false, event_count = 0, extra_headers = {}, use_statsig_headers = true, backoff_multiplier = @backoff_multiplier) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/network.rb', line 117

def request(method, url, body, retries = 0, backoff = 1, zipped = false, event_count = 0, extra_headers = {}, use_statsig_headers = true, backoff_multiplier = @backoff_multiplier)
  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
    pool = connection_pool_for(url)
    res = pool.with do |conn|
      begin
        request_headers = build_request_headers(zipped, event_count, extra_headers, use_statsig_headers)
        request = conn.headers(request_headers)

        response = case method
                   when :GET
                     request.get(url)
                   when :POST
                     request.post(url, body: body)
                   end

        # Fully drain the response before the client returns to the pool.
        response.body.to_s
        response
      rescue StandardError
        pool.discard_current_connection(&:close)
        raise
      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, extra_headers, use_statsig_headers, backoff_multiplier)
  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, extra_headers, use_statsig_headers, backoff_multiplier)
end

#shutdownObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/network.rb', line 103

def shutdown
  pools = @connection_pools_mutex.synchronize do
    pools = @connection_pools.values
    @connection_pools = {}
    pools
  end

  pools.each do |pool|
    pool.shutdown do |conn|
      conn.close if conn.respond_to?(:close)
    end
  end
end