bearcat 1.5.6 → 1.5.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f21ad17cf7cd2e8157f04e429943c0483c42d5cb7438f3e8708ad6f85f58bc9d
4
- data.tar.gz: 6350855a61b9d18121eab67e1f09ebbc3625ac8a52a1f6b669bf5ac6f47ee447
3
+ metadata.gz: 8a944f30f69dd137902c99b5810bb29eebbdf48136039160b5259106f722661e
4
+ data.tar.gz: 044c52dc104b65aac858da9f53f44de11ad8ac08a956cd9fdd21fb458a0d6b0f
5
5
  SHA512:
6
- metadata.gz: 621f03512619d6845e278882918b7ada0d2bb81ad7b1cc3737f4ebe0527b753709df2a4e17b7b44ad7416fe0634fb06b57d61a6c680e6c1e62c2adcf68485dee
7
- data.tar.gz: 3ebdcc2ba0809d4407eaba804b3cec69f4f7ea7b9182cebd60738f1de0c112017793e9db267e54f63c76091b251d37c71bc0d4871234ee21d2a091d14f07a603
6
+ metadata.gz: a02a9c9a4ff44e03da5a5e3b0de9aefa1565814aeb2ab8d2099d90eaf9388fae9de67118905b3fa4d72ec6684092b1ece95bc68bdd486494d221a2e05ace823f
7
+ data.tar.gz: 0a42a5dfee36522129ad182a88318891de98da90b6f120f269a93ea8f041d72b6482c0a77e711b1c75873356af10ea508c826e497a7c2c78b2b6481ce7e2c8ba
data/bearcat.gemspec CHANGED
@@ -33,4 +33,5 @@ Gem::Specification.new do |gem|
33
33
 
34
34
  gem.add_dependency "activesupport"
35
35
  gem.add_dependency "footrest", ">= 0.2.2"
36
+ gem.add_dependency "connection_pool", ">= 2.2.0", "< 3.0"
36
37
  end
@@ -85,8 +85,8 @@ module Bearcat
85
85
  rl = config[:rate_limiter] || Bearcat.rate_limiter
86
86
  master_rate_limit = config[:master_rate_limit].present? ? config[:master_rate_limit] : Bearcat.master_rate_limit
87
87
 
88
- if rl.nil? && master_rate_limit.nil? && defined?(Rails) && Rails.env.production?
89
- master_rate_limit = true if defined?(::Sidekiq)
88
+ if rl.nil? && master_rate_limit.nil? && defined?(Rails) && Rails.env.production? && defined?(::Redis) && RedisConnection.configured?("BEARCAT", explicit: false)
89
+ master_rate_limit = true
90
90
  end
91
91
 
92
92
  if rl.nil? && master_rate_limit
@@ -58,11 +58,10 @@ module Bearcat
58
58
  end
59
59
 
60
60
  def redis(&blk)
61
- # TODO Add bearcat-native ConnectionPool instead of relying on application or Sidekiq to do so
62
61
  if @params[:redis]
63
62
  @params[:redis].call(blk)
64
63
  else
65
- ::Sidekiq.redis(&blk)
64
+ ::Bearcat.redis(&blk)
66
65
  end
67
66
  end
68
67
  end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "connection_pool"
