Class: DevCycle::LocalBucketing

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb

Constant Summary collapse

BUFFER_HEADER_SIZE =
12
ARRAY_BUFFER_CLASS_ID =
1
STRING_CLASS_ID =
2
HEADER_ID =
9
@@rand =
Random.new(seed = Random.new_seed)
@@engine =
Wasmtime::Engine.new(parallel_compilation: false)
@@wasmmodule =

added to ensure that there is no processes deadlock when compiling wasm before forking

Wasmtime::Module.from_file(@@engine, "#{__dir__}/bucketing-lib.release.wasm")
@@wasi_ctx =
Wasmtime::WasiCtxBuilder.new
.inherit_stdout
.inherit_stderr
.set_argv(ARGV)
.set_env(ENV)
@@store =
Wasmtime::Store.new(@@engine, wasi_ctx: @@wasi_ctx)
@@linker =
Wasmtime::Linker.new(@@engine, wasi: true)
@@stack_tracer_raise =
lambda { |message| raise message }
@@stack_tracer =

each method reassigns stack_tracer so the call stack is properly displayed

lambda {}
@@instance =
@@linker.instantiate(@@store, @@wasmmodule)
@@memory =
@@instance.export("memory").to_memory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdkkey, options, wait_for_init) ⇒ LocalBucketing

Returns a new instance of LocalBucketing.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 96

def initialize(sdkkey, options, wait_for_init)
  @initialized = false
  @has_config = false
  @sdkkey = sdkkey
  @options = options
  @logger = options.logger
  @wasm_mutex = Mutex.new
  @variable_type_codes = {
    boolean: @@instance.export("VariableType.Boolean").to_global.get.to_i,
    string: @@instance.export("VariableType.String").to_global.get.to_i,
    number: @@instance.export("VariableType.Number").to_global.get.to_i,
    json: @@instance.export("VariableType.JSON").to_global.get.to_i
  }
  @buffer_header_addr = @@instance.export("__new").to_func.call(BUFFER_HEADER_SIZE, HEADER_ID)
  asc_pin(@buffer_header_addr)
  set_sdk_key_internal(sdkkey)
  platform_data = PlatformData.new('server', VERSION, RUBY_VERSION, nil, 'Ruby', Socket.gethostname)
  set_platform_data(platform_data)
  @config_manager = ConfigManager.new(@sdkkey, self, wait_for_init)
end

Instance Attribute Details

#has_configObject

Returns the value of attribute has_config.



18
19
20
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 18

def has_config
  @has_config
end

#initializedObject

Returns the value of attribute initialized.



17
18
19
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 17

def initialized
  @initialized
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 15

def options
  @options
end

#variable_type_codesObject (readonly)

Returns the value of attribute variable_type_codes.



16
17
18
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 16

def variable_type_codes
  @variable_type_codes
end

Instance Method Details

#check_event_queue_sizeObject



178
179
180
181
182
183
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 178

def check_event_queue_size
  @wasm_mutex.synchronize do
    @@stack_tracer = @@stack_tracer_raise
    @@instance.invoke("eventQueueSize", @sdkKeyAddr)
  end
end

#closeObject



117
118
119
120
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 117

def close
  @config_manager.close
  @config_manager = nil
end

#flush_event_queueObject



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 163

def flush_event_queue
  @wasm_mutex.synchronize do
    @@stack_tracer = @@stack_tracer_raise
    payload_addr = @@instance.invoke("flushEventQueue", @sdkKeyAddr)
    raw_json = read_asc_string(payload_addr)
    raw_payloads = Oj.load(raw_json)

    if raw_payloads == nil
      return []
    end
    raw_payloads.map { |raw_payload| EventsPayload.new(raw_payload["records"], raw_payload["payloadId"], raw_payload["eventCount"]) }
  end
end

#generate_bucketed_config(user) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 123

def generate_bucketed_config(user)
  @wasm_mutex.synchronize do
    user_addr = malloc_asc_byte_array(user.to_json)
    @@stack_tracer = @@stack_tracer_raise
    config_addr = @@instance.invoke("generateBucketedConfigForUserUTF8", @sdkKeyAddr, user_addr)
    bucketed_config_json = read_asc_byte_array(config_addr)
    bucketed_config_hash = Oj.load(bucketed_config_json)

    BucketedUserConfig.new(bucketed_config_hash['project'],
                          bucketed_config_hash['environment'],
                          bucketed_config_hash['features'],
                          bucketed_config_hash['featureVariationMap'],
                          bucketed_config_hash['variableVariationMap'],
                          bucketed_config_hash['variables'],
                          bucketed_config_hash['knownVariableKeys'])
  end
