get rid of PoolOptions, just set fields on Pool directly.
This commit is contained in:
+1
-1
@@ -112,7 +112,7 @@ func NewBunker(
|
|||||||
onAuth func(string),
|
onAuth func(string),
|
||||||
) *BunkerClient {
|
) *BunkerClient {
|
||||||
if pool == nil {
|
if pool == nil {
|
||||||
pool = nostr.NewPool(nostr.PoolOptions{})
|
pool = nostr.NewPool()
|
||||||
}
|
}
|
||||||
|
|
||||||
clientPublicKey := nostr.GetPublicKey(clientSecretKey)
|
clientPublicKey := nostr.GetPublicKey(clientSecretKey)
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func NewBunkerFromNostrConnect(
|
|||||||
pool *nostr.Pool,
|
pool *nostr.Pool,
|
||||||
) (*BunkerClient, error) {
|
) (*BunkerClient, error) {
|
||||||
if pool == nil {
|
if pool == nil {
|
||||||
pool = nostr.NewPool(nostr.PoolOptions{})
|
pool = nostr.NewPool()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(relayURLs) == 0 {
|
if len(relayURLs) == 0 {
|
||||||
|
|||||||
@@ -27,10 +27,10 @@ type Pool struct {
|
|||||||
authRequiredHandler func(context.Context, *Event) error
|
authRequiredHandler func(context.Context, *Event) error
|
||||||
cancel context.CancelCauseFunc
|
cancel context.CancelCauseFunc
|
||||||
|
|
||||||
eventMiddleware func(RelayEvent)
|
EventMiddleware func(RelayEvent)
|
||||||
duplicateMiddleware func(relay string, id ID)
|
DuplicateMiddleware func(relay string, id ID)
|
||||||
queryMiddleware func(relay string, pubkey PubKey, kind Kind)
|
QueryMiddleware func(relay string, pubkey PubKey, kind Kind)
|
||||||
relayOptions RelayOptions
|
RelayOptions RelayOptions
|
||||||
|
|
||||||
// custom things not often used
|
// custom things not often used
|
||||||
penaltyBox *xsync.MapOf[string, [2]float64]
|
penaltyBox *xsync.MapOf[string, [2]float64]
|
||||||
@@ -49,27 +49,15 @@ func (df DirectedFilter) String() string {
|
|||||||
func (ie RelayEvent) String() string { return fmt.Sprintf("[%s] >> %s", ie.Relay.URL, ie.Event) }
|
func (ie RelayEvent) String() string { return fmt.Sprintf("[%s] >> %s", ie.Relay.URL, ie.Event) }
|
||||||
|
|
||||||
// NewPool creates a new Pool with the given context and options.
|
// NewPool creates a new Pool with the given context and options.
|
||||||
func NewPool(opts PoolOptions) *Pool {
|
func NewPool() *Pool {
|
||||||
ctx, cancel := context.WithCancelCause(context.Background())
|
ctx, cancel := context.WithCancelCause(context.Background())
|
||||||
|
|
||||||
pool := &Pool{
|
return &Pool{
|
||||||
Relays: xsync.NewMapOf[string, *Relay](),
|
Relays: xsync.NewMapOf[string, *Relay](),
|
||||||
|
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
|
|
||||||
authRequiredHandler: opts.AuthRequiredHandler,
|
|
||||||
eventMiddleware: opts.EventMiddleware,
|
|
||||||
duplicateMiddleware: opts.DuplicateMiddleware,
|
|
||||||
queryMiddleware: opts.AuthorKindQueryMiddleware,
|
|
||||||
relayOptions: opts.RelayOptions,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.PenaltyBox {
|
|
||||||
go pool.startPenaltyBox()
|
|
||||||
}
|
|
||||||
|
|
||||||
return pool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PoolOptions struct {
|
type PoolOptions struct {
|
||||||
@@ -97,31 +85,35 @@ type PoolOptions struct {
|
|||||||
RelayOptions RelayOptions
|
RelayOptions RelayOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pool *Pool) startPenaltyBox() {
|
func (pool *Pool) StartPenaltyBox() {
|
||||||
pool.penaltyBox = xsync.NewMapOf[string, [2]float64]()
|
pool.penaltyBox = xsync.NewMapOf[string, [2]float64]()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
sleep := 30.0
|
sleep := 30.0
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Duration(sleep) * time.Second)
|
select {
|
||||||
|
case <-pool.Context.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(time.Duration(sleep) * time.Second):
|
||||||
|
|
||||||
nextSleep := 300.0
|
nextSleep := 300.0
|
||||||
for url, v := range pool.penaltyBox.Range {
|
for url, v := range pool.penaltyBox.Range {
|
||||||
remainingSeconds := v[1]
|
remainingSeconds := v[1]
|
||||||
remainingSeconds -= sleep
|
remainingSeconds -= sleep
|
||||||
if remainingSeconds <= 0 {
|
if remainingSeconds <= 0 {
|
||||||
pool.penaltyBox.Store(url, [2]float64{v[0], 0})
|
pool.penaltyBox.Store(url, [2]float64{v[0], 0})
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
pool.penaltyBox.Store(url, [2]float64{v[0], remainingSeconds})
|
pool.penaltyBox.Store(url, [2]float64{v[0], remainingSeconds})
|
||||||
|
}
|
||||||
|
|
||||||
|
if remainingSeconds < nextSleep {
|
||||||
|
nextSleep = remainingSeconds
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if remainingSeconds < nextSleep {
|
sleep = nextSleep
|
||||||
nextSleep = remainingSeconds
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep = nextSleep
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -145,7 +137,7 @@ func (pool *Pool) EnsureRelay(url string) (*Relay, error) {
|
|||||||
return relay, nil
|
return relay, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
relay = NewRelay(pool.Context, url, pool.relayOptions)
|
relay = NewRelay(pool.Context, url, pool.RelayOptions)
|
||||||
// try to connect
|
// try to connect
|
||||||
// we use this ctx here so when the pool dies everything dies
|
// we use this ctx here so when the pool dies everything dies
|
||||||
if err := relay.Connect(pool.Context); err != nil {
|
if err := relay.Connect(pool.Context); err != nil {
|
||||||
@@ -277,8 +269,8 @@ func (pool *Pool) fetchMany(
|
|||||||
if opts.CheckDuplicate == nil {
|
if opts.CheckDuplicate == nil {
|
||||||
opts.CheckDuplicate = func(id ID, relay string) bool {
|
opts.CheckDuplicate = func(id ID, relay string) bool {
|
||||||
_, exists := seenAlready.LoadOrStore(id, struct{}{})
|
_, exists := seenAlready.LoadOrStore(id, struct{}{})
|
||||||
if exists && pool.duplicateMiddleware != nil {
|
if exists && pool.DuplicateMiddleware != nil {
|
||||||
pool.duplicateMiddleware(relay, id)
|
pool.DuplicateMiddleware(relay, id)
|
||||||
}
|
}
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
@@ -359,7 +351,7 @@ func (pool *Pool) FetchManyReplaceable(
|
|||||||
go func(nm string) {
|
go func(nm string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
if mh := pool.queryMiddleware; mh != nil {
|
if mh := pool.QueryMiddleware; mh != nil {
|
||||||
if filter.Kinds != nil && filter.Authors != nil {
|
if filter.Kinds != nil && filter.Authors != nil {
|
||||||
for _, kind := range filter.Kinds {
|
for _, kind := range filter.Kinds {
|
||||||
for _, author := range filter.Authors {
|
for _, author := range filter.Authors {
|
||||||
@@ -407,7 +399,7 @@ func (pool *Pool) FetchManyReplaceable(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ie := RelayEvent{Event: evt, Relay: relay}
|
ie := RelayEvent{Event: evt, Relay: relay}
|
||||||
if mh := pool.eventMiddleware; mh != nil {
|
if mh := pool.EventMiddleware; mh != nil {
|
||||||
mh(ie)
|
mh(ie)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -450,8 +442,8 @@ func (pool *Pool) subMany(
|
|||||||
if opts.CheckDuplicate == nil {
|
if opts.CheckDuplicate == nil {
|
||||||
opts.CheckDuplicate = func(id ID, relay string) bool {
|
opts.CheckDuplicate = func(id ID, relay string) bool {
|
||||||
_, exists := seenAlready.LoadOrStore(id, Now())
|
_, exists := seenAlready.LoadOrStore(id, Now())
|
||||||
if exists && pool.duplicateMiddleware != nil {
|
if exists && pool.DuplicateMiddleware != nil {
|
||||||
pool.duplicateMiddleware(relay, id)
|
pool.DuplicateMiddleware(relay, id)
|
||||||
}
|
}
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
@@ -498,7 +490,7 @@ func (pool *Pool) subMany(
|
|||||||
|
|
||||||
var sub *Subscription
|
var sub *Subscription
|
||||||
|
|
||||||
if mh := pool.queryMiddleware; mh != nil {
|
if mh := pool.QueryMiddleware; mh != nil {
|
||||||
if filter.Kinds != nil && filter.Authors != nil {
|
if filter.Kinds != nil && filter.Authors != nil {
|
||||||
for _, kind := range filter.Kinds {
|
for _, kind := range filter.Kinds {
|
||||||
for _, author := range filter.Authors {
|
for _, author := range filter.Authors {
|
||||||
@@ -548,7 +540,7 @@ func (pool *Pool) subMany(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ie := RelayEvent{Event: evt, Relay: relay}
|
ie := RelayEvent{Event: evt, Relay: relay}
|
||||||
if mh := pool.eventMiddleware; mh != nil {
|
if mh := pool.EventMiddleware; mh != nil {
|
||||||
mh(ie)
|
mh(ie)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -642,7 +634,7 @@ func (pool *Pool) subManyEose(
|
|||||||
go func(nm string) {
|
go func(nm string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
if mh := pool.queryMiddleware; mh != nil {
|
if mh := pool.QueryMiddleware; mh != nil {
|
||||||
if filter.Kinds != nil && filter.Authors != nil {
|
if filter.Kinds != nil && filter.Authors != nil {
|
||||||
for _, kind := range filter.Kinds {
|
for _, kind := range filter.Kinds {
|
||||||
for _, author := range filter.Authors {
|
for _, author := range filter.Authors {
|
||||||
@@ -709,7 +701,7 @@ func (pool *Pool) subManyEose(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ie := RelayEvent{Event: evt, Relay: relay}
|
ie := RelayEvent{Event: evt, Relay: relay}
|
||||||
if mh := pool.eventMiddleware; mh != nil {
|
if mh := pool.EventMiddleware; mh != nil {
|
||||||
mh(ie)
|
mh(ie)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -808,8 +800,8 @@ func (pool *Pool) batchedQueryMany(
|
|||||||
|
|
||||||
opts.CheckDuplicate = func(id ID, relay string) bool {
|
opts.CheckDuplicate = func(id ID, relay string) bool {
|
||||||
_, exists := seenAlready.LoadOrStore(id, struct{}{})
|
_, exists := seenAlready.LoadOrStore(id, struct{}{})
|
||||||
if exists && pool.duplicateMiddleware != nil {
|
if exists && pool.DuplicateMiddleware != nil {
|
||||||
pool.duplicateMiddleware(relay, id)
|
pool.DuplicateMiddleware(relay, id)
|
||||||
}
|
}
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
@@ -897,8 +889,8 @@ func (pool *Pool) batchedSubscribeMany(
|
|||||||
|
|
||||||
opts.CheckDuplicate = func(id ID, relay string) bool {
|
opts.CheckDuplicate = func(id ID, relay string) bool {
|
||||||
_, exists := seenAlready.LoadOrStore(id, struct{}{})
|
_, exists := seenAlready.LoadOrStore(id, struct{}{})
|
||||||
if exists && pool.duplicateMiddleware != nil {
|
if exists && pool.DuplicateMiddleware != nil {
|
||||||
pool.duplicateMiddleware(relay, id)
|
pool.DuplicateMiddleware(relay, id)
|
||||||
}
|
}
|
||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -364,7 +364,6 @@ func (r *Relay) handleMessage(message string) {
|
|||||||
|
|
||||||
switch env := envelope.(type) {
|
switch env := envelope.(type) {
|
||||||
case *NoticeEnvelope:
|
case *NoticeEnvelope:
|
||||||
// see WithNoticeHandler
|
|
||||||
if r.noticeHandler != nil {
|
if r.noticeHandler != nil {
|
||||||
r.noticeHandler(r, string(*env))
|
r.noticeHandler(r, string(*env))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+5
-6
@@ -177,12 +177,11 @@ func NewSystem() *System {
|
|||||||
Hints: memoryh.NewHintDB(),
|
Hints: memoryh.NewHintDB(),
|
||||||
}
|
}
|
||||||
|
|
||||||
sys.Pool = nostr.NewPool(nostr.PoolOptions{
|
sys.Pool = nostr.NewPool()
|
||||||
AuthorKindQueryMiddleware: sys.TrackQueryAttempts,
|
sys.Pool.QueryMiddleware = sys.TrackQueryAttempts
|
||||||
EventMiddleware: sys.TrackEventHintsAndRelays,
|
sys.Pool.EventMiddleware = sys.TrackEventHintsAndRelays
|
||||||
DuplicateMiddleware: sys.TrackEventRelaysD,
|
sys.Pool.DuplicateMiddleware = sys.TrackEventRelaysD
|
||||||
PenaltyBox: true,
|
sys.Pool.StartPenaltyBox()
|
||||||
})
|
|
||||||
|
|
||||||
sys.metadataCacheOnce.Do(func() {
|
sys.metadataCacheOnce.Do(func() {
|
||||||
if sys.MetadataCache == nil {
|
if sys.MetadataCache == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user