get rid of mutexes and use a single loop to prevent races.

in the meantime change the API to makes a little less error-prone.
This commit is contained in:
fiatjaf
2023-06-21 19:55:40 -03:00
parent af4fc98fc2
commit 030c1d1898
4 changed files with 274 additions and 179 deletions

View File

@@ -9,7 +9,6 @@ import (
"io"
"net"
"net/http"
"sync"
"github.com/gobwas/httphead"
"github.com/gobwas/ws"
@@ -26,7 +25,6 @@ type Connection struct {
flateWriter *wsflate.Writer
writer *wsutil.Writer
msgState *wsflate.MessageState
mutex sync.Mutex
}
func NewConnection(ctx context.Context, url string, requestHeader http.Header) (*Connection, error) {
@@ -100,17 +98,7 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header) (
}, nil
}
func (c *Connection) Ping() error {
c.mutex.Lock()
defer c.mutex.Unlock()
return wsutil.WriteClientMessage(c.conn, ws.OpPing, nil)
}
func (c *Connection) WriteMessage(data []byte) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.msgState.IsCompressed() && c.enableCompression {
c.flateWriter.Reset(c.writer)
if _, err := io.Copy(c.flateWriter, bytes.NewReader(data)); err != nil {