Class: Statsig::Network

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/network.rb

Defined Under Namespace

Classes: HttpMethod

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Network.



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
52
53
54
# File 'lib/network.rb', line 26

def initialize(server_secret, options, backoff_mult = 10)
  super()
  URIHelper.initialize(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_pool = ConnectionPool.new(size: 3) do
    meta = 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' => meta['sdkType'],
        'STATSIG-SDK-VERSION' => meta['sdkVersion'],
        'Accept-Encoding' => 'gzip'
      }
    ).accept(:json)
    if @timeout
      client = client.timeout(@timeout)
    end

    client
  end
end

Instance Method Details

#check_gate(user, gate_name) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/network.rb', line 134

def check_gate(user, gate_name)
  request_body = JSON.generate({ 'user' => user&.serialize(false), 'gateName' => gate_name })
  response, = post('check_gate', request_body)
  return JSON.parse(response.body) unless response.nil?

  false
rescue StandardError
  false
end

#download_config_specs(since_time) ⇒ Object



60
61
62
# File 'lib/network.rb', line 60

def download_config_specs(since_time)
  get("download_config_specs/#{@server_secret}.json?sinceTime=#{since_time}")
end

#get(endpoint, retries = 0, backoff = 1) ⇒ Object



75
76
77
# File 'lib/network.rb', line 75

def get(endpoint, retries = 0, backoff = 1)
  request(HttpMethod::GET, endpoint, nil, retries, backoff)
end

#get_config(user, dynamic_config_name) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/network.rb', line 144

def get_config(user, dynamic_config_name)
  request_body = JSON.generate({ 'user' => user&.serialize(false), 'configName' => dynamic_config_name })
  response, = post('get_config', request_body)
  return JSON.parse(response.body) unless response.nil?

  nil
rescue StandardError
  nil
end

#post(endpoint, body, retries = 0, backoff = 1) ⇒ Object



83
84
85
# File 'lib/network.rb', line 83

def post(endpoint, body, retries = 0, backoff = 1)
  request(HttpMethod::POST, endpoint, body, retries, backoff)
end

#post_logs(events) ⇒ Object



154
155
156
157
158
159
# File 'lib/network.rb', line 154

def post_logs(events)
  json_body = JSON.generate({ 'events' => events, 'statsigMetadata' => Statsig. })
  post('log_event', json_body, @post_logs_retry_limit)
rescue StandardError

end

#request(method, endpoint, body, retries = 0, backoff = 1) ⇒ Object



91
92
93
94
95
96
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
# File 'lib/network.rb', line 91

def request(method, endpoint, body, retries = 0, backoff = 1)
  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
  url = URIHelper.build_url(endpoint)
  begin
    res = @connection_pool.with do |conn|
      request = conn.headers('STATSIG-CLIENT-TIME' => (Time.now.to_f * 1000).to_i.to_s)
      case method
      when HttpMethod::GET
        request.get(url)
      when HttpMethod::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, endpoint, body, retries - 1, backoff * @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, endpoint, body, retries - 1, backoff * @backoff_multiplier)
end