我如何等待通道 activity 中的平静来触发某些东西?
How do I wait for lulls in channel activity to trigger something?
我有一个通道可以接收突发写入。我想等到通道上的突发发送完成后再触发操作。
我看过这个 gist,但是,如果缓冲区中有数据,它将每隔 interval
发送一次输出:
func debounceChannel(interval time.Duration, output chan int) chan int {
input := make(chan int)
go func() {
var buffer int
var ok bool
// We do not start waiting for interval until called at least once
buffer, ok = <-input
// If channel closed exit, we could also close output
if !ok {
return
}
// We start waiting for an interval
for {
select {
case buffer, ok = <-input:
// If channel closed exit, we could also close output
if !ok {
return
}
case <-time.After(interval):
// Interval has passed and we have data, so send it
output <- buffer
// Wait for data again before starting waiting for an interval
buffer, ok = <-input
if !ok {
return
}
// If channel is not closed we have more data and start waiting for interval
}
}
}()
return input
}
在我的例子中,我想等到输入通道上不再有任何数据在触发或发送输出之前发送到此突发。
如何实现?
听起来你需要在 goroutine 之间进行同步,也许沿着这条线。
func main() {
// Create a channel for our input
input := make(chan int, 1)
// Create another for synchronization between main and forked goroutines
done := make(chan bool)
go func() {
// block-wait for received value
<-input
// do some more things here
// when done, send signal to the main goroutine
done <- true
}()
// Do something while wait for the forked goroutine
// this block until `<-done`
<-done
close(mychan)
}
这个post对使用频道和同步组的同步解释的很清楚。
这就是我最终实现的去抖动器:
func Debounce(lull time.Duration, in chan struct{}, out chan struct{}) {
go func() {
var last int64 = 0
for {
select {
case <-in:
last = time.Now().Unix()
case <-time.Tick(lull):
if last != 0 && time.Now().Unix() >= last+int64(lull.Seconds()) {
last = 0
out <- struct{}{}
}
}
}
}()
}
它需要一个间歇时间,如果我们没有收到输入,那么我们假设数据突发中断的持续时间。有2个通道,1个输入和1个输出。数据突发到达输入端,对于每个突发,我们在突发结束时写入输出通道。
实现极其简单。每次从输入通道接收时,我只存储当前的 unix 时间戳。然后,我有一个自动收报机在间歇时间的持续时间内滴答作响。所有这一切只是检查我们是否超过了最后一次爆发的等待时间。如果是这样,它将 last
重置为 0 并在输出通道上发出一个事件。
下面是一些使用去抖动函数的代码,其间歇时间为 2 秒,可在输入通道上发送随机脉冲串:
func main() {
out := make(chan struct{})
in := make(chan struct{})
Debounce(2*time.Second, in, out)
// Generating bursts of input data
go func(in chan struct{}) {
for {
select {
case <-time.Tick(1 * time.Second):
in <- struct{}{}
fmt.Println("Sending!")
shouldSleep := rand.Intn(2)
if shouldSleep == 1 {
time.Sleep(5 * time.Second)
}
}
}
}(in)
// Listening for output events
go func(out chan struct{}) {
for _ = range out {
fmt.Println("Got an event!")
}
}(out)
// Do not let main terminate.
done := make(chan struct{})
<-done
}
我有一个通道可以接收突发写入。我想等到通道上的突发发送完成后再触发操作。
我看过这个 gist,但是,如果缓冲区中有数据,它将每隔 interval
发送一次输出:
func debounceChannel(interval time.Duration, output chan int) chan int {
input := make(chan int)
go func() {
var buffer int
var ok bool
// We do not start waiting for interval until called at least once
buffer, ok = <-input
// If channel closed exit, we could also close output
if !ok {
return
}
// We start waiting for an interval
for {
select {
case buffer, ok = <-input:
// If channel closed exit, we could also close output
if !ok {
return
}
case <-time.After(interval):
// Interval has passed and we have data, so send it
output <- buffer
// Wait for data again before starting waiting for an interval
buffer, ok = <-input
if !ok {
return
}
// If channel is not closed we have more data and start waiting for interval
}
}
}()
return input
}
在我的例子中,我想等到输入通道上不再有任何数据在触发或发送输出之前发送到此突发。
如何实现?
听起来你需要在 goroutine 之间进行同步,也许沿着这条线。
func main() {
// Create a channel for our input
input := make(chan int, 1)
// Create another for synchronization between main and forked goroutines
done := make(chan bool)
go func() {
// block-wait for received value
<-input
// do some more things here
// when done, send signal to the main goroutine
done <- true
}()
// Do something while wait for the forked goroutine
// this block until `<-done`
<-done
close(mychan)
}
这个post对使用频道和同步组的同步解释的很清楚。
这就是我最终实现的去抖动器:
func Debounce(lull time.Duration, in chan struct{}, out chan struct{}) {
go func() {
var last int64 = 0
for {
select {
case <-in:
last = time.Now().Unix()
case <-time.Tick(lull):
if last != 0 && time.Now().Unix() >= last+int64(lull.Seconds()) {
last = 0
out <- struct{}{}
}
}
}
}()
}
它需要一个间歇时间,如果我们没有收到输入,那么我们假设数据突发中断的持续时间。有2个通道,1个输入和1个输出。数据突发到达输入端,对于每个突发,我们在突发结束时写入输出通道。
实现极其简单。每次从输入通道接收时,我只存储当前的 unix 时间戳。然后,我有一个自动收报机在间歇时间的持续时间内滴答作响。所有这一切只是检查我们是否超过了最后一次爆发的等待时间。如果是这样,它将 last
重置为 0 并在输出通道上发出一个事件。
下面是一些使用去抖动函数的代码,其间歇时间为 2 秒,可在输入通道上发送随机脉冲串:
func main() {
out := make(chan struct{})
in := make(chan struct{})
Debounce(2*time.Second, in, out)
// Generating bursts of input data
go func(in chan struct{}) {
for {
select {
case <-time.Tick(1 * time.Second):
in <- struct{}{}
fmt.Println("Sending!")
shouldSleep := rand.Intn(2)
if shouldSleep == 1 {
time.Sleep(5 * time.Second)
}
}
}
}(in)
// Listening for output events
go func(out chan struct{}) {
for _ = range out {
fmt.Println("Got an event!")
}
}(out)
// Do not let main terminate.
done := make(chan struct{})
<-done
}