mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-27 01:02:44 +02:00
cancel relay context when calling .Close()
.
fixing https://github.com/nbd-wtf/go-nostr/issues/76#issuecomment-1517251898
This commit is contained in:
parent
3785daf8aa
commit
9b89a49e5e
16
relay.go
16
relay.go
@ -42,10 +42,11 @@ type Relay struct {
|
|||||||
Connection *Connection
|
Connection *Connection
|
||||||
subscriptions s.MapOf[string, *Subscription]
|
subscriptions s.MapOf[string, *Subscription]
|
||||||
|
|
||||||
Challenges chan string // NIP-42 Challenges
|
Challenges chan string // NIP-42 Challenges
|
||||||
Notices chan string
|
Notices chan string
|
||||||
ConnectionError error
|
ConnectionError error
|
||||||
ConnectionContext context.Context // will be canceled when the connection closes
|
ConnectionContext context.Context // will be canceled when the connection closes
|
||||||
|
connectionContextCancel context.CancelFunc
|
||||||
|
|
||||||
okCallbacks s.MapOf[string, func(bool, string)]
|
okCallbacks s.MapOf[string, func(bool, string)]
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
@ -75,6 +76,7 @@ func (r *Relay) String() string {
|
|||||||
func (r *Relay) Connect(ctx context.Context) error {
|
func (r *Relay) Connect(ctx context.Context) error {
|
||||||
connectionContext, cancel := context.WithCancel(ctx)
|
connectionContext, cancel := context.WithCancel(ctx)
|
||||||
r.ConnectionContext = connectionContext
|
r.ConnectionContext = connectionContext
|
||||||
|
r.connectionContextCancel = cancel
|
||||||
|
|
||||||
if r.URL == "" {
|
if r.URL == "" {
|
||||||
cancel()
|
cancel()
|
||||||
@ -470,5 +472,11 @@ func (r *Relay) PrepareSubscription(ctx context.Context) *Subscription {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Relay) Close() error {
|
func (r *Relay) Close() error {
|
||||||
|
if r.connectionContextCancel == nil {
|
||||||
|
return fmt.Errorf("relay not connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
r.connectionContextCancel()
|
||||||
|
r.connectionContextCancel = nil
|
||||||
return r.Connection.Close()
|
return r.Connection.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user