belt 0.6.4 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2fcb629702530cd06ffb9af298dcd259c4fc0503107ef19ab58a58da476617b4
4
- data.tar.gz: 167a12259478e5079620446f76dad474cde1d456f08b328663fe11f82860fa66
3
+ metadata.gz: 39a9c396d05b2acfc5c69adc875f0050beb9395a2dc9f31dd4ac2d2221e011db
4
+ data.tar.gz: ddf3e2d95ea98eafc808f0d2e144196257f4880fb1b8d65ae063dd27becb9213
5
5
  SHA512:
6
- metadata.gz: bb37dd0fe9e0cd392a0824d14b0ab4c51c5d002b36240fc7795ad11ce2313dd0014bea535c020fab6e4ec8988c7cf4d5a90992e2f8cfcdbcea35bc780586592f
7
- data.tar.gz: d8d7423e5b3cd0c3d1598ff7b9e82446c9de4705b68313ade0ee0479ec26a87b5d4e3acd23a52b664ec2f50fd37e66cb046e1383f18bbf696cd01a13d24b140e
6
+ metadata.gz: f3463e2d5e75fc653437accecb76a3a66683cd28bf7781a9aadbef167b2cf3f18ad4d5e32fb3cb5f57c6e406929253f6f762ebecec5a4d20b2723c9a8118efd8
7
+ data.tar.gz: f1ccdeca8439a8063940c981f9c05a24abb319bcb0aa76c7212e0883fc6a9c4db7ee10f0e2da1f93105d1bca68844bd108ca7e820b8a462c900d309d35231905
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.5
4
+
5
+ ### Added
6
+
7
+ - **Per-route throttling in `routes.rb`.** Any route or resource accepts
8
+ `throttle: { rate: 2, burst: 5 }` (requests/second + burst). `belt routes -f json` emits it
9
+ as `throttle` on the route and conveyor-belt 0.0.20+ applies it as API Gateway stage method
10
+ settings, winning over the Lambda's `throttle:` in `config/lambda/<fn>.yml`. Malformed values
11
+ raise at load time. (Fizzy #1756)
12
+
3
13
  ## 0.6.4
4
14
 
5
15
  ### Added
data/README.md CHANGED
@@ -209,6 +209,15 @@ end
209
209
 
210
210
  **Key point:** `namespace` and `scope` are purely organizational — they affect URL paths and controller module resolution but never change which Lambda handles the request. Use `function` when you need routes to go to a different Lambda.
211
211
 
212
+ **Per-route throttling:** any route or resource accepts `throttle: { rate:, burst: }` (steady-state requests per second plus the burst the token bucket absorbs). `belt routes -f json` emits it and the conveyor-belt provider (0.0.20+) applies it as API Gateway stage method settings. A route's `throttle` wins over the serving Lambda's `throttle:` in `config/lambda/<fn>.yml`, which in turn wins over the provider's stage-level `throttle` block.
213
+
214
+ ```ruby
215
+ gateway :api, auth: :none do
216
+ post '/connect/chat', to: 'chat#create', throttle: { rate: 2, burst: 5 }
217
+ resources :leads, only: [:create], throttle: { rate: 1, burst: 3 }
218
+ end
219
+ ```
220
+
212
221
  ### Nested Resource DSL
213
222
 
214
223
  Inside a `resources` block, you can use Rails-like `member`, `collection`, `scope`, and singular `resource` for DRYer route definitions:
@@ -165,6 +165,7 @@ module Belt
165
165
  }
166
166
  rc = route.response_context.to_s
167
167
  hash[:response_context] = rc unless rc.empty?
168
+ hash[:throttle] = route.throttle if route.throttle
168
169
  hash
169
170
  end
170
171
 
@@ -9,7 +9,7 @@ module Belt
9
9
 
10
10
  class Route
11
11
  attr_reader :method, :path, :auth, :lambda, :cors, :tables, :route_type,
12
- :controller, :action, :request_model, :response_model, :response_context
12
+ :controller, :action, :request_model, :response_model, :response_context, :throttle
13
13
 
14
14
  def initialize(method, path, options = {})
15
15
  @method = method.to_s.upcase
@@ -17,6 +17,7 @@ module Belt
17
17
  @auth = options[:auth]
18
18
  @lambda = options[:lambda]
19
19
  @cors = options.fetch(:cors, true)
20
+ @throttle = self.class.normalize_throttle(options[:throttle])
20
21
  @tables = options[:tables] || []
21
22
  @route_type = options[:route_type] || :action
22
23
  @controller = options[:controller]
@@ -26,6 +27,31 @@ module Belt
26
27
  @response_context = options[:response_context]&.to_s
27
28
  end
28
29
 
30
+ # Per-route API Gateway limit: `throttle: { rate: 2, burst: 5 }` (requests per
31
+ # second, token bucket). Emitted by `belt routes -f json` and applied by the
32
+ # conveyor-belt provider (0.0.20+) as stage method settings; it wins over the
33
+ # Lambda's `throttle:` in config/lambda/<fn>.yml. Returns nil when unset.
34
+ def self.normalize_throttle(raw)
35
+ return nil if raw.nil?
36
+
37
+ unless raw.is_a?(Hash)
38
+ raise ArgumentError, "throttle must be a Hash like { rate: 2, burst: 5 } (got #{raw.inspect})"
39
+ end
40
+
41
+ rate = raw[:rate] || raw['rate']
42
+ burst = raw[:burst] || raw['burst']
43
+ raise ArgumentError, 'throttle needs rate (requests/second) and burst' if rate.nil? || burst.nil?
44
+
45
+ unless rate.is_a?(Numeric) && rate.positive?
46
+ raise ArgumentError, "throttle rate must be a number > 0 (got #{rate.inspect})"
47
+ end
48
+ unless burst.is_a?(Integer) && burst.positive?
49
+ raise ArgumentError, "throttle burst must be an integer > 0 (got #{burst.inspect})"
50
+ end
51
+
52
+ { rate: rate, burst: burst }
53
+ end
54
+
29
55
  def resource?
30
56
  @route_type == :resource || @route_type == :resources
31
57
  end
@@ -494,7 +520,8 @@ module Belt
494
520
  action: action,
495
521
  request_model: options[:request_model],
496
522
  response_model: options[:response_model],
497
- response_context: options[:response_context]
523
+ response_context: options[:response_context],
524
+ throttle: options[:throttle]
498
525
  }
499
526
 
500
527
  @routes << Belt::Route.new(method, path, route_options)
data/lib/belt/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Belt
4
- VERSION = '0.6.4'
4
+ VERSION = '0.6.5'
5
5
 
6
6
  # Ruby version new apps run on in Lambda. Rendered into the conveyor-belt
7
7
  # provider block (`ruby_version`) by `belt new` / `belt generate environment`.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: belt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stowzilla