semaphore.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2019 The Vitess Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sync
  14. // What's in a name? Channels have all you need to emulate a counting
  15. // semaphore with a boatload of extra functionality. However, in some
  16. // cases, you just want a familiar API.
  17. import (
  18. "context"
  19. "time"
  20. )
  21. // Semaphore is a counting semaphore with the option to
  22. // specify a timeout.
  23. type Semaphore struct {
  24. slots chan struct{}
  25. timeout time.Duration
  26. }
  27. // NewSemaphore creates a Semaphore. The count parameter must be a positive
  28. // number. A timeout of zero means that there is no timeout.
  29. func NewSemaphore(count int, timeout time.Duration) *Semaphore {
  30. sem := &Semaphore{
  31. slots: make(chan struct{}, count),
  32. timeout: timeout,
  33. }
  34. for i := 0; i < count; i++ {
  35. sem.slots <- struct{}{}
  36. }
  37. return sem
  38. }
  39. // Acquire returns true on successful acquisition, and
  40. // false on a timeout.
  41. func (sem *Semaphore) Acquire() bool {
  42. if sem.timeout == 0 {
  43. <-sem.slots
  44. return true
  45. }
  46. tm := time.NewTimer(sem.timeout)
  47. defer tm.Stop()
  48. select {
  49. case <-sem.slots:
  50. return true
  51. case <-tm.C:
  52. return false
  53. }
  54. }
  55. // AcquireContext returns true on successful acquisition, and
  56. // false on context expiry. Timeout is ignored.
  57. func (sem *Semaphore) AcquireContext(ctx context.Context) bool {
  58. select {
  59. case <-sem.slots:
  60. return true
  61. case <-ctx.Done():
  62. return false
  63. }
  64. }
  65. // TryAcquire acquires a semaphore if it's immediately available.
  66. // It returns false otherwise.
  67. func (sem *Semaphore) TryAcquire() bool {
  68. select {
  69. case <-sem.slots:
  70. return true
  71. default:
  72. return false
  73. }
  74. }
  75. // Release releases the acquired semaphore. You must
  76. // not release more than the number of semaphores you've
  77. // acquired.
  78. func (sem *Semaphore) Release() {
  79. sem.slots <- struct{}{}
  80. }
  81. // Size returns the current number of available slots.
  82. func (sem *Semaphore) Size() int {
  83. return len(sem.slots)
  84. }