forked from matrix-org/dendrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a FIFO queue instead of a channel to reduce backpressure
- Loading branch information
1 parent
a6f7e83
commit b63f699
Showing
2 changed files
with
69 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package input | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type fifoQueue struct { | ||
frames []*inputTask | ||
count int | ||
mutex sync.Mutex | ||
notifs chan struct{} | ||
} | ||
|
||
func newFIFOQueue() *fifoQueue { | ||
q := &fifoQueue{ | ||
notifs: make(chan struct{}), | ||
} | ||
return q | ||
} | ||
|
||
func (q *fifoQueue) push(frame *inputTask) bool { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
q.frames = append(q.frames, frame) | ||
q.count++ | ||
select { | ||
case q.notifs <- struct{}{}: | ||
default: | ||
} | ||
return true | ||
} | ||
|
||
func (q *fifoQueue) pop() (*inputTask, bool) { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
if q.count == 0 { | ||
return nil, false | ||
} | ||
frame := q.frames[0] | ||
q.frames[0] = nil | ||
q.frames = q.frames[1:] | ||
q.count-- | ||
if q.count == 0 { | ||
// Force a GC of the underlying array, since it might have | ||
// grown significantly if the queue was hammered for some reason | ||
q.frames = nil | ||
} | ||
return frame, true | ||
} | ||
|
||
func (q *fifoQueue) wait() <-chan struct{} { | ||
q.mutex.Lock() | ||
defer q.mutex.Unlock() | ||
if q.count > 0 { | ||
ch := make(chan struct{}) | ||
close(ch) | ||
return ch | ||
} | ||
return q.notifs | ||
} |