mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-03 16:32:10 +02:00
SimplePool improvements.
This commit is contained in:
15
pool.go
15
pool.go
@@ -9,13 +9,20 @@ import (
|
|||||||
|
|
||||||
type SimplePool struct {
|
type SimplePool struct {
|
||||||
Relays map[string]*Relay
|
Relays map[string]*Relay
|
||||||
|
Context context.Context
|
||||||
|
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSimplePool() *SimplePool {
|
func NewSimplePool(ctx context.Context) *SimplePool {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
return &SimplePool{
|
return &SimplePool{
|
||||||
Relays: make(map[string]*Relay),
|
Relays: make(map[string]*Relay),
|
||||||
|
|
||||||
|
Context: ctx,
|
||||||
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,13 +33,13 @@ func (pool *SimplePool) EnsureRelay(url string) *Relay {
|
|||||||
defer pool.mutex.Unlock()
|
defer pool.mutex.Unlock()
|
||||||
|
|
||||||
relay, ok := pool.Relays[nm]
|
relay, ok := pool.Relays[nm]
|
||||||
if ok {
|
if ok && relay.ConnectionContext.Err() == nil {
|
||||||
// already connected, unlock and return
|
// already connected, unlock and return
|
||||||
return relay
|
return relay
|
||||||
} else {
|
} else {
|
||||||
var err error
|
var err error
|
||||||
// when connecting to a relay we want the connection to persist forever if possible, so use a new context
|
// we use this ctx here so when the pool dies everything dies
|
||||||
relay, err = RelayConnect(context.Background(), nm)
|
relay, err = RelayConnect(pool.Context, nm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user