Kaynağa Gözat

Create semaphore util

Niko Kovacevic 6 yıl önce
ebeveyn
işleme
920d51350d
1 değiştirilmiş dosya ile 25 ekleme ve 0 silme
  1. 25 0
      util/semaphore.go

+ 25 - 0
util/semaphore.go

@@ -0,0 +1,25 @@
+package util
+
+// Semaphore implements a non-weighted semaphore for restricting
+// concurrent access to a limited number of processes.
+type Semaphore struct {
+	s chan bool
+}
+
+// Acquire blocks until access can be granted to the caller
+func (s *Semaphore) Acquire() {
+	s.s <- true
+}
+
+// Return releases access from the caller, opening it for acquisition
+func (s *Semaphore) Return() {
+	<-s.s
+}
+
+// NewSemaphore creates a new Semaphore that allows max number of
+// concurrent access
+func NewSemaphore(max int) *Semaphore {
+	return &Semaphore{
+		s: make(chan bool, max),
+	}
+}