Skip to content

Commit d762e71

Browse files
committed
core/flow: clean up observers once every 3h
1 parent 9b68aeb commit d762e71

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

intra/core/volatileflow.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"slices"
1111
"sync"
12+
"time"
1213
)
1314

1415
type FlowFunc[T any] func(v T)
@@ -32,6 +33,15 @@ func (f FlowOn[T]) flow(v T) (flowed bool) {
3233
return false
3334
}
3435

36+
func (f FlowOn[T]) obsolete() bool {
37+
select {
38+
case <-f.ctx.Done():
39+
return true
40+
default:
41+
}
42+
return false
43+
}
44+
3545
type Flow[T any] struct {
3646
ctx context.Context
3747

@@ -77,6 +87,16 @@ func (f *Flow[T]) stream() {
7787
}
7888
f.removeFinallys(notflowing)
7989
}()
90+
case <-time.Tick(3 * time.Hour):
91+
go func() {
92+
notflowing := make(map[FlowOn[T]]struct{})
93+
for _, o := range f.observers() {
94+
if o.obsolete() {
95+
notflowing[o] = struct{}{}
96+
}
97+
}
98+
f.removeFinallys(notflowing)
99+
}()
80100
}
81101
}
82102
}

0 commit comments

Comments
 (0)