File: round_robin.rb

package info (click to toggle)
ruby-gitlab-experiment 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,202; makefile: 7
file content (40 lines) | stat: -rw-r--r-- 1,089 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

# The round robin strategy will assign the next variant in the list, looping back to the first variant after all
# variants have been assigned. This is useful for very small sample sizes where very even distribution can be required.
#
# Requires a cache to be configured.
#
# Keeps track of the number of assignments into the experiment group, and uses this to rotate "round robin" style
# through the variants that are defined.
#
# Example configuration usage:
#
# config.default_rollout = Gitlab::Experiment::Rollout::RoundRobin.new
#
# Example class usage:
#
# class PillColorExperiment < ApplicationExperiment
#   control { }
#   variant(:red) { }
#   variant(:blue) { }
#
#   # Rotate evenly between all behaviors.
#   default_rollout :round_robin
# end
#
module Gitlab
  class Experiment
    module Rollout
      class RoundRobin < Base
        KEY_NAME = :last_round_robin_variant

        protected

        def execute_assignment
          behavior_names[(cache.attr_inc(KEY_NAME) - 1) % behavior_names.size]
        end
      end
    end
  end
end