1
1
module . exports = ( {
2
2
reverse_proxies,
3
- requestsPerSecond = 1 ,
3
+ requestsPerSecond = 5 ,
4
4
maxHostsPerPurge = 10 ,
5
5
} ) => {
6
- let queue = [ ] ;
6
+ let queue = new Set ( ) ; // Changed to Set to automatically handle duplicates
7
7
let isProcessing = false ;
8
8
let lastRequestTime = 0 ;
9
9
let currentBatchResolvers = [ ] ;
10
10
11
11
async function add ( hosts ) {
12
12
return new Promise ( ( resolve , reject ) => {
13
- queue . push ( ...hosts ) ;
13
+ // Add all hosts to the Set (duplicates will be automatically ignored)
14
+ hosts . forEach ( host => queue . add ( host ) ) ;
14
15
currentBatchResolvers . push ( { resolve, reject } ) ;
15
16
process ( ) ;
16
17
} ) ;
@@ -21,7 +22,7 @@ module.exports = ({
21
22
22
23
isProcessing = true ;
23
24
24
- while ( queue . length > 0 ) {
25
+ while ( queue . size > 0 ) {
25
26
const now = Date . now ( ) ;
26
27
const timeSinceLastRequest = now - lastRequestTime ;
27
28
const minimumGap = 1000 / requestsPerSecond ;
@@ -32,12 +33,15 @@ module.exports = ({
32
33
) ;
33
34
}
34
35
35
- const hostsBatch = queue . splice ( 0 , maxHostsPerPurge ) ;
36
+ // Convert part of the Set to Array for processing
37
+ const hostsBatch = Array . from ( queue ) . slice ( 0 , maxHostsPerPurge ) ;
38
+ // Remove processed hosts from the Set
39
+ hostsBatch . forEach ( host => queue . delete ( host ) ) ;
36
40
37
41
try {
38
42
await flushHosts ( hostsBatch ) ;
39
43
40
- if ( queue . length === 0 ) {
44
+ if ( queue . size === 0 ) {
41
45
// Resolve all promises when the entire queue is processed
42
46
currentBatchResolvers . forEach ( ( { resolve } ) => resolve ( ) ) ;
43
47
currentBatchResolvers = [ ] ;
@@ -46,7 +50,7 @@ module.exports = ({
46
50
// Reject all promises if there's an error
47
51
currentBatchResolvers . forEach ( ( { reject } ) => reject ( error ) ) ;
48
52
currentBatchResolvers = [ ] ;
49
- queue = [ ] ;
53
+ queue . clear ( ) ;
50
54
}
51
55
52
56
lastRequestTime = Date . now ( ) ;
@@ -59,7 +63,6 @@ module.exports = ({
59
63
for ( const reverse_proxy_url of reverse_proxies ) {
60
64
try {
61
65
const url = `${ reverse_proxy_url } /purge?${ hosts
62
- . filter ( ( host , index , self ) => self . indexOf ( host ) === index )
63
66
. map ( ( host ) => `host=${ encodeURIComponent ( host ) } ` )
64
67
. join ( "&" ) } `;
65
68
@@ -98,4 +101,4 @@ module.exports = ({
98
101
99
102
await add ( hosts ) ;
100
103
} ;
101
- } ;
104
+ } ;
0 commit comments