rx-healthcheck 0.1.8 → 0.1.10

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: 3ca68e3e5241e5baf4b9b2454ece5f8cc87889f018ac31dfdb8e2243d58107da
4
- data.tar.gz: eda14ecb4304cb18e4b95f2f0431d23b14c119c1b95e25622711cf5aa193019a
3
+ metadata.gz: 825528f73f53ce6f4a0da5495543859293ee6688e23cf1b1365206738e02e420
4
+ data.tar.gz: 2b447c79ac8bed5bf4e3ee84fc19e1eda62575368d714b5eb4ea472fcb59ed77
5
5
  SHA512:
6
- metadata.gz: f59ce880cd0cba664a0705b76393bdefa84a7b91e595c001b9d59bc0161be13b32d5736ffd57db204c495f2537045cc23932c0bebb51c13b94081664111fbd7b
7
- data.tar.gz: d333264758d8233c45b397cda440528a24474a2000b0ddb9950dbf87feb81444c464015e1d7aa27f241efabec21b37b231518b57fb1f3fe0fc05fc53b6c8cb12
6
+ metadata.gz: b10aa92da2efb24e41afc29637df1160c73e6af556be2a3db64d69949b8163c84a5a7b05c3050b52e2189231140669fdb94113554196cd7d2eaa5fa628e997f1
7
+ data.tar.gz: fa7b6088065d80115b39b844e7773ddd640720da0e7a69fba29a6552ae736b359d8dd32df31626fc6c3891c93cab978403b1506515feec72de2325a87c93323f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rx-healthcheck (0.1.8)
4
+ rx-healthcheck (0.1.10)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -17,6 +17,7 @@ GEM
17
17
  simplecov_json_formatter (0.1.3)
18
18
 
19
19
  PLATFORMS
20
+ ruby
20
21
  x86_64-linux
21
22
 
22
23
  DEPENDENCIES
data/README.md CHANGED
@@ -59,6 +59,28 @@ deep_secondary: []
59
59
 
60
60
  Each collection must contain 0 or more `Rx::Check` objects. Those checks will be performed when the health check is queried. Deep checks will always also run the readiness checks.
61
61
 
62
+ ### Cache
63
+
64
+ For deep health checks Rx by default will use an in-memory cache. While this is useful when running in production, during testing it could lead to unexpected results.
65
+ You can disable the in-memory cache by setting the cache key false in the options hash.
66
+
67
+ ```ruby
68
+ Rails.application.config.middleware.insert(
69
+ Rx::Middleware,
70
+ liveness: [Rx::Check::FileSystemCheck.new],
71
+ readiness: [
72
+ Rx::Check::FileSystemCheck.new,
73
+ Rx::Check::ActiveRecordCheck.new,
74
+ Rx::Check::HttpCheck.new("http://example.com"),
75
+ Rx::Check::GenericCheck.new(-> { $redis.ping == "PONG" }, "redis")],
76
+ deep_critical: [Rx::Check::HttpCheck.new("http://criticalservice.com/health")],
77
+ deep_secondary: [Rx::Check::HttpCheck.new("http://otherservice.com/health-check")],
78
+ options: {
79
+ cache: false
80
+ }
81
+ )
82
+ ```
83
+
62
84
  ### Deep end-point authorization
63
85
 
64
86
  It is considered as a good practice to protect the deep checks with a GUID to mitigate DDOS attacks. Hence you have 2 options to enable this. One is to use the `default_authorization` by passing the token to the authorization inside options hash, which you can use in a request with the authorization_token in the header of it. The other option would be to pass a lambda with an env argument (this gives you access to hash of request data) and have your own `custom_authorization`:
@@ -75,6 +97,19 @@ options: {
75
97
  }
