mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-18 05:42:20 +01:00
SimplePool improvements.
This commit is contained in:
parent
2b2c000d6a
commit
c0a42ff2ff
19
pool.go
19
pool.go
@ -8,14 +8,21 @@ import (
|
||||
)
|
||||
|
||||
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{
|
||||
Relays: make(map[string]*Relay),
|
||||
|
||||
Context: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,13 +33,13 @@ func (pool *SimplePool) EnsureRelay(url string) *Relay {
|
||||
defer pool.mutex.Unlock()
|
||||
|
||||
relay, ok := pool.Relays[nm]
|
||||
if ok {
|
||||
if ok && relay.ConnectionContext.Err() == nil {
|
||||
// already connected, unlock and return
|
||||
return relay
|
||||
} else {
|
||||
var err error
|
||||
// when connecting to a relay we want the connection to persist forever if possible, so use a new context
|
||||
relay, err = RelayConnect(context.Background(), nm)
|
||||
// we use this ctx here so when the pool dies everything dies
|
||||
relay, err = RelayConnect(pool.Context, nm)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user