Draft for copy from throttle
This commit is contained in:
parent
7a3e4d617d
commit
5a116c69e9
@ -12,7 +12,7 @@ const errorHandlerFactory = require('../services/error_handler_factory');
|
|||||||
const StreamCopy = require('../services/stream_copy');
|
const StreamCopy = require('../services/stream_copy');
|
||||||
const StreamCopyMetrics = require('../services/stream_copy_metrics');
|
const StreamCopyMetrics = require('../services/stream_copy_metrics');
|
||||||
const zlib = require('zlib');
|
const zlib = require('zlib');
|
||||||
const { PassThrough } = require('stream');
|
const { PassThrough, Transform } = require('stream');
|
||||||
const handleQueryMiddleware = require('../middlewares/handle-query');
|
const handleQueryMiddleware = require('../middlewares/handle-query');
|
||||||
|
|
||||||
function CopyController(metadataBackend, userDatabaseService, userLimitsService, logger) {
|
function CopyController(metadataBackend, userDatabaseService, userLimitsService, logger) {
|
||||||
@ -97,6 +97,26 @@ function handleCopyTo (logger) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Throttle extends Transform {
|
||||||
|
constructor (pgstream, ...args) {
|
||||||
|
super(...args);
|
||||||
|
|
||||||
|
this.pgstream = pgstream;
|
||||||
|
|
||||||
|
this.timeout = setTimeout(() => {
|
||||||
|
pgstream.emit('error', new Error('Connection closed by server: no data received'));
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
_transform (chunk, encoding, callback) {
|
||||||
|
if (!this.timeout._destroyed) {
|
||||||
|
clearTimeout(this.timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function handleCopyFrom (logger) {
|
function handleCopyFrom (logger) {
|
||||||
return function handleCopyFromMiddleware (req, res, next) {
|
return function handleCopyFromMiddleware (req, res, next) {
|
||||||
const { sql, userDbParams, user, dbRemainingQuota } = res.locals;
|
const { sql, userDbParams, user, dbRemainingQuota } = res.locals;
|
||||||
@ -113,6 +133,8 @@ function handleCopyFrom (logger) {
|
|||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const throttle = new Throttle(pgstream);
|
||||||
|
|
||||||
req
|
req
|
||||||
.on('data', data => isGzip ? metrics.addGzipSize(data.length) : undefined)
|
.on('data', data => isGzip ? metrics.addGzipSize(data.length) : undefined)
|
||||||
.on('error', err => {
|
.on('error', err => {
|
||||||
@ -120,6 +142,7 @@ function handleCopyFrom (logger) {
|
|||||||
pgstream.emit('error', err);
|
pgstream.emit('error', err);
|
||||||
})
|
})
|
||||||
.on('close', () => pgstream.emit('error', new Error('Connection closed by client')))
|
.on('close', () => pgstream.emit('error', new Error('Connection closed by client')))
|
||||||
|
.pipe(throttle)
|
||||||
.pipe(decompress)
|
.pipe(decompress)
|
||||||
.on('data', data => {
|
.on('data', data => {
|
||||||
metrics.addSize(data.length);
|
metrics.addSize(data.length);
|
||||||
|
Loading…
Reference in New Issue
Block a user