mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-19 18:47:37 +01:00
Add mutexes around websockets
We replace the bare websocket.Conn type with a new Connection type which implements `WriteJSON`, `WriteMessage`, and `Close`. The Connection type adds mutexes around writes since gorilla doesn't support concurrent writes to websockets. Signed-off-by: Honza Pokorny <honza@pokorny.ca>
This commit is contained in:
14
relaypool.go
14
relaypool.go
@@ -28,7 +28,7 @@ type RelayPool struct {
|
||||
SecretKey *string
|
||||
|
||||
Relays map[string]RelayPoolPolicy
|
||||
websockets map[string]*websocket.Conn
|
||||
websockets map[string]*Connection
|
||||
subscriptions map[string]*Subscription
|
||||
|
||||
Notices chan *NoticeMessage
|
||||
@@ -61,7 +61,7 @@ type NoticeMessage struct {
|
||||
func NewRelayPool() *RelayPool {
|
||||
return &RelayPool{
|
||||
Relays: make(map[string]RelayPoolPolicy),
|
||||
websockets: make(map[string]*websocket.Conn),
|
||||
websockets: make(map[string]*Connection),
|
||||
subscriptions: make(map[string]*Subscription),
|
||||
|
||||
Notices: make(chan *NoticeMessage),
|
||||
@@ -80,11 +80,13 @@ func (r *RelayPool) Add(url string, policy RelayPoolPolicy) error {
|
||||
return fmt.Errorf("invalid relay URL '%s'", url)
|
||||
}
|
||||
|
||||
conn, _, err := websocket.DefaultDialer.Dial(NormalizeURL(url), nil)
|
||||
socket, _, err := websocket.DefaultDialer.Dial(NormalizeURL(url), nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening websocket to '%s': %w", nm, err)
|
||||
}
|
||||
|
||||
conn := NewConnection(socket)
|
||||
|
||||
r.Relays[nm] = policy
|
||||
r.websockets[nm] = conn
|
||||
|
||||
@@ -94,7 +96,7 @@ func (r *RelayPool) Add(url string, policy RelayPoolPolicy) error {
|
||||
|
||||
go func() {
|
||||
for {
|
||||
typ, message, err := conn.ReadMessage()
|
||||
typ, message, err := conn.socket.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println("read error: ", err)
|
||||
return
|
||||
@@ -183,7 +185,7 @@ func (r *RelayPool) Sub(filters EventFilters) *Subscription {
|
||||
|
||||
subscription := Subscription{filters: filters}
|
||||
subscription.channel = hex.EncodeToString(random)
|
||||
subscription.relays = make(map[string]*websocket.Conn)
|
||||
subscription.relays = make(map[string]*Connection)
|
||||
for relay, policy := range r.Relays {
|
||||
if policy.ShouldRead(filters) {
|
||||
ws := r.websockets[relay]
|
||||
@@ -225,7 +227,7 @@ func (r *RelayPool) PublishEvent(evt *Event) (*Event, chan PublishStatus, error)
|
||||
continue
|
||||
}
|
||||
|
||||
go func(relay string, conn *websocket.Conn) {
|
||||
go func(relay string, conn *Connection) {
|
||||
err := conn.WriteJSON([]interface{}{"EVENT", evt})
|
||||
if err != nil {
|
||||
log.Printf("error sending event to '%s': %s", relay, err.Error())
|
||||
|
||||
Reference in New Issue
Block a user