-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So that we can detect when the publisher is stalled. Relates to #131.
- Loading branch information
1 parent
adc614f
commit e4bafcc
Showing
3 changed files
with
85 additions
and
10 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,56 @@ | ||
package publisher | ||
|
||
import ( | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/matrix-org/waterfall/pkg/worker" | ||
) | ||
|
||
type Status int | ||
|
||
const ( | ||
StatusStalled Status = iota + 1 | ||
StatusRecovered | ||
) | ||
|
||
// `statusObserver` is a helper that observes the status of the publisher. | ||
// Essentially it's a simple worker that expects to be informed about new packet | ||
// arrivals. If no packets are received for N seconds, the worker will report the | ||
// stalled status over the `statusCh` channel. Likewise, it'll update the status to | ||
// recovered once a new packet is received. | ||
type statusObserver struct { | ||
worker *worker.Worker[struct{}] | ||
statusCh chan Status | ||
stalled atomic.Bool | ||
} | ||
|
||
func newStatusObserver(timeout time.Duration) *statusObserver { | ||
statusCh := make(chan Status, 1) | ||
stalled := atomic.Bool{} | ||
|
||
worker := worker.StartWorker(worker.Config[struct{}]{ | ||
ChannelSize: 1, | ||
Timeout: timeout, | ||
OnTimeout: func() { | ||
stalled.Store(true) | ||
statusCh <- StatusStalled | ||
}, | ||
OnTask: func(struct{}) { | ||
if stalled.CompareAndSwap(true, false) { | ||
statusCh <- StatusRecovered | ||
} | ||
}, | ||
}) | ||
|
||
return &statusObserver{worker, statusCh, stalled} | ||
} | ||
|
||
func (o *statusObserver) packetArrived() { | ||
o.worker.Send(struct{}{}) | ||
} | ||
|
||
func (o *statusObserver) stop() { | ||
o.worker.Stop() | ||
close(o.statusCh) | ||
} |
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