4
+ require "redis"
5
+ require "uri"
6
+
7
+ # Adapted from Sidekiq 6.x Implementation
8
+
9
+ module Bearcat
10
+ module RedisConnection
11
+ KNOWN_REDIS_URL_ENVS = %w[REDIS_URL REDISTOGO_URL REDISCLOUD_URL].freeze
12
+
13
+ class << self
14
+ def configured?(prefix, explicit: false)
15
+ determine_redis_provider(prefix: prefix, explicit: true)
16
+ end
17
+
18
+ def create(options = {})
19
+ symbolized_options = options.transform_keys(&:to_sym)
20
+
21
+ if !symbolized_options[:url] && (u = determine_redis_provider(prefix: options[:env_prefix]))
22
+ symbolized_options[:url] = u
23
+ end
24
+
25
+ size = if symbolized_options[:size]
26
+ symbolized_options[:size]
27
+ elsif symbolized_options[:env_prefix] && (v = ENV["#{symbolized_options[:env_prefix]}_REDIS_POOL_SIZE"])
28
+ Integer(v)
29
+ elsif ENV["RAILS_MAX_THREADS"]
30
+ Integer(ENV["RAILS_MAX_THREADS"])
31
+ else
32
+ 5
33
+ end
34
+
35
+ pool_timeout = symbolized_options[:pool_timeout] || 1
36
+
37
+ ConnectionPool.new(timeout: pool_timeout, size: size) do
38
+ namespace = symbolized_options[:namespace]
39
+ client = Redis.new client_opts(symbolized_options)
40
+
41
+ if namespace
42
+ begin
43
+ require "redis/namespace"
44
+ Redis::Namespace.new(namespace, redis: client)
45
+ rescue LoadError
46
+ Rails.logger.error("Your Redis configuration uses the namespace '#{namespace}' but the redis-namespace gem is not included in the Gemfile." \
47
+ "Add the gem to your Gemfile to continue using a namespace. Otherwise, remove the namespace parameter.")
48
+ exit(-127)
49
+ end
50
+ else
51
+ client
52
+ end
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def client_opts(options)
59
+ opts = options.dup
60
+ if opts[:namespace]
61
+ opts.delete(:namespace)
62
+ end
63
+
64
+ if opts[:network_timeout]
65
+ opts[:timeout] = opts[:network_timeout]
66
+ opts.delete(:network_timeout)
67
+ end
68
+
69
+ opts[:reconnect_attempts] ||= 1
70
+
71
+ opts
72
+ end
73
+
74
+ def determine_redis_provider(prefix: nil, explicit: false)
75
+ vars = []
76
+
77
+ if prefix.present?
78
+ if (ptr = ENV["#{prefix}_REDIS_PROVIDER"]).present?
79
+ return ENV[ptr]
80
+ else
81
+ vars.push(*KNOWN_REDIS_URL_ENVS.map { |e| ENV["#{prefix}_#{e}"] })
82
+ end
83
+ end
84
+
85
+ if !explicit || !prefix.present?
86
+ if (ptr = ENV["REDIS_PROVIDER"]).present?
87
+ vars << ptr # Intentionally not a return
88
+ else
89
+ vars.push(*KNOWN_REDIS_URL_ENVS)
90
+ end
91
+ end
92
+
93
+ vars.select!(&:present?)
94
+
95
+ vars.each do |e|
96
+ return ENV[e] if ENV[e].present?
97
+ end
98
+
99
+ nil
100
+ end
101
+ end
102
+ end
103
+ end
@@ -1,3 +1,3 @@
1
1
  module Bearcat
2
- VERSION = '1.5.6' unless defined?(Bearcat::VERSION)
2
+ VERSION = '1.5.7' unless defined?(Bearcat::VERSION)
3
3
  end
data/lib/bearcat.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'bearcat/version'
2
2
  require 'bearcat/client'
3
+ require 'bearcat/redis_connection'
3
4
 
4
5
  module Bearcat
5
6
  class << self
@@ -27,6 +28,7 @@ module Bearcat
27
28
  @max_sleep_seconds ||= 60
28
29
  end
29
30
 
31
+ # @deprecated
30
32
  def master_rate_limit
31
33
  @master_rate_limit ||= false
32
34
  end
@@ -37,5 +39,16 @@ module Bearcat
37
39
  @logger.level = Logger::DEBUG
38
40
  @logger
39
41
  end
42
+
43
+ def redis_pool
44
+ @redis_pool ||= RedisConnection.create(env_prefix: "BEARCAT")
45
+ end
46
+
47
+ def redis
48
+ raise ArgumentError, "requires a block" unless block_given?
49
+ redis_pool.with do |conn|
50
+ yield conn
51
+ end
52
+ end
40
53
  end
41
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bearcat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.6
4
+ version: 1.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Instructure CustomDev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-15 00:00:00.000000000 Z
11
+ date: 2023-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -122,6 +122,26 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: 0.2.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: connection_pool
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 2.2.0
132
+ - - "<"
133
+ - !ruby/object:Gem::Version
134
+ version: '3.0'
135
+ type: :runtime
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 2.2.0
142
+ - - "<"
143
+ - !ruby/object:Gem::Version
144
+ version: '3.0'
125
145
  description: Ruby interface for interacting with the canvas API
126
146
  email:
127
147
  - pseng@instructure.com
@@ -187,6 +207,7 @@ files:
187
207
  - lib/bearcat/rate_limiting/functions.lua
188
208
  - lib/bearcat/rate_limiting/increment_bucket.lua
189
209
  - lib/bearcat/rate_limiting/redis_script.rb
210
+ - lib/bearcat/redis_connection.rb
190
211
  - lib/bearcat/spec_helpers.rb
191
212
  - lib/bearcat/version.rb
192
213
  - lib/catalogcat.rb