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 +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +9 -0
- data/lib/belt/cli/routes_command.rb +1 -0
- data/lib/belt/route_dsl.rb +29 -2
- data/lib/belt/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 39a9c396d05b2acfc5c69adc875f0050beb9395a2dc9f31dd4ac2d2221e011db
|
|
4
|
+
data.tar.gz: ddf3e2d95ea98eafc808f0d2e144196257f4880fb1b8d65ae063dd27becb9213
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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:
|
data/lib/belt/route_dsl.rb
CHANGED
|
@@ -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