Commit e7d1b729 authored by AlexStocks's avatar AlexStocks

add updateWriteDeadline

parent d708dcdd
...@@ -15,8 +15,9 @@ ...@@ -15,8 +15,9 @@
> improvement > improvement
* 1 rDeadline -> rTimeout * 1 rDeadline -> rTimeout
* 2 wDeadline -> wTimeout * 2 wDeadline -> wTimeout
* 3 disable readtimeout in func (w *gettyWSConn) read() * 3 disable readtimeout in gettyWSConn:read
* 4 add gettyWSConn:updateWriteDeadline
- 2018/03/08 - 2018/03/08
> feature > feature
* 1 add udp client and udp server * 1 add udp client and udp server
......
...@@ -375,24 +375,7 @@ func (w *gettyWSConn) SetCompressType(c CompressType) { ...@@ -375,24 +375,7 @@ func (w *gettyWSConn) SetCompressType(c CompressType) {
} }
func (w *gettyWSConn) handlePing(message string) error { func (w *gettyWSConn) handlePing(message string) error {
var ( err := w.writePong([]byte(message))
err error
currentTime time.Time
)
if w.wTimeout > 0 {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime = wheel.Now()
if currentTime.Sub(w.wLastDeadline) > (w.wTimeout >> 2) {
if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout)); err != nil {
return err
}
w.wLastDeadline = currentTime
}
}
err = w.conn.WriteMessage(websocket.PongMessage, []byte(message))
if err == websocket.ErrCloseSent { if err == websocket.ErrCloseSent {
err = nil err = nil
} else if e, ok := err.(net.Error); ok && e.Temporary() { } else if e, ok := err.(net.Error); ok && e.Temporary() {
...@@ -426,18 +409,12 @@ func (w *gettyWSConn) read() ([]byte, error) { ...@@ -426,18 +409,12 @@ func (w *gettyWSConn) read() ([]byte, error) {
return b, e return b, e
} }
// websocket connection write func (w *gettyWSConn) updateWriteDeadline() error {
func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
var ( var (
err error err error
currentTime time.Time currentTime time.Time
ok bool
p []byte
) )
if p, ok = pkg.([]byte); !ok {
return 0, fmt.Errorf("illegal @pkg{%#v} type", pkg)
}
if w.wTimeout > 0 { if w.wTimeout > 0 {
// Optimization: update write deadline only if more than 25% // Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded. // of the last write deadline exceeded.
...@@ -445,24 +422,45 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) { ...@@ -445,24 +422,45 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
currentTime = wheel.Now() currentTime = wheel.Now()
if currentTime.Sub(w.wLastDeadline) > (w.wTimeout >> 2) { if currentTime.Sub(w.wLastDeadline) > (w.wTimeout >> 2) {
if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout)); err != nil { if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout)); err != nil {
return 0, err return err
} }
w.wLastDeadline = currentTime w.wLastDeadline = currentTime
} }
} }
return nil
}
// websocket connection write
func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
var (
ok bool
p []byte
)
if p, ok = pkg.([]byte); !ok {
return 0, fmt.Errorf("illegal @pkg{%#v} type", pkg)
}
// atomic.AddUint32(&w.writeCount, 1) // atomic.AddUint32(&w.writeCount, 1)
atomic.AddUint32(&w.writeCount, (uint32)(len(p))) atomic.AddUint32(&w.writeCount, (uint32)(len(p)))
// w.conn.SetWriteTimeout(time.Now().Add(w.wTimeout)) w.updateWriteDeadline()
return len(p), w.conn.WriteMessage(websocket.BinaryMessage, p) return len(p), w.conn.WriteMessage(websocket.BinaryMessage, p)
} }
func (w *gettyWSConn) writePing() error { func (w *gettyWSConn) writePing() error {
w.updateWriteDeadline()
return w.conn.WriteMessage(websocket.PingMessage, []byte{}) return w.conn.WriteMessage(websocket.PingMessage, []byte{})
} }
func (w *gettyWSConn) writePong(message []byte) error {
w.updateWriteDeadline()
return w.conn.WriteMessage(websocket.PongMessage, message)
}
// close websocket connection // close websocket connection
func (w *gettyWSConn) close(waitSec int) { func (w *gettyWSConn) close(waitSec int) {
w.updateWriteDeadline()
w.conn.WriteMessage(websocket.CloseMessage, []byte("bye-bye!!!")) w.conn.WriteMessage(websocket.CloseMessage, []byte("bye-bye!!!"))
conn := w.conn.UnderlyingConn() conn := w.conn.UnderlyingConn()
if tcpConn, ok := conn.(*net.TCPConn); ok { if tcpConn, ok := conn.(*net.TCPConn); ok {
...@@ -503,7 +501,7 @@ func setUDPSocketOptions(conn *net.UDPConn) error { ...@@ -503,7 +501,7 @@ func setUDPSocketOptions(conn *net.UDPConn) error {
// create gettyUDPConn // create gettyUDPConn
func newGettyUDPConn(conn *net.UDPConn, peerUDPAddr *net.UDPAddr) *gettyUDPConn { func newGettyUDPConn(conn *net.UDPConn, peerUDPAddr *net.UDPAddr) *gettyUDPConn {
if conn == nil { if conn == nil {
panic("newGettyWSConn(conn):@conn is nil") panic("newGettyUDPConn(conn):@conn is nil")
} }
var localAddr, peerAddr string var localAddr, peerAddr string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment