Hacker News new | ask | show | jobs
by lukesandberg 4829 days ago
This isn't actually a singleton. If the singleton is requested while it is being opened initially (by another request) then two connections will be opened. There needs to be some additional state tracking whether or the connection is currently being opened, so concurrent requests can wait for it. Unless the author just doesn't care... but this isn't a singleton.
1 comments

Be careful of double-checked locking errors: http://en.wikipedia.org/wiki/Double-checked_locking
seeing as this is node you won't need a lock (and since it is single threaded there are no cross thread visibility issues).

What you do need is a state variable that indicates if the connection is uninitialized, loading, or ready. And if it is in the loading state you need to have a list of callbacks so that concurrent requests can be added to the list.