Class: Xcflushd::PriorityAuthRenewer

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

Overview

Apart from flushing all the cached reports and renewing the authorizations periodically, we need to provide a mechanism to renew a specific auth at any time. The information needed is the combination of service, application credentials and metric.

When the client looks for the auth of a combination in the cache, it might not be there. It could be an authorization that has never been cached or one that has expired. In that case, we need to provide a way to check a specific authorization without waiting for the next flush cycle.

We use Redis publish/subscribe to solve this problem. We use 2 different type of channels:

1) Auth requests channel. It's the channel where the client specifies the
   combinations that need to be checked. xcflushd is subscribed to the
   channel. There is only one channel of this type.
2) Responses channel. Every time there's a request for a specific
   combination, a channel of this type is created. The client is
   subscribed to this channel, and xcflushd will publish the authorization
   status once it gets it from 3scale.

Instance Method Summary collapse

Constructor Details

#initialize(authorizer, storage, redis_pub, redis_sub, auth_ttl, logger, threads) ⇒ PriorityAuthRenewer

We need two separate Redis clients: one for subscribing to a channel and the other one to publish to different channels. It is specified in the Redis website: redis.io/topics/pubsub



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/xcflushd/priority_auth_renewer.rb', line 29

def initialize(authorizer, storage, redis_pub, redis_sub,
               auth_ttl, logger, threads)
  @authorizer = authorizer
  @storage = storage
  @redis_pub = redis_pub
  @redis_sub = redis_sub
  @auth_ttl = auth_ttl
  @logger = logger

  # We can receive several requests to renew the authorization of a
  # combination while we are already renewing it. We want to avoid
  # performing several calls to 3scale asking for the same thing. For that
  # reason, we use a map to keep track of the combinations that we are
  # renewing.
  # This map is updated from different threads. We use Concurrent::Map to
  # ensure thread-safety.
  @current_auths = Concurrent::Map.new

  @thread_pool = Concurrent::FixedThreadPool.new(threads)
end

Instance Method Details

#shutdownObject



50
51
52
# File 'lib/xcflushd/priority_auth_renewer.rb', line 50

def shutdown
  @thread_pool.shutdown
end

#startObject



62
63
64
65
66
67
68
69
70
# File 'lib/xcflushd/priority_auth_renewer.rb', line 62

def start
  begin
    subscribe_to_requests_channel
  rescue StandardError => e
    logger.error("PriorityAuthRenewer can't subscribe to the requests "\
                 "channel - #{e.class} #{e.message} #{e.cause}")
    raise e
  end
end

#terminateObject



58
59
60
# File 'lib/xcflushd/priority_auth_renewer.rb', line 58

def terminate
  @thread_pool.kill
end

#wait_for_termination(secs = nil) ⇒ Object



54
55
56
# File 'lib/xcflushd/priority_auth_renewer.rb', line 54

def wait_for_termination(secs = nil)
  @thread_pool.wait_for_termination(secs)
end