Put dropped router values into top relay selections
This commit is contained in:
@@ -154,11 +154,11 @@ export const mergeSubscriptions = (subs: Subscription[]) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(
|
console.log(
|
||||||
// `Starting ${mergedSubscriptions.length} subscriptions on ${uniq(mergedSubscriptions.flatMap(s => s.request.relays)).length} relays`,
|
`Starting ${mergedSubscriptions.length} subscriptions on ${uniq(mergedSubscriptions.flatMap(s => s.request.relays)).length} relays`,
|
||||||
// uniq(mergedSubscriptions.flatMap(s => s.request.relays)),
|
uniq(mergedSubscriptions.flatMap(s => s.request.relays)),
|
||||||
// ...mergeFilters(mergedSubscriptions.flatMap(s => s.request.filters)),
|
...mergeFilters(mergedSubscriptions.flatMap(s => s.request.filters)),
|
||||||
// )
|
)
|
||||||
|
|
||||||
return mergedSubscriptions
|
return mergedSubscriptions
|
||||||
}
|
}
|
||||||
@@ -253,6 +253,12 @@ export const subscribe = (request: SubscribeRequest) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (request.immediate) {
|
if (request.immediate) {
|
||||||
|
console.log(
|
||||||
|
`Starting 1 subscriptions on ${request.relays.length} relays`,
|
||||||
|
request.relays,
|
||||||
|
...mergeFilters(request.filters)
|
||||||
|
)
|
||||||
|
|
||||||
executeSubscription(subscription)
|
executeSubscription(subscription)
|
||||||
} else {
|
} else {
|
||||||
executeSubscriptionBatched(subscription)
|
executeSubscriptionBatched(subscription)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type {Event} from 'nostr-tools'
|
import type {Event} from 'nostr-tools'
|
||||||
import {matchFilter as nostrToolsMatchFilter} from 'nostr-tools'
|
import {matchFilter as nostrToolsMatchFilter} from 'nostr-tools'
|
||||||
import {prop, hash, identity, groupBy, randomId, uniq} from '@coracle.social/lib'
|
import {prop, avg, hash, identity, groupBy, randomId, uniq} from '@coracle.social/lib'
|
||||||
import type {Rumor} from './Events'
|
import type {Rumor} from './Events'
|
||||||
import {decodeAddress, addressFromEvent, encodeAddress} from './Address'
|
import {decodeAddress, addressFromEvent, encodeAddress} from './Address'
|
||||||
import {isReplaceableKind} from './Kinds'
|
import {isReplaceableKind} from './Kinds'
|
||||||
@@ -168,5 +168,5 @@ export const getFilterGenerality = (filter: Filter) => {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
export const guessFilterDelta = (filters: Filter[], max = 60 * 60 * 24) =>
|
export const guessFilterDelta = (filters: Filter[], max = 60 * 60 * 24 * 7) =>
|
||||||
Math.round(max * Math.max(0.005, 1 - filters.map(getFilterGenerality).reduce((a, b) => a + b) / filters.length))
|
Math.round(max * Math.max(0.005, 1 - avg(filters.map(getFilterGenerality))))
|
||||||
|
|||||||
+11
-11
@@ -1,5 +1,5 @@
|
|||||||
import type {EventTemplate} from 'nostr-tools'
|
import type {EventTemplate} from 'nostr-tools'
|
||||||
import {first, identity, sortBy, uniq, shuffle, pushToMapKey} from '@coracle.social/lib'
|
import {first, splitAt, identity, sortBy, uniq, shuffle, pushToMapKey} from '@coracle.social/lib'
|
||||||
import {Tags, Tag} from '@coracle.social/util'
|
import {Tags, Tag} from '@coracle.social/util'
|
||||||
import type {Rumor} from './Events'
|
import type {Rumor} from './Events'
|
||||||
import {getAddress, isReplaceable} from './Events'
|
import {getAddress, isReplaceable} from './Events'
|
||||||
@@ -44,12 +44,6 @@ export class Router {
|
|||||||
|
|
||||||
// Utilities derived from options
|
// Utilities derived from options
|
||||||
|
|
||||||
getTagSelections = (tags: Tags) =>
|
|
||||||
tags
|
|
||||||
.filter(t => isShareableRelayUrl(t.nth(2)))
|
|
||||||
.mapTo(t => this.selection(t.nth(1), [t.nth(2)]))
|
|
||||||
.valueOf()
|
|
||||||
|
|
||||||
getPubkeySelection = (pubkey: string, mode?: RelayMode) =>
|
getPubkeySelection = (pubkey: string, mode?: RelayMode) =>
|
||||||
this.selection(pubkey, this.options.getPubkeyRelays(pubkey, mode))
|
this.selection(pubkey, this.options.getPubkeyRelays(pubkey, mode))
|
||||||
|
|
||||||
@@ -288,7 +282,9 @@ export class RouterScenario {
|
|||||||
allValues.add(value)
|
allValues.add(value)
|
||||||
|
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
pushToMapKey(valuesByRelay, relay, value)
|
if (isShareableRelayUrl(relay)) {
|
||||||
|
pushToMapKey(valuesByRelay, relay, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +292,7 @@ export class RouterScenario {
|
|||||||
// are wee're less tolerant of failure. Add more redundancy to fill our relay limit.
|
// are wee're less tolerant of failure. Add more redundancy to fill our relay limit.
|
||||||
const limit = this.getLimit()
|
const limit = this.getLimit()
|
||||||
const redundancy = this.getRedundancy()
|
const redundancy = this.getRedundancy()
|
||||||
const adjustedRedundancy = redundancy * (limit / (allValues.size * redundancy))
|
const adjustedRedundancy = Math.max(redundancy, redundancy * (limit / (allValues.size * redundancy)))
|
||||||
|
|
||||||
const seen = new Map<string, number>()
|
const seen = new Map<string, number>()
|
||||||
const result: ValuesByRelay = new Map()
|
const result: ValuesByRelay = new Map()
|
||||||
@@ -330,9 +326,13 @@ export class RouterScenario {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const selections = this.router.relaySelectionsFromMap(result)
|
const [keep, discard] = splitAt(limit, this.router.relaySelectionsFromMap(result))
|
||||||
|
|
||||||
return limit ? selections.slice(0, limit) : selections
|
for (const target of keep.slice(0, redundancy)) {
|
||||||
|
target.values = uniq(discard.concat(target).flatMap((selection: RelayValues) => selection.values))
|
||||||
|
}
|
||||||
|
|
||||||
|
return keep
|
||||||
}
|
}
|
||||||
|
|
||||||
getUrls = () => this.getSelections().map((selection: RelayValues) => selection.relay)
|
getUrls = () => this.getSelections().map((selection: RelayValues) => selection.relay)
|
||||||
|
|||||||
Reference in New Issue
Block a user