-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add capability for memory and persistent queue to block when add items
Signed-off-by: Bogdan Drutu <[email protected]>
- Loading branch information
1 parent
7f4664e
commit f66fc54
Showing
7 changed files
with
243 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package exporterqueue | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// cond is equivalent with sync.Cond, but context.Context aware. Which means Wait() will return if context is done. | ||
// Also, it requires the caller to hold the c.L during all calls. | ||
type cond struct { | ||
L sync.Locker | ||
ch chan struct{} | ||
waiting int64 | ||
} | ||
|
||
func newCond(l sync.Locker) *cond { | ||
return &cond{L: l, ch: make(chan struct{}, 1)} | ||
} | ||
|
||
// Signal wakes one goroutine waiting on c, if there is any. | ||
// It requires for the caller to hold c.L during the call. | ||
func (c *cond) Signal() { | ||
if c.waiting == 0 { | ||
return | ||
} | ||
c.waiting-- | ||
c.ch <- struct{}{} | ||
} | ||
|
||
// Broadcast wakes all goroutines waiting on c. | ||
// It requires for the caller to hold c.L during the call. | ||
func (c *cond) Broadcast() { | ||
for ; c.waiting > 0; c.waiting-- { | ||
c.ch <- struct{}{} | ||
} | ||
} | ||
|
||
// Wait atomically unlocks c.L and suspends execution of the calling goroutine. After later resuming execution, Wait locks c.L before returning. | ||
func (c *cond) Wait(ctx context.Context) error { | ||
c.waiting++ | ||
c.L.Unlock() | ||
select { | ||
case <-ctx.Done(): | ||
c.L.Lock() | ||
if c.waiting == 0 { | ||
// If waiting is 0, it means that there was a signal sent and nobody else waits for it. | ||
// Consume it, so that we don't unblock other consumer unnecessary, | ||
// or we don't block the producer because the channel buffer is full. | ||
<-c.ch | ||
} else { | ||
// Decrease the number of waiting routines. | ||
c.waiting-- | ||
} | ||
return ctx.Err() | ||
case <-c.ch: | ||
c.L.Lock() | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.