Class: RubyRemoteConfig::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository: Repository, refresh_interval: Integer) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_remote_config.rb', line 13

def initialize(repository: Repository, refresh_interval: Integer)
  # Create the Client instance with the provided repository and refresh interval.
  @repository = repository
  @refresh_interval = refresh_interval

  @cancel = Concurrent::MutexAtomicBoolean.new(false)

  # Refresh the configuration data for the first time to ensure the
  # Client is initialized with the latest data before it is used.
  @repository.refresh

  # Start the background refresh goroutine by calling the refresh function
  # with the newly created context and the client as arguments.
  Thread.new { refresh() }

  # Return the created Client instance, which is now ready to use.
  self
end

Instance Attribute Details

#cancelObject (readonly)

Returns the value of attribute cancel.



12
13
14
# File 'lib/ruby_remote_config.rb', line 12

def cancel
  @cancel
end

#refresh_intervalObject (readonly)

Returns the value of attribute refresh_interval.



12
13
14
# File 'lib/ruby_remote_config.rb', line 12

def refresh_interval
  @refresh_interval
end

#repositoryObject (readonly)

Returns the value of attribute repository.



12
13
14
# File 'lib/ruby_remote_config.rb', line 12

def repository
  @repository
end

Instance Method Details

#refreshObject



32
33
34
35
36
37
38
# File 'lib/ruby_remote_config.rb', line 32

def refresh
  loop do
    sleep(refresh_interval)
    @repository.refresh()
    break if @cancel
  end
end

#stopObject



40
41
42
# File 'lib/ruby_remote_config.rb', line 40

def stop
  @cancel.make_true
end