MCPcopy Create free account
hub / github.com/nodejs/undici / connectionPoolingExample

Function connectionPoolingExample

docs/examples/socks5-proxy.js:99–131  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

97
98// Example with connection pooling
99async function connectionPoolingExample () {
100 console.log('\n=== Connection Pooling Example ===')
101
102 try {
103 const socks5Proxy = new Socks5Agent('socks5://localhost:1080', {
104 connections: 5, // Allow up to 5 concurrent connections
105 pipelining: 1 // Enable HTTP/1.1 pipelining
106 })
107
108 // Make multiple concurrent requests
109 const requests = []
110 for (let i = 0; i < 3; i++) {
111 requests.push(
112 request(`http://httpbin.org/delay/${i}`, {
113 dispatcher: socks5Proxy
114 })
115 )
116 }
117
118 console.log('Making 3 concurrent requests...')
119 const responses = await Promise.all(requests)
120
121 for (let i = 0; i < responses.length; i++) {
122 console.log(`Request ${i + 1} status:`, responses[i].statusCode)
123 // Consume body to avoid warnings
124 await responses[i].body.dump()
125 }
126
127 await socks5Proxy.close()
128 } catch (error) {
129 console.error('Error:', error.message)
130 }
131}
132
133// Example with error handling
134async function errorHandlingExample () {

Callers 1

runExamplesFunction · 0.85

Calls 5

dumpMethod · 0.80
closeMethod · 0.65
requestFunction · 0.50
pushMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected