Class: Statsig::Network
Instance Method Summary collapse
- #check_gate(user, gate_name) ⇒ Object
- #get_config(user, dynamic_config_name) ⇒ Object
-
#initialize(server_secret, options, backoff_mult = 10) ⇒ Network
constructor
A new instance of Network.
- #post_helper(endpoint, body, retries = 0, backoff = 1) ⇒ Object
- #post_logs(events) ⇒ Object
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 |
# File 'lib/network.rb', line 26 def initialize(server_secret, , backoff_mult = 10) super() URIHelper.initialize() @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 end |
Instance Method Details
#check_gate(user, gate_name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/network.rb', line 83 def check_gate(user, gate_name) begin request_body = JSON.generate({ 'user' => user&.serialize(false), 'gateName' => gate_name }) response, _ = post_helper('check_gate', request_body) return JSON.parse(response.body) unless response.nil? false rescue return false end end |
#get_config(user, dynamic_config_name) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/network.rb', line 94 def get_config(user, dynamic_config_name) begin request_body = JSON.generate({ 'user' => user&.serialize(false), 'configName' => dynamic_config_name }) response, _ = post_helper('get_config', request_body) return JSON.parse(response.body) unless response.nil? nil rescue return nil end end |
#post_helper(endpoint, body, retries = 0, backoff = 1) ⇒ Object
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 |
# File 'lib/network.rb', line 41 def post_helper(endpoint, body, retries = 0, backoff = 1) if @local_mode return nil, nil end = Statsig. http = HTTP.headers( { "STATSIG-API-KEY" => @server_secret, "STATSIG-CLIENT-TIME" => (Time.now.to_f * 1000).to_i.to_s, "STATSIG-SERVER-SESSION-ID" => @session_id, "Content-Type" => "application/json; charset=UTF-8", "STATSIG-SDK-TYPE" => ['sdkType'], "STATSIG-SDK-VERSION" => ['sdkVersion'], }).accept(:json) if @timeout http = http.timeout(@timeout) 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 = http.post(url, body: body) rescue StandardError => e ## network error retry return nil, e unless retries > 0 sleep backoff_adjusted return post_helper(endpoint, body, retries - 1, backoff * @backoff_multiplier) end return res, nil if res.status.success? return nil, NetworkError.new("Got an exception when making request to #{url}: #{res.to_s}", res.status.to_i) unless retries > 0 && $retry_codes.include?(res.code) ## status code retry sleep backoff_adjusted post_helper(endpoint, body, retries - 1, backoff * @backoff_multiplier) end |
#post_logs(events) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/network.rb', line 105 def post_logs(events) begin json_body = JSON.generate({ 'events' => events, 'statsigMetadata' => Statsig. }) post_helper('log_event', json_body, @post_logs_retry_limit) rescue end end |