From 8c99ca8e74d33f0247b5356fe6df4ed56772dc62 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Thu, 11 Sep 2025 15:41:19 +0100 Subject: [PATCH 1/4] CLDC-4066: Implement a debounce on address search disappointingly the underlying library https://github.com/alphagov/accessible-autocomplete doesn't have this as an option, though we can implement one ourselves in the fetch code --- .../controllers/address_search_controller.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/frontend/controllers/address_search_controller.js b/app/frontend/controllers/address_search_controller.js index de54090ec..833bc317f 100644 --- a/app/frontend/controllers/address_search_controller.js +++ b/app/frontend/controllers/address_search_controller.js @@ -4,10 +4,28 @@ import 'accessible-autocomplete/dist/accessible-autocomplete.min.css' const options = [] +let latestQueryId = 0; + +const sleep = (ms) => { + return new Promise(resolve => setTimeout(resolve, ms)); +} + const fetchOptions = async (query, searchUrl) => { if (query.length < 2) { throw new Error('Query must be at least 2 characters long.') } + + // implement a debounce + // this is because this API has periods of high latency if OS Places has an outage + // making too many requests can overwhelm the number of threads available on the server + // which can in turn cause a site wide outage + latestQueryId++; + const myQueryId = latestQueryId; + await sleep(500); + if (myQueryId !== latestQueryId) { + throw new Error('Outdated query, ignoring result.'); + } + try { const response = await fetch(`${searchUrl}?query=${encodeURIComponent(query.trim())}`) return await response.json() From fee374b5f0bb1e2b83d60b0709a5564c54d4cdc9 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Thu, 11 Sep 2025 15:49:06 +0100 Subject: [PATCH 2/4] CLDC-4066: Reduce read timeout for OS Places APIs 30 -> 15 in practice waiting up to 1 min 30 for requests could cause too many threads to be blocked under periods of slow OS Places API this reduces the max wait time to 45 seconds, as requested by CORE --- app/services/address_client.rb | 2 +- app/services/uprn_client.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/address_client.rb b/app/services/address_client.rb index 20cf603fe..3fca0a0d0 100644 --- a/app/services/address_client.rb +++ b/app/services/address_client.rb @@ -35,7 +35,7 @@ private client.use_ssl = true client.verify_mode = OpenSSL::SSL::VERIFY_PEER client.max_retries = 3 - client.read_timeout = 30 # seconds + client.read_timeout = 15 # seconds client end diff --git a/app/services/uprn_client.rb b/app/services/uprn_client.rb index f847c7da5..b758a0380 100644 --- a/app/services/uprn_client.rb +++ b/app/services/uprn_client.rb @@ -39,7 +39,7 @@ private client.use_ssl = true client.verify_mode = OpenSSL::SSL::VERIFY_PEER client.max_retries = 3 - client.read_timeout = 30 # seconds + client.read_timeout = 20 # seconds client end From bb645a40f94e52850339aa50654aaa3db782d9b0 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Thu, 11 Sep 2025 17:13:52 +0100 Subject: [PATCH 3/4] CLDC-4066: Lint --- .../controllers/address_search_controller.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/frontend/controllers/address_search_controller.js b/app/frontend/controllers/address_search_controller.js index 833bc317f..b3410e49d 100644 --- a/app/frontend/controllers/address_search_controller.js +++ b/app/frontend/controllers/address_search_controller.js @@ -4,10 +4,10 @@ import 'accessible-autocomplete/dist/accessible-autocomplete.min.css' const options = [] -let latestQueryId = 0; +let latestQueryId = 0 const sleep = (ms) => { - return new Promise(resolve => setTimeout(resolve, ms)); + return new Promise(resolve => setTimeout(resolve, ms)) } const fetchOptions = async (query, searchUrl) => { @@ -19,11 +19,11 @@ const fetchOptions = async (query, searchUrl) => { // this is because this API has periods of high latency if OS Places has an outage // making too many requests can overwhelm the number of threads available on the server // which can in turn cause a site wide outage - latestQueryId++; - const myQueryId = latestQueryId; - await sleep(500); + latestQueryId++ + const myQueryId = latestQueryId + await sleep(500) if (myQueryId !== latestQueryId) { - throw new Error('Outdated query, ignoring result.'); + throw new Error('Outdated query, ignoring result.') } try { From f12ac9d4693646678e4b1ea1a2af368dc7f53f26 Mon Sep 17 00:00:00 2001 From: Samuel Young Date: Fri, 12 Sep 2025 10:56:36 +0100 Subject: [PATCH 4/4] CLDC-4066: Fix uprn timeout to match address search --- app/services/uprn_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/uprn_client.rb b/app/services/uprn_client.rb index b758a0380..8dcd2e7a0 100644 --- a/app/services/uprn_client.rb +++ b/app/services/uprn_client.rb @@ -39,7 +39,7 @@ private client.use_ssl = true client.verify_mode = OpenSSL::SSL::VERIFY_PEER client.max_retries = 3 - client.read_timeout = 20 # seconds + client.read_timeout = 15 # seconds client end