How to implement pull?

I want to implement pull. Came to mind to make a map with the connections and to make the counter "open connections". After connection add connection to the pool. When reusing it removed from map (without touching the counter), and as soon as he released it back to return to the map. And Mapa to pull the connection randomly. If available connections are not present that to check how many connections (the value of the counter) and if they are less than the maximum available then create a new one. Otherwise, throw in a turn (if you please then enter a description of the logic queues in the answers)

I don't need code. I want to know I had the right train of thought.
July 9th 19 at 10:54
2 answers
July 9th 19 at 10:56
Solution
Sorry
then just add that
the old connection should(can) be removed,
to consider appeals from different threads.
If you reach the limit, you can block the call whether to return fail
Why would a random choice - there is no need.
Additional checks for correctness of the code, the consideration of the dual returns
Ideally, the return could be done in destruere well, or the equivalent in that language, or defer or whatever
And why kill the old connection? - Brody_Dickins commented on July 9th 19 at 10:59
Then.what if 90% of the time is used 3 connection,probably makes no sense to keep open 1000 - mercedes commented on July 9th 19 at 11:02
: and that is true. Thank you. - Brody_Dickins commented on July 9th 19 at 11:05
July 9th 19 at 10:58
Solution
Random to pull too much. If we are talking about database connections - they are identical.
And we must not forget to return to pool - there might be an ambush.

Find more questions by tags GoData storageProgramming