update lock

This commit is contained in:
hetao 2017-06-19 17:44:40 +08:00
parent 9fa7e7f648
commit cdd8bdb93a
2 changed files with 23 additions and 31 deletions

50
conn.go
View File

@ -360,6 +360,23 @@ func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr() return c.conn.RemoteAddr()
} }
//
func (c *Conn) WriteLock() {
c.writeMu.Lock()
c.isWriting = true
}
//
func (c *Conn) WriteUnlock() {
c.isWriting = false
c.writeMu.Unlock()
}
// Return the conn writing status
func (c *Conn) IsWriting() bool {
return c.isWriting
}
// Write methods // Write methods
func (c *Conn) writeFatal(err error) error { func (c *Conn) writeFatal(err error) error {
@ -579,22 +596,8 @@ func (w *messageWriter) flushFrame(final bool, extra []byte) error {
} }
} }
// Write the buffers to the connection with best-effort detection of
// concurrent writes. See the concurrency section in the package
// documentation for more info.
if c.isWriting {
panic("concurrent write to websocket connection")
}
c.isWriting = true
err := c.write(w.frameType, c.writeDeadline, c.writeBuf[framePos:w.pos], extra) err := c.write(w.frameType, c.writeDeadline, c.writeBuf[framePos:w.pos], extra)
if !c.isWriting {
panic("concurrent write to websocket connection")
}
c.isWriting = false
if err != nil { if err != nil {
return w.fatal(err) return w.fatal(err)
} }
@ -715,23 +718,17 @@ func (c *Conn) WritePreparedMessage(pm *PreparedMessage) error {
if err != nil { if err != nil {
return err return err
} }
if c.isWriting {
panic("concurrent write to websocket connection")
}
c.isWriting = true
err = c.write(frameType, c.writeDeadline, frameData, nil) err = c.write(frameType, c.writeDeadline, frameData, nil)
if !c.isWriting {
panic("concurrent write to websocket connection")
}
c.isWriting = false
return err return err
} }
// WriteMessage is a helper method for getting a writer using NextWriter, // WriteMessage is a helper method for getting a writer using NextWriter,
// writing the message and closing the writer. // writing the message and closing the writer.
func (c *Conn) WriteMessage(messageType int, data []byte) error { func (c *Conn) WriteMessage(messageType int, data []byte) error {
c.writeMu.Lock() c.WriteLock()
defer c.writeMu.Unlock() defer c.WriteUnlock()
if c.isServer && (c.newCompressionWriter == nil || !c.enableWriteCompression) { if c.isServer && (c.newCompressionWriter == nil || !c.enableWriteCompression) {
// Fast path with no allocations and single frame. // Fast path with no allocations and single frame.
@ -765,11 +762,6 @@ func (c *Conn) SetWriteDeadline(t time.Time) error {
return nil return nil
} }
// Return the conn writing status
func (c *Conn) IsWriting() bool {
return c.isWriting
}
// Read methods // Read methods
func (c *Conn) advanceFrame() (int, error) { func (c *Conn) advanceFrame() (int, error) {

View File

@ -19,8 +19,8 @@ func WriteJSON(c *Conn, v interface{}) error {
// See the documentation for encoding/json Marshal for details about the // See the documentation for encoding/json Marshal for details about the
// conversion of Go values to JSON. // conversion of Go values to JSON.
func (c *Conn) WriteJSON(v interface{}) error { func (c *Conn) WriteJSON(v interface{}) error {
c.writeMu.Lock() c.WriteLock()
defer c.writeMu.Unlock() defer c.WriteUnlock()
w, err := c.NextWriter(TextMessage) w, err := c.NextWriter(TextMessage)
if err != nil { if err != nil {
return err return err