2019-09-02 14:24:02 +01:00
|
|
|
(function(global) {
|
2016-03-02 17:36:20 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
Stop AJAX updates queuing requests
The `updateContent` module updates a section of the page based on an
AJAX request.
The current implementation will poll every `x` seconds. If the server
takes a long time to respond, this results in a the browser queuing up
requests. If a particular endpoint is slow, this can result in one
client making enough requests to slow down the whole server, to the
point where it gets removed from the loadbalancer, eventually bringing
the whole site down.
This commit rewrites the module so that it queues up the render
operations, not the requests.
There is one queue per endpoint, so for
`http://example.com/endpoint.json`:
1. Queue is empty
```javascript
{
'http://example.com/endpoint.json': []
}
```
2. Inital re-render is put on the queue…
```javascript
{
'http://example.com/endpoint.json': [
function render(){…}
]
}
```
…AJAX request fires
```
GET http://example.com/endpoint.json
```
3. Every `x` seconds, another render operation is put on the queue
```javascript
{
'http://example.com/endpoint.json': [
function render(){…},
function render(){…},
function render(){…}
]
}
```
4. AJAX request returns queue is flushed by executing each queued
render function in sequence
```javascript
render(response); render(response); render(response);
```
```javascript
{
'http://example.com/endpoint.json': []
}
```
5. Repeat
This means that, at most, the AJAX requests will never fire more than
once every `x` seconds, where `x` defaults to `1.5`.
2016-06-11 12:02:47 +01:00
|
|
|
var queues = {};
|
2022-01-27 10:44:20 +00:00
|
|
|
var morphdom = global.GOVUK.vendor.morphdom;
|
2020-04-16 13:21:24 +01:00
|
|
|
var defaultInterval = 2000;
|
Delay AJAX calls if the server is slow to respond
By default our AJAX calls were 2 seconds. Then they were 5 seconds
because someone reckoned 2 seconds was putting too much load on the
system. Then we made them 10 seconds while we were having an incident.
Then we made them 20 seconds for the heaviest pages, but back to 5
seconds or 2 seconds for the rest of the pages.
This is not a good situation because:
- it slows all services down equally, no matter how much traffic they
have, or which features they have switched on
- it slows everything down by the same amount, no matter how much load
the platform is under
- the values are set based on our worst performance, until we manually
remember to switch them back
- we spend time during incidents deploying changes to slow down the
dashboard refresh time because it’s a nothing-to-lose change that
might relieve some symptoms, when we could be spending time digging
into the underlying cause
This pull request makes the Javascript smarter about how long it waits
until it makes another AJAX call. It bases the delay on how long the
server takes to respond (as a proxy for how much load the server is
under).
It’s based on the square root of the response time, so is more sensitive
to slow downs early on, and less sensitive to slow downs later on. This
helps us give a more pronounced difference in delay between an AJAX call
that is fast (for example the page for a single notification) and one
that is slow (for example a dashboard for a service with lots of
traffic).
*Some examples of what this would mean for various pages*
Page | Response time | Wait until next AJAX call
---|---|---
Check a reply to address | 130ms | 1,850ms
Brand new service dashboard | 229ms | 2,783ms
HM Passport Office dashboard | 634ms | 5,294ms
NHS Coronavirus Service dashboard | 779ms | 5,977ms
_Example of the kind of slowness we’ve seen during an incident_ | 6,000ms | 18,364ms
GOV.UK email dashboard | `HTTP 504` | 😬
2020-04-08 17:55:53 +01:00
|
|
|
var interval = 0;
|
|
|
|
|
|
|
|
|
|
var calculateBackoff = responseTime => parseInt(Math.max(
|
|
|
|
|
(250 * Math.sqrt(responseTime)) - 1000,
|
|
|
|
|
1000
|
|
|
|
|
));
|
2016-08-04 12:06:04 +01:00
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
// Methods to ensure the DOM fragment is clean of classes added by JS before diffing
|
|
|
|
|
// and that they are replaced afterwards.
|
2022-02-15 11:26:17 +00:00
|
|
|
//
|
|
|
|
|
// Added to allow the use of JS, in main.js, to apply styles which in future could be
|
|
|
|
|
// achieved with the :has pseudo-class. If :has is available in our supported browsers,
|
|
|
|
|
// this can be removed in favour of a CSS-only solution.
|
2022-02-16 16:08:48 +00:00
|
|
|
var ClassesPersister = function ($contents) {
|
|
|
|
|
this._$contents = $contents;
|
|
|
|
|
this._classNames = [];
|
|
|
|
|
this._classesTo$ElsMap = {};
|
|
|
|
|
};
|
|
|
|
|
ClassesPersister.prototype.addClassName = function (className) {
|
|
|
|
|
if (this._classNames.indexOf(className) === -1) {
|
|
|
|
|
this._classNames.push(className);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ClassesPersister.prototype.remove = function () {
|
|
|
|
|
// Store references to any elements with class names to persist
|
|
|
|
|
this._classNames.forEach(className => {
|
|
|
|
|
var $elsWithClassName = $('.' + className, this._$contents).removeClass(className);
|
2022-02-11 12:30:07 +00:00
|
|
|
|
2022-02-16 16:08:48 +00:00
|
|
|
if ($elsWithClassName.length > 0) {
|
|
|
|
|
this._classesTo$ElsMap[className] = $elsWithClassName;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
ClassesPersister.prototype.replace = function () {
|
2022-02-18 11:55:13 +00:00
|
|
|
var replaceClasses = (idx, el) => {
|
2022-02-16 15:56:52 +00:00
|
|
|
|
2022-02-18 11:55:13 +00:00
|
|
|
// Avoid updating elements that are no longer present.
|
|
|
|
|
// elements removed will still exist in memory but won't be attached to the DOM any more
|
|
|
|
|
if (global.document.body.contains(el)) {
|
|
|
|
|
$(el).addClass(className);
|
|
|
|
|
}
|
2022-02-16 15:56:52 +00:00
|
|
|
|
2022-02-18 11:55:13 +00:00
|
|
|
};
|
|
|
|
|
var className;
|
2022-02-08 11:33:13 +00:00
|
|
|
|
2022-02-18 11:55:13 +00:00
|
|
|
for (className in this._classesTo$ElsMap) {
|
|
|
|
|
this._classesTo$ElsMap[className].each(replaceClasses);
|
2022-02-08 11:33:13 +00:00
|
|
|
}
|
2022-02-16 16:08:48 +00:00
|
|
|
|
|
|
|
|
// remove references to elements
|
|
|
|
|
this._classesTo$ElsMap = {};
|
2022-02-08 11:33:13 +00:00
|
|
|
};
|
|
|
|
|
|
2022-02-16 16:08:48 +00:00
|
|
|
var getRenderer = ($contents, key, classesPersister) => response => {
|
2022-02-11 15:35:04 +00:00
|
|
|
classesPersister.remove();
|
2022-02-08 11:33:13 +00:00
|
|
|
morphdom(
|
2022-02-04 11:48:43 +00:00
|
|
|
$contents.get(0),
|
|
|
|
|
$(response[key]).get(0)
|
2022-02-08 11:33:13 +00:00
|
|
|
);
|
2022-02-11 15:35:04 +00:00
|
|
|
classesPersister.replace();
|
2022-02-08 11:33:13 +00:00
|
|
|
};
|
2016-03-02 17:36:20 +00:00
|
|
|
|
Stop AJAX updates queuing requests
The `updateContent` module updates a section of the page based on an
AJAX request.
The current implementation will poll every `x` seconds. If the server
takes a long time to respond, this results in a the browser queuing up
requests. If a particular endpoint is slow, this can result in one
client making enough requests to slow down the whole server, to the
point where it gets removed from the loadbalancer, eventually bringing
the whole site down.
This commit rewrites the module so that it queues up the render
operations, not the requests.
There is one queue per endpoint, so for
`http://example.com/endpoint.json`:
1. Queue is empty
```javascript
{
'http://example.com/endpoint.json': []
}
```
2. Inital re-render is put on the queue…
```javascript
{
'http://example.com/endpoint.json': [
function render(){…}
]
}
```
…AJAX request fires
```
GET http://example.com/endpoint.json
```
3. Every `x` seconds, another render operation is put on the queue
```javascript
{
'http://example.com/endpoint.json': [
function render(){…},
function render(){…},
function render(){…}
]
}
```
4. AJAX request returns queue is flushed by executing each queued
render function in sequence
```javascript
render(response); render(response); render(response);
```
```javascript
{
'http://example.com/endpoint.json': []
}
```
5. Repeat
This means that, at most, the AJAX requests will never fire more than
once every `x` seconds, where `x` defaults to `1.5`.
2016-06-11 12:02:47 +01:00
|
|
|
var getQueue = resource => (
|
|
|
|
|
queues[resource] = queues[resource] || []
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var flushQueue = function(queue, response) {
|
|
|
|
|
while(queue.length) queue.shift()(response);
|
2016-03-02 17:36:20 +00:00
|
|
|
};
|
|
|
|
|
|
2016-06-12 08:40:56 +01:00
|
|
|
var clearQueue = queue => (queue.length = 0);
|
|
|
|
|
|
Delay AJAX calls if the server is slow to respond
By default our AJAX calls were 2 seconds. Then they were 5 seconds
because someone reckoned 2 seconds was putting too much load on the
system. Then we made them 10 seconds while we were having an incident.
Then we made them 20 seconds for the heaviest pages, but back to 5
seconds or 2 seconds for the rest of the pages.
This is not a good situation because:
- it slows all services down equally, no matter how much traffic they
have, or which features they have switched on
- it slows everything down by the same amount, no matter how much load
the platform is under
- the values are set based on our worst performance, until we manually
remember to switch them back
- we spend time during incidents deploying changes to slow down the
dashboard refresh time because it’s a nothing-to-lose change that
might relieve some symptoms, when we could be spending time digging
into the underlying cause
This pull request makes the Javascript smarter about how long it waits
until it makes another AJAX call. It bases the delay on how long the
server takes to respond (as a proxy for how much load the server is
under).
It’s based on the square root of the response time, so is more sensitive
to slow downs early on, and less sensitive to slow downs later on. This
helps us give a more pronounced difference in delay between an AJAX call
that is fast (for example the page for a single notification) and one
that is slow (for example a dashboard for a service with lots of
traffic).
*Some examples of what this would mean for various pages*
Page | Response time | Wait until next AJAX call
---|---|---
Check a reply to address | 130ms | 1,850ms
Brand new service dashboard | 229ms | 2,783ms
HM Passport Office dashboard | 634ms | 5,294ms
NHS Coronavirus Service dashboard | 779ms | 5,977ms
_Example of the kind of slowness we’ve seen during an incident_ | 6,000ms | 18,364ms
GOV.UK email dashboard | `HTTP 504` | 😬
2020-04-08 17:55:53 +01:00
|
|
|
var poll = function(renderer, resource, queue, form) {
|
|
|
|
|
|
|
|
|
|
let startTime = Date.now();
|
2016-03-02 17:36:20 +00:00
|
|
|
|
2022-10-27 11:12:39 -04:00
|
|
|
if (document.visibilityState !== "hidden" && queue.push(renderer) === 1) {
|
|
|
|
|
$.ajax(
|
|
|
|
|
resource,
|
|
|
|
|
{
|
|
|
|
|
'method': form ? 'post' : 'get',
|
|
|
|
|
'data': form ? $('#' + form).serialize() : {}
|
2019-05-20 17:32:40 +01:00
|
|
|
}
|
2022-10-27 11:12:39 -04:00
|
|
|
).done(
|
|
|
|
|
response => {
|
|
|
|
|
flushQueue(queue, response);
|
|
|
|
|
if (response.stop === 1) {
|
|
|
|
|
poll = function(){};
|
|
|
|
|
}
|
|
|
|
|
interval = calculateBackoff(Date.now() - startTime);
|
|
|
|
|
}
|
|
|
|
|
).fail(
|
|
|
|
|
() => poll = function(){}
|
|
|
|
|
);
|
|
|
|
|
}
|
Stop AJAX updates queuing requests
The `updateContent` module updates a section of the page based on an
AJAX request.
The current implementation will poll every `x` seconds. If the server
takes a long time to respond, this results in a the browser queuing up
requests. If a particular endpoint is slow, this can result in one
client making enough requests to slow down the whole server, to the
point where it gets removed from the loadbalancer, eventually bringing
the whole site down.
This commit rewrites the module so that it queues up the render
operations, not the requests.
There is one queue per endpoint, so for
`http://example.com/endpoint.json`:
1. Queue is empty
```javascript
{
'http://example.com/endpoint.json': []
}
```
2. Inital re-render is put on the queue…
```javascript
{
'http://example.com/endpoint.json': [
function render(){…}
]
}
```
…AJAX request fires
```
GET http://example.com/endpoint.json
```
3. Every `x` seconds, another render operation is put on the queue
```javascript
{
'http://example.com/endpoint.json': [
function render(){…},
function render(){…},
function render(){…}
]
}
```
4. AJAX request returns queue is flushed by executing each queued
render function in sequence
```javascript
render(response); render(response); render(response);
```
```javascript
{
'http://example.com/endpoint.json': []
}
```
5. Repeat
This means that, at most, the AJAX requests will never fire more than
once every `x` seconds, where `x` defaults to `1.5`.
2016-06-11 12:02:47 +01:00
|
|
|
|
2016-08-04 12:06:04 +01:00
|
|
|
setTimeout(
|
2019-04-17 12:40:49 +01:00
|
|
|
() => poll.apply(window, arguments), interval
|
Stop AJAX updates queuing requests
The `updateContent` module updates a section of the page based on an
AJAX request.
The current implementation will poll every `x` seconds. If the server
takes a long time to respond, this results in a the browser queuing up
requests. If a particular endpoint is slow, this can result in one
client making enough requests to slow down the whole server, to the
point where it gets removed from the loadbalancer, eventually bringing
the whole site down.
This commit rewrites the module so that it queues up the render
operations, not the requests.
There is one queue per endpoint, so for
`http://example.com/endpoint.json`:
1. Queue is empty
```javascript
{
'http://example.com/endpoint.json': []
}
```
2. Inital re-render is put on the queue…
```javascript
{
'http://example.com/endpoint.json': [
function render(){…}
]
}
```
…AJAX request fires
```
GET http://example.com/endpoint.json
```
3. Every `x` seconds, another render operation is put on the queue
```javascript
{
'http://example.com/endpoint.json': [
function render(){…},
function render(){…},
function render(){…}
]
}
```
4. AJAX request returns queue is flushed by executing each queued
render function in sequence
```javascript
render(response); render(response); render(response);
```
```javascript
{
'http://example.com/endpoint.json': []
}
```
5. Repeat
This means that, at most, the AJAX requests will never fire more than
once every `x` seconds, where `x` defaults to `1.5`.
2016-06-11 12:02:47 +01:00
|
|
|
);
|
|
|
|
|
};
|
2016-03-02 17:36:20 +00:00
|
|
|
|
2019-09-02 14:24:02 +01:00
|
|
|
global.GOVUK.Modules.UpdateContent = function() {
|
2016-03-02 17:36:20 +00:00
|
|
|
|
2022-02-08 11:33:13 +00:00
|
|
|
this.start = component => {
|
|
|
|
|
var $component = $(component);
|
2022-02-04 11:48:43 +00:00
|
|
|
var $contents = $component.children().eq(0);
|
|
|
|
|
var key = $component.data('key');
|
|
|
|
|
var resource = $component.data('resource');
|
|
|
|
|
var form = $component.data('form');
|
2022-02-16 16:08:48 +00:00
|
|
|
var classesPersister = new ClassesPersister($contents);
|
2022-02-04 11:48:43 +00:00
|
|
|
|
2022-02-15 11:26:17 +00:00
|
|
|
// Replace component with contents.
|
|
|
|
|
// The renderer does this anyway when diffing against the first response
|
2022-02-04 11:48:43 +00:00
|
|
|
$component.replaceWith($contents);
|
2022-02-08 11:33:13 +00:00
|
|
|
|
2022-02-15 11:26:17 +00:00
|
|
|
// Store any classes that should persist through updates
|
|
|
|
|
//
|
|
|
|
|
// Added to allow the use of JS, in main.js, to apply styles which in future could be
|
|
|
|
|
// achieved with the :has pseudo-class. If :has is available in our supported browsers,
|
|
|
|
|
// this can be removed in favour of a CSS-only solution.
|
2022-02-08 11:33:13 +00:00
|
|
|
if ($contents.data('classesToPersist') !== undefined) {
|
|
|
|
|
$contents.data('classesToPersist')
|
|
|
|
|
.split(' ')
|
2022-02-11 15:40:47 +00:00
|
|
|
.forEach(className => classesPersister.addClassName(className));
|
2022-02-08 11:33:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeout(
|
|
|
|
|
() => poll(
|
2022-02-16 16:08:48 +00:00
|
|
|
getRenderer($contents, key, classesPersister),
|
2022-02-04 11:48:43 +00:00
|
|
|
resource,
|
|
|
|
|
getQueue(resource),
|
|
|
|
|
form
|
2022-02-08 11:33:13 +00:00
|
|
|
),
|
|
|
|
|
defaultInterval
|
|
|
|
|
);
|
|
|
|
|
};
|
2016-03-02 17:36:20 +00:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
Delay AJAX calls if the server is slow to respond
By default our AJAX calls were 2 seconds. Then they were 5 seconds
because someone reckoned 2 seconds was putting too much load on the
system. Then we made them 10 seconds while we were having an incident.
Then we made them 20 seconds for the heaviest pages, but back to 5
seconds or 2 seconds for the rest of the pages.
This is not a good situation because:
- it slows all services down equally, no matter how much traffic they
have, or which features they have switched on
- it slows everything down by the same amount, no matter how much load
the platform is under
- the values are set based on our worst performance, until we manually
remember to switch them back
- we spend time during incidents deploying changes to slow down the
dashboard refresh time because it’s a nothing-to-lose change that
might relieve some symptoms, when we could be spending time digging
into the underlying cause
This pull request makes the Javascript smarter about how long it waits
until it makes another AJAX call. It bases the delay on how long the
server takes to respond (as a proxy for how much load the server is
under).
It’s based on the square root of the response time, so is more sensitive
to slow downs early on, and less sensitive to slow downs later on. This
helps us give a more pronounced difference in delay between an AJAX call
that is fast (for example the page for a single notification) and one
that is slow (for example a dashboard for a service with lots of
traffic).
*Some examples of what this would mean for various pages*
Page | Response time | Wait until next AJAX call
---|---|---
Check a reply to address | 130ms | 1,850ms
Brand new service dashboard | 229ms | 2,783ms
HM Passport Office dashboard | 634ms | 5,294ms
NHS Coronavirus Service dashboard | 779ms | 5,977ms
_Example of the kind of slowness we’ve seen during an incident_ | 6,000ms | 18,364ms
GOV.UK email dashboard | `HTTP 504` | 😬
2020-04-08 17:55:53 +01:00
|
|
|
global.GOVUK.Modules.UpdateContent.calculateBackoff = calculateBackoff;
|
|
|
|
|
|
2019-09-02 14:24:02 +01:00
|
|
|
})(window);
|