-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_timeout.go
More file actions
33 lines (27 loc) · 897 Bytes
/
Copy pathwrite_timeout.go
File metadata and controls
33 lines (27 loc) · 897 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"
// WriteTimeoutHandler 在写空闲超时后抛出异常并关闭 Channel。
type WriteTimeoutHandler struct {
idle *IdleStateHandler
}
func NewWriteTimeoutHandler(timeoutMillis int64) *WriteTimeoutHandler {
idle := NewIdleStateHandler(0, timeoutMillis, 0)
idle.onIdle = func(ctx *channel.HandlerContext, event IdleStateEvent) bool {
if event.State != WriterIdle {
return false
}
ctx.FireExceptionCaught(ErrWriteTimeout)
_ = ctx.Close()
return true
}
return &WriteTimeoutHandler{idle: idle}
}
func (h *WriteTimeoutHandler) ChannelActive(ctx *channel.HandlerContext) {
h.idle.ChannelActive(ctx)
}
func (h *WriteTimeoutHandler) Write(ctx *channel.HandlerContext, msg any) error {
return h.idle.Write(ctx, msg)
}
func (h *WriteTimeoutHandler) ChannelInactive(ctx *channel.HandlerContext) {
h.idle.ChannelInactive(ctx)
}