fix excess query spam

This commit is contained in:
Martti Malmi 2023-03-28 14:30:46 +03:00
parent 1af6c8b905
commit 32de1b5b7d
5 changed files with 38 additions and 37 deletions

View File

@ -710,7 +710,11 @@ const Events = {
if (this.directRepliesByMessageId.has(id) || this.likesByMessageId.has(id)) {
callback();
}
return PubSub.subscribe([{ kinds: [1, 6, 7, 9735], '#e': [id] }], callback);
PubSub.subscribedRepliesAndReactions.add(id);
PubSub.subscribeToRepliesAndReactions();
return () => {
PubSub.subscribedRepliesAndReactions.delete(id);
};
},
getEventById(id: string, proxyFirst = false, cb?: (event: Event) => void) {
const event = this.db.by('id', id);
@ -718,28 +722,19 @@ const Events = {
cb(event);
return;
}
const askWs = () => {
const unsub = PubSub.subscribe([{ ids: [id] }], () => {
const msg = this.db.by('id', id);
if (msg) {
cb?.(msg);
unsub();
}
});
};
PubSub.subscribedPosts.add(id);
if (proxyFirst) {
// give proxy 300 ms to respond, then ask ws
const askWsTimeout = setTimeout(() => {
askWs();
const askRelaysTimeout = setTimeout(() => {
PubSub.subscribeToPosts();
}, 300);
fetch(`https://api.iris.to/event/${id}`).then((res) => {
if (res.status === 200) {
res.json().then((event) => {
// TODO verify sig
if (event && event.id === id) {
clearTimeout(askWsTimeout);
PubSub.subscribedPosts.add(id);
clearTimeout(askRelaysTimeout);
Events.handle(event, true);
cb?.(event);
}
@ -747,7 +742,7 @@ const Events = {
}
});
} else {
askWs();
PubSub.subscribeToPosts();
}
},
getDirectMessagesByUser(address: string, cb?: (messageIds: string[]) => void): Unsubscribe {
@ -756,14 +751,28 @@ const Events = {
};
this.directMessagesByUser.has(address) && callback();
const myPub = Key.getPubKey();
return PubSub.subscribe([{ kinds: [4], '#p': [myPub], authors: [address] }, { kinds: [4], '#p': [address], authors: [myPub] }], callback);
return PubSub.subscribe(
[
{ kinds: [4], '#p': [myPub], authors: [address] },
{ kinds: [4], '#p': [address], authors: [myPub] },
],
callback,
'dmsByUser',
);
},
getDirectMessages(cb) {
getDirectMessages(cb?) {
const callback = () => {
cb?.(this.directMessagesByUser);
};
callback();
return PubSub.subscribe([{ kinds: [4], '#p': [Key.getPubKey()] }], callback);
return PubSub.subscribe(
[
{ kinds: [4], '#p': [Key.getPubKey()] },
{ kinds: [4], authors: [Key.getPubKey()] },
],
callback,
'dms',
);
},
};

View File

@ -161,13 +161,13 @@ const PubSub = {
callback,
});
}
if (name) {
const existing = this.internalSubscriptionsByName.get(name);
if (existing) {
this.subscriptions.delete(existing);
}
this.internalSubscriptionsByName.set(name, currentSubscriptionId);
name = name || JSON.stringify(filters);
const existing = this.internalSubscriptionsByName.get(name);
if (existing) {
this.subscriptions.delete(existing);
}
this.internalSubscriptionsByName.set(name, currentSubscriptionId);
filters.forEach((f) => {
const query = {};

View File

@ -302,7 +302,6 @@ export default {
resubscribe(relay?: Relay) {
console.log('subscribedFiltersByName.size', PubSub.subscribedFiltersByName.size);
for (const [name, filters] of Array.from(PubSub.subscribedFiltersByName.entries())) {
console.log('resubscribing to ', name, filters);
this.subscribe(filters.filters, name, false, 0, filters.sinceRelayLastSeen, relay && [relay]);
}
},
@ -370,7 +369,6 @@ export default {
return unsubscribe;
}
authorRelayUrls = this.getUserRelays(author).filter(([url, settings]) => {
console.log('already have?', url, this.relays.has(url));
return settings.write && !this.relays.has(url);
});
// pick 3 random relays

View File

@ -138,8 +138,7 @@ const Session = {
'notifications',
true,
); // notifications
PubSub.subscribe([{ '#p': [myPub], kinds: [4] }], undefined, 'dms', true); // notifications and DMs
PubSub.subscribe([{ '#p': [myPub], kinds: [4], limit: 20 }], undefined, 'latestDms', true); // notifications and DMs
Events.getDirectMessages();
}, 200);
setInterval(() => {
console.log('handled msgs per second', Math.round(Events.handledMsgsPerSecond / 5));

View File

@ -119,14 +119,6 @@ export default {
}
this.followedByUser.get(follower)?.add(followedUser);
if (follower === myPub) {
PubSub.subscribe(
[{ kinds: [1, 5, 7], authors: [followedUser] }],
undefined,
'followed',
true,
);
}
if (followedUser === myPub) {
if (this.followersByUser.get(followedUser)?.size === 1) {
localState.get('hasNostrFollowers').put(true);
@ -269,7 +261,10 @@ export default {
});
}
PubSub.subscribedProfiles.add(address);
return PubSub.subscribe([{ authors: [address], kinds: [0, 3] }], callback);
PubSub.subscribeToAuthors();
return () => {
PubSub.subscribedProfiles.delete(address);
};
},
setMetadata(data: any) {
const event = {