title: How to timeout a semaphore
body: <p>Semaphore in Go is implemented with a channel:</p>
  <p>An example is this:
  <a href="https://sites.google.com/site/gopatterns/concurrency/semaphores" rel="nofollow noreferrer">https://sites.google.com/site/gopatterns/concurrency/semaphores</a></p>
  <p><strong>Context:</strong></p>
  <p>We have a few hundred servers and there are shared resources that we want to limit access to. So for a given resource, we want to use a semaphore to limit access to only 5 concurrent access by those servers. In order to do that, we are planning to use a lock server. When a machine accesses the resource, it will first register with the lock server that it is accessing the resource by a key. And then when it is done, it will send another request to the lock server to say that its done and release the semaphore. This ensures that we limit access to those resources to a maximal number of concurrent access.</p>
  <p><strong>Problem:</strong> Want to handle this gracefully if something goes wrong.</p>
  <p><strong>Question</strong>:</p>
  <p>How do you go about implementing a timeout on the semaphore?</p>
  <p><strong>Example:</strong></p>
  <p>Let's say I have a semaphore size of 5. There are simultaneously 10 processes trying to acquire a lock in the semaphore so in this case only 5 will acquire it.</p>
  <p>Sometimes, processes will die without responding (the real reason is a bit complicated to explain, but basically sometimes the process might not unlock it) so that causes a problem as a space in the semaphore is now permanently locked.</p>
  <p>So I would like to have a timeout on this. Here are some issues:</p>
  <p>The processes will run from anywhere between 2 seconds up to 60 minutes.</p>
  <p>We have some race conditions, because if it times out and then the process tries to unlock it, then we have unlocked the semaphore twice instead of once. And vice versa, we unlock it first and then it times out.</p>
  <p>How do I take the suggested pattern posted above and turn this into a thread-safe semaphore with timeouts?</p>