Comments (4)
👍 agreed
from node-pool.
You can do this in the create method. Perhaps doing something similar in your own code around acquire would work
Create Timeout
var poolModule = require('generic-pool')
var q = require('q')
var pool = poolModule.pool({
name: 'test-pool',
create: createFn
})
function createFn(cb) {
createWithPromise()
.then(function(item) {
return cb(null, item)
})
.fail(function(err) {
return cb(err)
})
}
function createWithPromise() {
var deferred = q.defer()
create()
setTimeout(rejectOnTimeout, 1000)
return deferred.promise
function rejectOnTimeout() {
var error = new Error('timeout reached')
return deferred.reject(error)
}
function create() {
var item = {}
// some async thing that might not complete before timeout
// as an example the deferred.resolve line below is commented out
// return deferred.resolve(item)
}
}
Note in the example above how the deferred is never resolved. If the deferred is resolved but after it is rejected, the resolve handler with be a no-op. This is because the q promises library has the very nice property that a promise can only be either resolved or rejected exactly once
Acquire Timeout
var timeoutInMilliseconds = 1000
acquireWithTimeout(timeoutInMilliseconds)
.then(function(client) {
console.log('got client')
// do something with the client here
})
.fail(function(err) {
console.log('failed to get client')
console.dir(err)
})
function acquireWithTimeout(timeout) {
var deferred = q.defer()
pool.acquireWithPromise()
.then(function(client) {
return deferred.resolve(client
})
}
setTimeout(reject, timeout)
function reject() {
var error = new Error('timeout reached trying to acquire client from pool')
return deferred.reject(error)
}
}
function acquireWithPromise() {
var deferred = q.defer()
pool.acquire(callback)
return deferred.promise
function callback(err, client) {
if (errr) {
return deferred.reject(err)
}
return deferred.resolve(client
}
}
from node-pool.
Right, I think it's best to control this behavior in the factory create
function.
from node-pool.
The timeout of creating resource is NOT the same as acquire timeout. i.e. the creation of resource is successful, but these resource is insufficient, so we need a timeout setting for acquire. Can we reconsider it?
from node-pool.
Related Issues (20)
- waitingClientsCount function has disappeared HOT 1
- resource release not getting called HOT 2
- NPM installation faliure HOT 2
- Add param to factory
- Feature: add pool.use method. HOT 1
- Borrowed resources never go higher than 1 HOT 2
- Share pool between servers in a cluster HOT 3
- How to work maxWaitingClients? HOT 1
- Add destroy timeout to prevent pool.clear from hanging when destroy promise never resolves HOT 4
- testOnReturn = no-op?
- What does happen if I don't release the resource and close the node? HOT 2
- Resource not currently part of this pool HOT 1
- `ready` method from Pool is missing from the TS definitions (v3.8.2)
- .use does not handle synchronous work functions
- Consistently occuring `ResourceRequest timed out` Error HOT 1
- TimeoutError: ResourceRequest timed out HOT 2
- Suggested Feature: borrowTimeoutMillis HOT 2
- Pool does not maintain the minimum HOT 1
- Suggestion on softEvictConnections
- AutoStart Completion Callback HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-pool.