mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2024-05-17 04:56:33 +02:00
Simplify close code of client to make clear when it gets closed internally.
This commit is contained in:
parent
b17eb584b4
commit
758899b745
78
client.go
78
client.go
|
@ -105,10 +105,10 @@ type Client struct {
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
closeChan chan bool
|
closeChan chan struct{}
|
||||||
messagesDone chan bool
|
closeOnce sync.Once
|
||||||
messageChan chan *bytes.Buffer
|
messagesDone chan struct{}
|
||||||
messageProcessing uint32
|
messageChan chan *bytes.Buffer
|
||||||
|
|
||||||
OnLookupCountry func(*Client) string
|
OnLookupCountry func(*Client) string
|
||||||
OnClosed func(*Client)
|
OnClosed func(*Client)
|
||||||
|
@ -127,31 +127,23 @@ func NewClient(conn *websocket.Conn, remoteAddress string, agent string) (*Clien
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &Client{
|
client := &Client{
|
||||||
conn: conn,
|
|
||||||
addr: remoteAddress,
|
|
||||||
agent: agent,
|
agent: agent,
|
||||||
logRTT: true,
|
logRTT: true,
|
||||||
|
|
||||||
closeChan: make(chan bool, 1),
|
|
||||||
messageChan: make(chan *bytes.Buffer, 16),
|
|
||||||
messagesDone: make(chan bool, 1),
|
|
||||||
|
|
||||||
OnLookupCountry: func(client *Client) string { return unknownCountry },
|
|
||||||
OnClosed: func(client *Client) {},
|
|
||||||
OnMessageReceived: func(client *Client, data []byte) {},
|
|
||||||
OnRTTReceived: func(client *Client, rtt time.Duration) {},
|
|
||||||
}
|
}
|
||||||
|
client.SetConn(conn, remoteAddress)
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SetConn(conn *websocket.Conn, remoteAddress string) {
|
func (c *Client) SetConn(conn *websocket.Conn, remoteAddress string) {
|
||||||
c.conn = conn
|
c.conn = conn
|
||||||
c.addr = remoteAddress
|
c.addr = remoteAddress
|
||||||
c.closeChan = make(chan bool, 1)
|
c.closeChan = make(chan struct{})
|
||||||
c.messageChan = make(chan *bytes.Buffer, 16)
|
c.messageChan = make(chan *bytes.Buffer, 16)
|
||||||
|
c.messagesDone = make(chan struct{})
|
||||||
c.OnLookupCountry = func(client *Client) string { return unknownCountry }
|
c.OnLookupCountry = func(client *Client) string { return unknownCountry }
|
||||||
c.OnClosed = func(client *Client) {}
|
c.OnClosed = func(client *Client) {}
|
||||||
c.OnMessageReceived = func(client *Client, data []byte) {}
|
c.OnMessageReceived = func(client *Client, data []byte) {}
|
||||||
|
c.OnRTTReceived = func(c *Client, d time.Duration) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) IsConnected() bool {
|
func (c *Client) IsConnected() bool {
|
||||||
|
@ -188,38 +180,36 @@ func (c *Client) Country() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Close() {
|
func (c *Client) Close() {
|
||||||
if !atomic.CompareAndSwapUint32(&c.closed, 0, 1) {
|
if atomic.LoadUint32(&c.closed) >= 2 {
|
||||||
|
// Prevent reentrant call in case this was the second closing
|
||||||
|
// step. Would otherwise deadlock in the "Once.Do" call path
|
||||||
|
// through "Hub.processUnregister" (which calls "Close" again).
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.mu.Lock()
|
c.closeOnce.Do(func() {
|
||||||
if c.conn != nil {
|
c.doClose()
|
||||||
c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) // nolint
|
})
|
||||||
}
|
|
||||||
c.mu.Unlock()
|
|
||||||
|
|
||||||
if atomic.LoadUint32(&c.messageProcessing) == 1 {
|
|
||||||
// Defer closing
|
|
||||||
atomic.StoreUint32(&c.closed, 2)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.doClose()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) doClose() {
|
func (c *Client) doClose() {
|
||||||
c.closeChan <- true
|
closed := atomic.AddUint32(&c.closed, 1)
|
||||||
<-c.messagesDone
|
if closed == 1 {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
if c.conn != nil {
|
||||||
|
c.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) // nolint
|
||||||
|
c.conn.Close()
|
||||||
|
c.conn = nil
|
||||||
|
}
|
||||||
|
} else if closed == 2 {
|
||||||
|
// Both the read pump and message processing must be finished before closing.
|
||||||
|
close(c.closeChan)
|
||||||
|
<-c.messagesDone
|
||||||
|
|
||||||
c.OnClosed(c)
|
c.OnClosed(c)
|
||||||
c.SetSession(nil)
|
c.SetSession(nil)
|
||||||
|
|
||||||
c.mu.Lock()
|
|
||||||
if c.conn != nil {
|
|
||||||
c.conn.Close()
|
|
||||||
c.conn = nil
|
|
||||||
}
|
}
|
||||||
c.mu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) SendError(e *Error) bool {
|
func (c *Client) SendError(e *Error) bool {
|
||||||
|
@ -341,7 +331,6 @@ func (c *Client) ReadPump() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) processMessages() {
|
func (c *Client) processMessages() {
|
||||||
atomic.StoreUint32(&c.messageProcessing, 1)
|
|
||||||
for {
|
for {
|
||||||
buffer := <-c.messageChan
|
buffer := <-c.messageChan
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
|
@ -351,12 +340,9 @@ func (c *Client) processMessages() {
|
||||||
c.OnMessageReceived(c, buffer.Bytes())
|
c.OnMessageReceived(c, buffer.Bytes())
|
||||||
bufferPool.Put(buffer)
|
bufferPool.Put(buffer)
|
||||||
}
|
}
|
||||||
atomic.StoreUint32(&c.messageProcessing, 0)
|
|
||||||
|
|
||||||
c.messagesDone <- true
|
close(c.messagesDone)
|
||||||
if atomic.LoadUint32(&c.closed) == 2 {
|
c.doClose()
|
||||||
c.doClose()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) writeInternal(message json.Marshaler) bool {
|
func (c *Client) writeInternal(message json.Marshaler) bool {
|
||||||
|
|
Loading…
Reference in a new issue