-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsessionManager.js
More file actions
140 lines (115 loc) · 4.24 KB
/
sessionManager.js
File metadata and controls
140 lines (115 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import axios from 'axios';
import { EventEmitter } from 'events';
class FlareSolverrSessionManager extends EventEmitter {
constructor() {
super();
this.numSessions = parseInt(process.env.FLARESOLVERR_NUM_SESSIONS) || 3;
this.sessions = [];
this.requestQueue = [];
this.initialized = false;
}
async sendFlaresolverrRequest(cmd, sessionId = null, url = null) {
const payload = { cmd };
if (sessionId) {
payload.session = sessionId;
}
if (url) {
payload.url = url;
}
if (process.env.PROXY_SERVER) {
payload.proxy = { url: process.env.PROXY_SERVER };
}
const response = await axios.post(process.env.FLARESOLVERR_URL, payload, {
headers: { "Content-Type": "application/json" }
});
if (response.data.status !== 'ok') {
throw new Error(`FlareSolverr request failed: ${response.data.status} - ${response.data.message}`);
}
return response.data;
}
async initialize() {
if (this.initialized) return;
this.initialized = true;
console.log(`Initializing ${this.numSessions} FlareSolverr sessions...`);
for (let i = 0; i < this.numSessions; i++) {
const sessionId = `session_${i}_${Date.now()}`;
try {
await this.sendFlaresolverrRequest("sessions.create", sessionId);
this.sessions.push({
id: sessionId,
inUse: false,
createdAt: new Date()
});
console.log(`Created session: ${sessionId}`);
} catch (error) {
console.error(`Failed to create session ${sessionId}:`, error.message);
}
}
console.log(`Session manager initialized with ${this.sessions.length} sessions`);
}
getFreeSession() {
return this.sessions.find(session => !session.inUse);
}
async acquireSession() {
return new Promise((resolve, reject) => {
const freeSession = this.getFreeSession();
if (freeSession) {
freeSession.inUse = true;
resolve(freeSession);
} else {
this.requestQueue.push({ resolve, reject, timestamp: Date.now() });
console.log(`Request queued. Queue length: ${this.requestQueue.length}`);
}
});
}
releaseSession(sessionId) {
const session = this.sessions.find(s => s.id === sessionId);
if (session) {
session.inUse = false;
if (this.requestQueue.length > 0) {
const { resolve } = this.requestQueue.shift();
session.inUse = true;
resolve(session);
console.log(`Session ${sessionId} assigned to queued request. Queue length: ${this.requestQueue.length}`);
}
}
}
async makeRequest(url) {
if (!this.initialized) {
await this.initialize();
}
const session = await this.acquireSession();
try {
const response = await this.sendFlaresolverrRequest("request.get", session.id, url);
return response.solution.response;
} finally {
this.releaseSession(session.id);
}
}
getStats() {
return {
totalSessions: this.sessions.length,
activeSessions: this.sessions.filter(s => s.inUse).length,
queueLength: this.requestQueue.length,
sessions: this.sessions.map(s => ({
id: s.id,
inUse: s.inUse,
createdAt: s.createdAt
}))
};
}
async destroyAllSessions() {
console.log('Destroying all FlareSolverr sessions...');
for (const session of this.sessions) {
try {
await this.sendFlaresolverrRequest("sessions.destroy", session.id);
console.log(`Destroyed session: ${session.id}`);
} catch (error) {
console.error(`Failed to destroy session ${session.id}:`, error.message);
}
}
this.sessions = [];
this.initialized = false;
}
}
export default FlareSolverrSessionManager;