title: Data structure to block when a list of element has reach its quota
body: <p>I have a 'key' that let me call a rest endpoint 500 times per minutes (each key can either be 200, 300 or 500)
   when the key has reached its quota within a 1 minute (1 min bucket not sliding window) the endpoint returns a 429 (rate limited) with a waiting delay. Once the delay has passed I can re-used again this key. </p>
  
  <p>My API is a kind of proxy (client call my API, my API calls an other service by providing one of the keys)
  My API would stack all my users keys so that in a sense it becomes 1 big key.
  and that when 'any' of my client call my api, my system would smartly chose 1 key which still has quota and when that key reached quota it removes it from the available keys list, or queue or whatever structure </p>
  
  <p>I'm having difficulties choosing a good data structure and logic for it. What I have in mind is something like this</p>
  
  <p>1) get a key which has still quota and has at least 1 remaining call available -1a) if no key are available just 'wait' until one becomes available</p>
  
  <p>2) call the endpoint</p>
  
  <p>3) Decrease the key quota of 1 and if the key got rate limited remove it from the 'queue' or 'list' or whatever.</p>
  
  <p>4) back to 1</p>
  
  <p>Note that the endpoint my API is calling returns useful information such as "requestRemaining" that indicate how much request a key has still left before it gets rate limited. "rate delay" that indicates when the key ca be used again in case it gets rate limited.</p>
  
  <p>I think a good solution here would be to pro-actively knowing when a key is going to be come rate limited instead of relying on the endpoint responses only. Like this we avoid making a call 'for nothing' to endpoint with a key that has reached quota.</p>
  
  <p>I have already tried something with DelayQueue and some semaphore to block when no keys are available but I have an issue knowing in advance if a key will become rate limited.
  I would like to be concurrent at the 'request' level not at the key level. Meaning that I don't want a client to lock a key until it made the request. If a key has a quota of 500 i would like at max 500 concurrent client to use 1 single key;</p>
  
  <p>I would love to have the input of someone more experienced on how they would approach this.</p>