76
98
  ```
77
99
 
100
+ ### Customizing health-check endpoints
101
+
102
+ By default, Rx will mount the health checks at `/liveness`, `/readiness`, and `/deep`. You can customize these endpoints by passing a `liveness_path`, `readiness_path`, and `deep_path` options to the middleware:
103
+
104
+ ```ruby
105
+ # in this example, liveness and readiness endpoints have been customized. the deep
106
+ # endpoint will use the default path (/deep).
107
+ Rx::Middleware.new(options: {
108
+ liveness_path: "/other_liveness",
109
+ readiness_path: "/other_readiness"
110
+ })
111
+ ```
112
+
78
113
  ## Contributing
79
114
 
80
115
  Bug reports and pull requests are welcome on GitHub at https://github.com/zachpendleton/rx.
@@ -2,7 +2,7 @@ require_relative "../util/heap"
2
2
 
3
3
  module Rx
4
4
  module Cache
5
- class InMemoryCache
5
+ class LRUCache
6
6
  def initialize
7
7
  @heap = Rx::Util::Heap.new do |a, b|
8
8
  a[1] < b[1]
@@ -0,0 +1,45 @@
1
+ require "thread"
2
+
3
+ module Rx
4
+ module Cache
5
+ class MapCache
6
+ def initialize
7
+ @data = {}
8
+ @lock = Mutex.new
9
+ end
10
+
11
+ def cache(k, expires_in = 60)
12
+ unless (value = get(k)).nil?
13
+ return value
14
+ end
15
+
16
+ value = yield
17
+ put(k, value, expires_in)
18
+ value
19
+ end
20
+
21
+ def get(k)
22
+ lock.synchronize do
23
+ value = data[k]
24
+ return nil unless value
25
+
26
+ if value[1] < Time.now
27
+ data.delete(k)
28
+ return nil
29
+ end
30
+
31
+ value[0]
32
+ end
33
+ end
34
+
35
+ def put(k, v, expires_in = 60)
36
+ lock.synchronize do
37
+ data[k] = [v, Time.now + expires_in]
38
+ end
39
+ end
40
+
41
+ private
42
+ attr_reader :data, :lock
43
+ end
44
+ end
45
+ end
data/lib/rx/middleware.rb CHANGED
@@ -4,7 +4,10 @@ module Rx
4
4
  class Middleware
5
5
  DEFAULT_OPTIONS = {
6
6
  cache: true,
7
- authorization: nil
7
+ authorization: nil,
8
+ liveness_path: "/liveness",
9
+ readiness_path: "/readiness",
10
+ deep_path: "/deep"
8
11
  }.freeze
9
12
 
10
13
  def initialize(app,
@@ -29,12 +32,12 @@ module Rx
29
32
  end
30
33
 
31
34
  case path(env)
32
- when "/liveness"
35
+ when @options[:liveness_path]
33
36
  ok = check_to_component(liveness_checks).map { |x| x[:status] == 200 }.all?
34
37
  liveness_response(ok)
35
- when "/readiness"
38
+ when @options[:readiness_path]
36
39
  readiness_response(check_to_component(readiness_checks))
37
- when "/deep"
40
+ when @options[:deep_path]
38
41
  if !Rx::Util::HealthCheckAuthorization.new(env, @options[:authorization]).ok?
39
42
  deep_response_authorization_failed
40
43
  else
@@ -55,15 +58,20 @@ module Rx
55
58
  :deep_secondary_checks, :options
56
59
 
57
60
  def cache_factory(options)
58
- unless options[:cache]
59
- return Rx::Cache::NoOpCache.new
61
+ case options[:cache]
62
+ when true
63
+ Rx::Cache::LRUCache.new
64
+ when "LRU"
65
+ Rx::Cache::LRUCache.new
66
+ when "MAP"
67
+ Rx::Cache::MapCache.new
68
+ else
69
+ Rx::Cache::NoOpCache.new
60
70
  end
61
-
62
- Rx::Cache::InMemoryCache.new
63
71
  end
64
72
 
65
73
  def health_check_request?(path)
66
- %w[/liveness /readiness /deep].include?(path)
74
+ [@options[:liveness_path], @options[:readiness_path], @options[:deep_path]].include?(path)
67
75
  end
68
76
 
69
77
  def liveness_response(is_ok)
data/lib/rx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rx
4
- VERSION = "0.1.8"
4
+ VERSION = "0.1.10"
5
5
  end
data/lib/rx.rb CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  require_relative "rx/version"
4
4
  require_relative "rx/middleware"
5
- require_relative "rx/cache/in_memory_cache"
5
+ require_relative "rx/cache/lru_cache"
6
+ require_relative "rx/cache/map_cache"
6
7
  require_relative "rx/cache/no_op_cache"
7
8
  require_relative "rx/check/active_record_check"
8
9
  require_relative "rx/check/file_system_check"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rx-healthcheck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Pendleton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-31 00:00:00.000000000 Z
11
+ date: 2022-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -26,7 +26,8 @@ files:
26
26
  - bin/console
27
27
  - bin/setup
28
28
  - lib/rx.rb
29
- - lib/rx/cache/in_memory_cache.rb
29
+ - lib/rx/cache/lru_cache.rb
30
+ - lib/rx/cache/map_cache.rb
30
31
  - lib/rx/cache/no_op_cache.rb
31
32
  - lib/rx/check/active_record_check.rb
32
33
  - lib/rx/check/file_system_check.rb
@@ -61,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  - !ruby/object:Gem::Version
62
63
  version: '0'
63
64
  requirements: []
64
- rubygems_version: 3.2.22
65
+ rubygems_version: 3.3.7
65
66
  signing_key:
66
67
  specification_version: 4
67
68
  summary: Standard health checks for Rails and Rack applications