end

#init_event_queue(options) ⇒ Object



249
250
251
252
253
254
255
256
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 249

def init_event_queue(options)
  @wasm_mutex.synchronize do
    options_json = Oj.dump(options)
    options_addr = malloc_asc_string(options_json)
    @@stack_tracer = @@stack_tracer_raise
    @@instance.invoke("initEventQueue", @sdkKeyAddr, options_addr)
  end
end

#on_payload_failure(payload_id, retryable) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 195

def on_payload_failure(payload_id, retryable)
  @wasm_mutex.synchronize do
    payload_addr = malloc_asc_string(payload_id)
    @@stack_tracer = @@stack_tracer_raise
    @@instance.invoke("onPayloadFailure", @sdkKeyAddr, payload_addr, retryable ? 1 : 0)
  end
end

#on_payload_success(payload_id) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 186

def on_payload_success(payload_id)
  @wasm_mutex.synchronize do
    payload_addr = malloc_asc_string(payload_id)
    @@stack_tracer = @@stack_tracer_raise
    @@instance.invoke("onPayloadSuccess", @sdkKeyAddr, payload_addr)
  end
end

#queue_aggregate_event(event, bucketeduser) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 219

def queue_aggregate_event(event, bucketeduser)
  @wasm_mutex.synchronize do
    begin
      variable_variation_map =
        if !bucketeduser.nil?
          bucketeduser.variable_variation_map
        else
          {}
        end
      varmap_addr = malloc_asc_string(Oj.dump(variable_variation_map))
      asc_pin(varmap_addr)
      event_addr = malloc_asc_string(event.to_json)
      @@stack_tracer = @@stack_tracer_raise
      @@instance.invoke("queueAggregateEvent", @sdkKeyAddr, event_addr, varmap_addr)
    ensure
      asc_unpin(varmap_addr)
    end
  end
end

#queue_event(user, event) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 204

def queue_event(user, event)
  @wasm_mutex.synchronize do
    begin
      user_addr = malloc_asc_string(user.to_json)
      asc_pin(user_addr)
      event_addr = malloc_asc_string(event.to_json)
      @@stack_tracer = @@stack_tracer_raise
      @@instance.invoke("queueEvent", @sdkKeyAddr, user_addr, event_addr)
    ensure
      asc_unpin(user_addr)
    end
  end
end

#set_client_custom_data(custom_data) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 259

def set_client_custom_data(custom_data)
  @wasm_mutex.synchronize do
    custom_data_json = Oj.dump(custom_data, mode: :json)
    custom_data_addr = malloc_asc_byte_array(custom_data_json)
    @@stack_tracer = @@stack_tracer_raise
    @@instance.invoke("setClientCustomDataUTF8", @sdkKeyAddr, custom_data_addr)
  end
end

#store_config(config) ⇒ Object



240
241
242
243
244
245
246
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 240

def store_config(config)
  @wasm_mutex.synchronize do
    config_addr = malloc_asc_byte_array(config)
    @@stack_tracer = @@stack_tracer_raise
    @@instance.invoke("setConfigDataUTF8", @sdkKeyAddr, config_addr)
  end
end

#variable_for_user(user, key, variable_type) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 142

def variable_for_user(user, key, variable_type)
  @wasm_mutex.synchronize do
    user_addr = malloc_asc_string(user.to_json)
    key_addr = malloc_asc_string(key)
    @@stack_tracer = @@stack_tracer_raise
    var_addr = @@instance.invoke("variableForUser", @sdkKeyAddr, user_addr, key_addr, variable_type, 1)
    read_asc_string(var_addr)
  end
end

#variable_for_user_pb(bin_str) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/devcycle-ruby-server-sdk/localbucketing/local_bucketing.rb', line 153

def variable_for_user_pb(bin_str)
  @wasm_mutex.synchronize do
    @@stack_tracer = @@stack_tracer_raise
    params_addr = malloc_asc_byte_array(bin_str)
    var_addr = @@instance.invoke("variableForUser_PB", params_addr)
    read_asc_byte_array(var_addr)
  end
end