Skip to content

Commit b7167ec

Browse files
committed
new: add debouncer for sending proxy info
1 parent a4ff217 commit b7167ec

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

v2/hcore/debouncer.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package hcore
2+
3+
import (
4+
"time"
5+
)
6+
7+
type Debouncer struct {
8+
delay time.Duration
9+
timer *time.Timer
10+
}
11+
12+
func NewDebouncer(d time.Duration) *Debouncer {
13+
t := time.NewTimer(d)
14+
t.Stop()
15+
return &Debouncer{
16+
delay: d,
17+
timer: t,
18+
}
19+
}
20+
21+
func (d *Debouncer) Hit() {
22+
if !d.timer.Stop() {
23+
select {
24+
case <-d.timer.C:
25+
default:
26+
}
27+
}
28+
d.timer.Reset(d.delay)
29+
}
30+
31+
func (d *Debouncer) C() <-chan time.Time {
32+
return d.timer.C
33+
}
34+
35+
func (d *Debouncer) Stop() {
36+
d.timer.Stop()
37+
}

v2/hcore/proxy_info.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hcore
22

33
import (
44
"strings"
5+
"time"
56

67
hcommon "github.com/hiddify/hiddify-core/v2/hcommon"
78
"github.com/sagernet/sing-box/adapter"
@@ -130,29 +131,31 @@ func (h *HiddifyInstance) AllProxiesInfoStream(stream grpc.ServerStreamingServer
130131
return err
131132
}
132133
stream.Send(h.GetAllProxiesInfo(onlyMain))
134+
debouncer := NewDebouncer(500 * time.Millisecond)
135+
defer debouncer.Stop()
136+
133137
for {
134-
debounce:
135-
for {
136-
select {
137-
case <-urltestch:
138-
default:
139-
break debounce
138+
select {
139+
case u := <-urltestch:
140+
if u == 2 {
141+
stream.Send(h.GetAllProxiesInfo(onlyMain))
142+
} else {
143+
debouncer.Hit()
144+
}
145+
146+
case <-debouncer.C():
147+
if err := stream.Send(h.GetAllProxiesInfo(onlyMain)); err != nil {
148+
return err
140149
}
141-
}
142150

143-
select {
144151
case <-stream.Context().Done():
145152
return nil
146153
case <-ctx.Done():
147154
return nil
148155
case <-done:
149156
return nil
150-
// case <-time.After(5000 * time.Millisecond):
151-
case <-urltestch:
152-
stream.Send(h.GetAllProxiesInfo(onlyMain))
153157
}
154158
}
155-
156159
}
157160
return nil
158161
}

0 commit comments

Comments
 (0)