package connection: add func ModReadTimeoutMS & ModWriteTimeoutMS

pull/2/head
q191201771 5 years ago
parent d6ed5bbb08
commit 246bc02cc8

@ -22,6 +22,8 @@ type Connection interface {
Printf(fmt string, v ...interface{}) (n int, err error)
ModWriteBufSize(n int)
ModReadTimeoutMS(n int)
ModWriteTimeoutMS(n int)
}
type Config struct {
@ -58,7 +60,9 @@ type connection struct {
config Config
}
// 这个函数不加锁,由调用方保证不和写操作并发执行
// Mod函数不加锁
// 由调用方保证不和写操作并发执行
func (c *connection) ModWriteBufSize(n int) {
if c.config.WriteBufSize > 0 {
// 如果之前已经设置过写缓冲,直接 panic
@ -69,6 +73,20 @@ func (c *connection) ModWriteBufSize(n int) {
c.w = bufio.NewWriterSize(c.Conn, n)
}
func (c *connection) ModReadTimeoutMS(n int) {
if c.config.ReadTimeoutMS > 0 {
panic(connectionErr)
}
c.config.ReadTimeoutMS = n
}
func (c *connection) ModWriteTimeoutMS(n int) {
if c.config.WriteTimeoutMS > 0 {
panic(connectionErr)
}
c.config.WriteTimeoutMS = n
}
func (c *connection) ReadAtLeast(buf []byte, min int) (n int, err error) {
if c.config.ReadTimeoutMS > 0 {
_ = c.Conn.SetReadDeadline(time.Now().Add(time.Duration(c.config.ReadTimeoutMS) * time.Millisecond))

Loading…
Cancel
Save