-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpool-scaling.js
More file actions
98 lines (83 loc) · 3.04 KB
/
pool-scaling.js
File metadata and controls
98 lines (83 loc) · 3.04 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
const sql = require('msnodesqlv8')
const { TestEnv } = require('../../test/env/test-env')
const env = new TestEnv()
const connectionString = env.connectionString
// Example configurations for different scaling strategies
async function demonstrateScalingStrategies () {
console.log('Pool Scaling Strategies Demo\n')
// Configuration examples
const configs = {
aggressive: {
connectionString,
ceiling: 50,
floor: 0,
heartbeatSecs: 20,
inactivityTimeoutSecs: 60,
scalingStrategy: 'aggressive' // Default - creates all 50 connections at once
},
gradual: {
connectionString,
ceiling: 50,
floor: 0,
heartbeatSecs: 20,
inactivityTimeoutSecs: 60,
scalingStrategy: 'gradual',
scalingIncrement: 5, // Creates 5 connections at a time
scalingDelay: 100 // 100ms delay between each connection creation
},
exponential: {
connectionString,
ceiling: 50,
floor: 1,
heartbeatSecs: 20,
inactivityTimeoutSecs: 60,
scalingStrategy: 'exponential',
scalingFactor: 1.5, // Grows by 50% each time (1->2->3->5->8->12->18->27->40->50)
scalingDelay: 50 // 50ms delay between connections
}
}
console.log('1. Aggressive Strategy (Original Behavior):')
console.log(' - Creates all 50 connections immediately')
console.log(' - Fastest startup but highest resource spike')
console.log(' - Config:', JSON.stringify(configs.aggressive, null, 2))
console.log('\n2. Gradual Strategy:')
console.log(' - Creates connections in increments of 5')
console.log(' - Smoother resource usage, more predictable')
console.log(' - Takes longer to reach full capacity')
console.log(' - Config:', JSON.stringify(configs.gradual, null, 2))
console.log('\n3. Exponential Strategy:')
console.log(' - Starts small and grows exponentially')
console.log(' - Balances between speed and resource usage')
console.log(' - Growth pattern: 1->2->3->5->8->12->18->27->40->50')
console.log(' - Config:', JSON.stringify(configs.exponential, null, 2))
console.log('\n\nUsage Example:')
console.log(`
const pool = new sql.Pool(configs.gradual)
pool.on('debug', msg => console.log('Pool:', msg))
pool.open((err) => {
if (err) {
console.error('Failed to open pool:', err)
return
}
// Pool will grow gradually as queries are executed
pool.query('SELECT 1', (err, results) => {
// ...
})
})
`)
console.log('\nKey Benefits:')
console.log('- Gradual: Prevents connection storms, good for shared databases')
console.log('- Exponential: Adapts to load quickly while avoiding initial spike')
console.log('- Configurable delays: Prevents overwhelming the database server')
console.log('- Maintains backward compatibility with aggressive as default')
}
async function run () {
try {
await demonstrateScalingStrategies()
} catch (err) {
console.error('Failed to open pool:', err)
}
}
run().then(() => {
console.log('Pool Scaling Strategies Demo\n')
}).catch(err => { console.error('Failed to open pool:', err) })