Class: LaunchDarkly::Impl::BigSegmentStoreManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/big_segments.rb

Overview

Since:

  • 5.5.0

Constant Summary collapse

EMPTY_MEMBERSHIP =

use this as a singleton whenever a membership query returns nil; it’s safe to reuse it because we will never modify the membership properties after they’re queried

Since:

  • 5.5.0

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(big_segments_config, logger) ⇒ BigSegmentStoreManager

Returns a new instance of BigSegmentStoreManager.

Since:

  • 5.5.0



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ldclient-rb/impl/big_segments.rb', line 18

def initialize(big_segments_config, logger)
  @store = big_segments_config.store
  @stale_after_millis = big_segments_config.stale_after * 1000
  @status_provider = BigSegmentStoreStatusProviderImpl.new(-> { get_status })
  @logger = logger
  @last_status = nil

  unless @store.nil?
    @cache = ExpiringCache.new(big_segments_config.context_cache_size, big_segments_config.context_cache_time)
    @poll_worker = RepeatingTask.new(big_segments_config.status_poll_interval, 0, -> { poll_store_and_update_status }, logger)
    @poll_worker.start
  end
end

Instance Attribute Details

#status_providerObject (readonly)

Since:

  • 5.5.0



32
33
34
# File 'lib/ldclient-rb/impl/big_segments.rb', line 32

def status_provider
  @status_provider
end

Class Method Details

.hash_for_context_key(context_key) ⇒ Object

Since:

  • 5.5.0



83
84
85
# File 'lib/ldclient-rb/impl/big_segments.rb', line 83

def self.hash_for_context_key(context_key)
  Digest::SHA256.base64digest(context_key)
end

Instance Method Details

#get_context_membership(context_key) ⇒ Object

Since:

  • 5.5.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ldclient-rb/impl/big_segments.rb', line 39

def get_context_membership(context_key)
  return nil unless @store
  membership = @cache[context_key]
  unless membership
    begin
      membership = @store.get_membership(BigSegmentStoreManager.hash_for_context_key(context_key))
      membership = EMPTY_MEMBERSHIP if membership.nil?
      @cache[context_key] = membership
    rescue => e
      LaunchDarkly::Util.log_exception(@logger, "Big Segment store membership query returned error", e)
      return BigSegmentMembershipResult.new(nil, BigSegmentsStatus::STORE_ERROR)
    end
  end
  poll_store_and_update_status unless @last_status
  unless @last_status.available
    return BigSegmentMembershipResult.new(membership, BigSegmentsStatus::STORE_ERROR)
  end
  BigSegmentMembershipResult.new(membership, @last_status.stale ? BigSegmentsStatus::STALE : BigSegmentsStatus::HEALTHY)
end

#get_statusObject

Since:

  • 5.5.0



59
60
61
# File 'lib/ldclient-rb/impl/big_segments.rb', line 59

def get_status
  @last_status || poll_store_and_update_status
end

#poll_store_and_update_statusObject

Since:

  • 5.5.0



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ldclient-rb/impl/big_segments.rb', line 63

def poll_store_and_update_status
  new_status = Interfaces::BigSegmentStoreStatus.new(false, false) # default to "unavailable" if we don't get a new status below
  unless @store.nil?
    begin
       = @store.
      new_status = Interfaces::BigSegmentStoreStatus.new(true, ! || stale?(.last_up_to_date))
    rescue => e
      LaunchDarkly::Util.log_exception(@logger, "Big Segment store status query returned error", e)
    end
  end
  @last_status = new_status
  @status_provider.update_status(new_status)

  new_status
end

#stale?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 5.5.0



79
80
81
# File 'lib/ldclient-rb/impl/big_segments.rb', line 79

def stale?(timestamp)
  !timestamp || ((Impl::Util.current_time_millis - timestamp) >= @stale_after_millis)
end

#stopObject

Since:

  • 5.5.0



34
35
36
37
# File 'lib/ldclient-rb/impl/big_segments.rb', line 34

def stop
  @poll_worker.stop unless @poll_worker.nil?
  @store.stop unless @store.nil?
end