-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_timeout.go
More file actions
33 lines (27 loc) · 887 Bytes
/
Copy pathread_timeout.go
File metadata and controls
33 lines (27 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package timeout
import "gnalloy.org/gnalloy/channel"
// ReadTimeoutHandler 在读空闲超时后抛出异常并关闭 Channel。
type ReadTimeoutHandler struct {
idle *IdleStateHandler
}
func NewReadTimeoutHandler(timeoutMillis int64) *ReadTimeoutHandler {
idle := NewIdleStateHandler(timeoutMillis, 0, 0)
idle.onIdle = func(ctx *channel.HandlerContext, event IdleStateEvent) bool {
if event.State != ReaderIdle {
return false
}
ctx.FireExceptionCaught(ErrReadTimeout)
_ = ctx.Close()
return true
}
return &ReadTimeoutHandler{idle: idle}
}
func (h *ReadTimeoutHandler) ChannelActive(ctx *channel.HandlerContext) {
h.idle.ChannelActive(ctx)
}
func (h *ReadTimeoutHandler) ChannelRead(ctx *channel.HandlerContext, msg any) {
h.idle.ChannelRead(ctx, msg)
}
func (h *ReadTimeoutHandler) ChannelInactive(ctx *channel.HandlerContext) {
h.idle.ChannelInactive(ctx)
}