Skip to content

Commit 4ba70f1

Browse files
committed
Add idle disconnect logic to czar
1 parent ce509c4 commit 4ba70f1

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

protocol/czar/engine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ import (
4646
var Conf = struct {
4747
// The newly created stream has a higher write priority.
4848
FastWriteDuration time.Duration
49+
// If no new data is received for a period of time, the mux is disconnected.
50+
IdleAfterDuration time.Duration
4951
// Packet size. Since the size of the packet header is 4, this value must be greater than 4. If the value is too
5052
// small, the transmission efficiency will be reduced, and if it is too large, the concurrency capability of mux
5153
// will be reduced.
5254
PacketSize int
5355
}{
5456
FastWriteDuration: time.Second * 8,
57+
IdleAfterDuration: time.Minute * 8,
5558
PacketSize: 2048,
5659
}
5760

protocol/czar/mux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ func (m *Mux) Recv() {
201201
buf = make([]byte, 4)
202202
cmd uint8
203203
err error
204+
fin = time.AfterFunc(Conf.IdleAfterDuration, func() {
205+
m.Close()
206+
})
204207
idx uint8
205208
msg []byte
206209
old *Stream
207210
stm *Stream
208211
)
209212
for {
213+
fin.Reset(Conf.IdleAfterDuration)
210214
_, err = io.ReadFull(m.con, buf[:4])
211215
if err != nil {
212216
m.rer.Put(err)
@@ -253,6 +257,7 @@ func (m *Mux) Recv() {
253257
}
254258
}
255259
close(m.ach)
260+
fin.Stop()
256261
}
257262

258263
// NewMux returns a new Mux.

0 commit comments

Comments
 (0)