|
7 | 7 |
|
8 | 8 | import path from "path";
|
9 | 9 | import SocketIOFile from "socket.io-file";
|
10 |
| -import { cacheDirectory } from '@best/utils'; |
| 10 | +import { cacheDirectory, randomAlphanumeric } from '@best/utils'; |
11 | 11 | import { x as extractTar } from 'tar';
|
12 | 12 | import { Socket } from "socket.io";
|
13 | 13 |
|
14 | 14 | // This is all part of the initialization
|
15 |
| -const LOADER_CONFIG = { |
16 |
| - uploadDir: path.join(cacheDirectory('best_agent'), 'uploads'), |
| 15 | +const LOADER_CONFIG_DEFAULTS = { |
17 | 16 | accepts: [],
|
18 | 17 | maxFileSize: 52428800, // 50 mb
|
19 | 18 | chunkSize: 10240, // 10kb
|
20 | 19 | transmissionDelay: 0,
|
21 | 20 | overwrite: true,
|
22 | 21 | };
|
23 | 22 |
|
24 |
| -// In order to make the uploader singleton, but yet allow multiple file downloads we need to do some manual cleanup |
25 |
| -// The assumption is only one upload can occurr at the time, otherwise this code might not work as expected |
26 |
| - |
27 | 23 | export function getUploaderInstance(socket: Socket): SocketIOFile {
|
28 |
| - const uploader: any = new SocketIOFile(socket, LOADER_CONFIG); |
| 24 | + // In case multiple agents are connected to the same hub and multiple benchmarks are invoked concurrently, |
| 25 | + // if more than one benchmark has the exact same name, an error could occur because of a race condition. |
| 26 | + // This race condition is triggered when one client is uploading to the hub while the hub is uploading |
| 27 | + // same-named benchmark to the agent. When this happens, the agent may get a partial file or the hub may fail |
| 28 | + // because there is a lock on the same-named file. |
| 29 | + const config = Object.assign({}, LOADER_CONFIG_DEFAULTS, { |
| 30 | + uploadDir: path.join(cacheDirectory('best_agent'), 'uploads', randomAlphanumeric(16)) |
| 31 | + }); |
| 32 | + |
| 33 | + const uploader: any = new SocketIOFile(socket, config); |
29 | 34 | uploader.load = function () {
|
30 | 35 | return new Promise((resolve, reject) => {
|
31 | 36 | uploader.on('complete', (info: any) => {
|
|
0 commit comments