Skip to content

Commit ba96dfe

Browse files
committed
Rate limit requests to GOV.UK Notify
This limits the number of requests we send to GOV.UK Notify each minute to ensure that we stay within their rate limits. If we were to breach these limits, the jobs would keep retrying until they succeed.
1 parent 0e5f2a5 commit ba96dfe

File tree

7 files changed

+33
-13
lines changed

7 files changed

+33
-13
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module NotifyThrottlingConcern
4+
extend ActiveSupport::Concern
5+
6+
include Sidekiq::Job
7+
include Sidekiq::Throttled::Job
8+
9+
included do
10+
self.queue_adapter = :sidekiq unless Rails.env.test?
11+
12+
sidekiq_throttle_as :notify
13+
14+
queue_as :notifications
15+
end
16+
end

app/jobs/email_delivery_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class EmailDeliveryJob < NotifyDeliveryJob
4+
include NotifyThrottlingConcern
5+
46
def perform(
57
template_name,
68
consent: nil,

app/jobs/notify_delivery_job.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
require "notifications/client"
44

55
class NotifyDeliveryJob < ApplicationJob
6-
self.queue_adapter = :sidekiq unless Rails.env.test?
7-
8-
queue_as :notifications
9-
10-
retry_on Notifications::Client::ServerError, wait: :polynomially_longer
11-
126
def self.client
137
@client ||=
148
Notifications::Client.new(
@@ -20,13 +14,9 @@ def self.deliveries
2014
@deliveries ||= []
2115
end
2216

23-
def self.send_via_notify?
24-
Settings.govuk_notify&.enabled
25-
end
17+
def self.send_via_notify? = Settings.govuk_notify&.enabled
2618

27-
def self.send_via_test?
28-
Rails.env.test?
29-
end
19+
def self.send_via_test? = Rails.env.test?
3020

3121
class UnknownTemplate < StandardError
3222
end

app/jobs/sms_delivery_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class SMSDeliveryJob < NotifyDeliveryJob
4+
include NotifyThrottlingConcern
5+
46
def perform(
57
template_name,
68
consent: nil,

app/jobs/status_updater_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
class StatusUpdaterJob < NotifyDeliveryJob
3+
class StatusUpdaterJob < ApplicationJob
44
include GoodJob::ActiveJobExtensions::Concurrency
55

66
queue_as :statuses

config/initializers/sidekiq.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
config.cooldown_threshold = 1000
1919
end
2020

21+
# https://docs.notifications.service.gov.uk/rest-api.html#rate-limits
22+
Sidekiq::Throttled::Registry.add(
23+
:notify,
24+
threshold: {
25+
limit: Settings.govuk_notify.rate_limit_per_minute.to_i,
26+
period: 1.minute
27+
}
28+
)
29+
2130
Sidekiq::Throttled::Registry.add(
2231
:pds,
2332
threshold: {

config/settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ govuk_notify:
2828
team_key: <%= Rails.application.credentials.govuk_notify&.team_key %>
2929
live_key: <%= Rails.application.credentials.govuk_notify&.live_key %>
3030
callback_bearer_token: <%= Rails.application.credentials.govuk_notify&.callback_bearer_token %>
31+
rate_limit_per_minute: 3000
3132

3233
nhs_api:
3334
api_key: <%= Rails.application.credentials.nhs_api&.api_key %>

0 commit comments

Comments
 (0)