mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01: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
|
||||
subscriptions s.MapOf[string, *Subscription]
|
||||
|
||||
Challenges chan string // NIP-42 Challenges
|
||||
Notices chan string
|
||||
ConnectionError error
|
||||
ConnectionContext context.Context // will be canceled when the connection closes
|
||||
Challenges chan string // NIP-42 Challenges
|
||||
Notices chan string
|
||||
ConnectionError error
|
||||
ConnectionContext context.Context // will be canceled when the connection closes
|
||||
connectionContextCancel context.CancelFunc
|
||||
|
||||
okCallbacks s.MapOf[string, func(bool, string)]
|
||||
mutex sync.RWMutex
|
||||
@ -75,6 +76,7 @@ func (r *Relay) String() string {
|
||||
func (r *Relay) Connect(ctx context.Context) error {
|
||||
connectionContext, cancel := context.WithCancel(ctx)
|
||||
r.ConnectionContext = connectionContext
|
||||
r.connectionContextCancel = cancel
|
||||
|
||||
if r.URL == "" {
|
||||
cancel()
|
||||
@ -470,5 +472,11 @@ func (r *Relay) PrepareSubscription(ctx context.Context) *Subscription {
|
||||
}
|
||||
|
||||
func (r *Relay) Close() error {
|
||||
if r.connectionContextCancel == nil {
|
||||
return fmt.Errorf("relay not connected")
|
||||
}
|
||||
|
||||
r.connectionContextCancel()
|
||||
r.connectionContextCancel = nil
|
||||
return r.Connection.Close()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user