From a802e7bd1ffbb7f4cd8505894d343ba02317da09 Mon Sep 17 00:00:00 2001 From: Vishal <64505169+vishalxl@users.noreply.github.com> Date: Fri, 23 Dec 2022 13:43:39 +0530 Subject: [PATCH] reduced items fetched to reduce cpu use etc fixed user count in relay which was undone few commits ago --- bin/nostr_console.dart | 45 ++++++++++++++++++++---------------------- lib/relays.dart | 33 ++++++++++++++++++++++--------- pubspec.yaml | 2 ++ 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index f8683f7..dd81c68 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -236,9 +236,6 @@ Future main(List arguments) async { } gEventsFilename = argResults[eventFileArg]; - if( gEventsFilename != "") { - //print("Going to use ${whetherDefault}file to read from and store events: $gEventsFilename"); - } } Set initialEvents = {}; // collect all events here and then create tree out of them @@ -247,15 +244,27 @@ Future main(List arguments) async { stdout.write('Reading events from ${whetherDefault}file.......'); // read file events and give the events to relays from where they're picked up later - //log.info("before reading events"); initialEvents = await readEventsFromFile(gEventsFilename); - //log.info("after reading events"); // count events initialEvents.forEach((element) { numFileEvents++;}); print("read $numFileEvents events from file $gEventsFilename"); } + int limitSelfEvents = 200; + int limitOthersEvents = 4; + int limitPerSubscription = gLimitPerSubscription; + + // if more than 1000 posts have already been read from the file, then don't get too many day's events. Only for last 3 days. + if(numFileEvents > 1000) { + limitPerSubscription = 5000; + limitSelfEvents = 10; + limitOthersEvents = 3; + gDefaultNumWaitSeconds = gDefaultNumWaitSeconds ~/5; + } else { + printInfoForNewUser(); + } + // process request string. If this is blank then the application only reads from file and does not connect to internet. if( argResults[requestArg] != null) { int numWaitSeconds = gDefaultNumWaitSeconds; @@ -267,14 +276,18 @@ Future main(List arguments) async { numWaitSeconds = 0; gEventsFilename = ""; // so it wont write it back to keep it faster ( and since without internet no new event is there to be written ) } + + if( userPublicKey!= "") { + getUserEvents(gListRelayUrls1, userPublicKey, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents)); + getMentionEvents(gListRelayUrls1, {userPublicKey}, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents), "#p"); + } - Future.delayed(Duration(milliseconds: numWaitSeconds * 2), () { + Future.delayed(Duration(milliseconds: numWaitSeconds), () { Set receivedEvents = getRecievedEvents(); - //stdout.write("received ${receivedEvents.length - numFilePosts} events\n"); initialEvents.addAll(receivedEvents); - // Creat tree from all events read form file + // Create tree from all events read form file Store node = getTree(initialEvents); clearEvents(); @@ -293,21 +306,6 @@ Future main(List arguments) async { // then get the events of user-id's mentioned in p-tags of received events and the contact list // then display them all - int limitSelfEvents = 300; - int limitOthersEvents = 20; - - int limitPerSubscription = gLimitPerSubscription; - - // if more than 1000 posts have already been read from the file, then don't get too many day's events. Only for last 3 days. - if(numFileEvents > 1000) { - limitPerSubscription = 5000; - limitSelfEvents = 10; - limitOthersEvents = 3; - gDefaultNumWaitSeconds = gDefaultNumWaitSeconds ~/5 ; - } else { - printInfoForNewUser(); - } - // get event for user if( userPublicKey!= "") { getUserEvents(gListRelayUrls1, userPublicKey, limitPerSubscription, getSecondsDaysAgo(limitSelfEvents)); @@ -319,7 +317,6 @@ Future main(List arguments) async { // remove user from default list if he exists in it. because theyv'e already been fetched. gDefaultFollows = gDefaultFollows.difference(usersFetched); - //print("getting defaultfollows. usersFetched len = ${usersFetched.length} "); // get other user events getMultiUserEvents(gListRelayUrls1, gDefaultFollows, 4 * limitPerSubscription, getSecondsDaysAgo(limitOthersEvents)); usersFetched = usersFetched.union(gDefaultFollows); diff --git a/lib/relays.dart b/lib/relays.dart index f33bef5..6f24b7d 100644 --- a/lib/relays.dart +++ b/lib/relays.dart @@ -8,7 +8,7 @@ import 'package:web_socket_channel/io.dart'; class Relay { String url; IOWebSocketChannel socket; - List users; // is used so that duplicate requests aren't sent for same user for this same relay + Set users; // is used so that duplicate requests aren't sent for same user for this same relay int numReceived; int numRequestsSent; Relay(this.url, this.socket, this.users, this.numReceived, this.numRequestsSent); @@ -40,7 +40,7 @@ class Relays { IOWebSocketChannel fws = IOWebSocketChannel.connect(relayUrl); print('In Relay.relay: connecting to relay $relayUrl'); Map mapRelay = {}; - Relay relayObject = Relay( relayUrl, fws, [], 0, 0); + Relay relayObject = Relay( relayUrl, fws, {}, 0, 0); mapRelay[relayUrl] = relayObject; return Relays(mapRelay, {}, {}); @@ -67,14 +67,19 @@ class Relays { String subscriptionId = "single_user" + (relays[relayUrl]?.numRequestsSent??"").toString() + "_" + relayUrl.substring(6); if( relays.containsKey(relayUrl)) { - List? users = relays[relayUrl]?.users; + Set? users = relays[relayUrl]?.users; if( users != null) { // get a user only if it has not already been requested // following is too restrictive casuse changed sinceWhen is not considered. TODO improve it - for(int i = 0; i < users.length; i++) { - if( users[i] == publicKey) { - return; + bool alreadyRecevied = false; + users.forEach((user) { + if( user == publicKey) { + alreadyRecevied = true; } - } + }); + + if( alreadyRecevied) + return; + users.add(publicKey); } } @@ -104,9 +109,19 @@ class Relays { void getMultiUserEvents(String relayUrl, List publicKeys, int limit, int sinceWhen, [Set? kind = null]) { //print("In relays: getmulti events kind = $kind len ${publicKeys.length}"); + Set setPublicKeys = publicKeys.toSet(); + + if( relays.containsKey(relayUrl)) { + Set? users = relays[relayUrl]?.users; + if( users != null) { + relays[relayUrl]?.users = users.union(setPublicKeys); + + } + } + String subscriptionId = "multiple_user" + (relays[relayUrl]?.numRequestsSent??"").toString() + "_" + relayUrl.substring(6); - String request = getMultiUserRequest( subscriptionId, publicKeys.toSet(), limit, sinceWhen, kind); + String request = getMultiUserRequest( subscriptionId, setPublicKeys, limit, sinceWhen, kind); sendRequest(relayUrl, request); } @@ -132,7 +147,7 @@ class Relays { try { IOWebSocketChannel fws2 = IOWebSocketChannel.connect(relay); - Relay newRelay = Relay(relay, fws2, [], 0, 1); + Relay newRelay = Relay(relay, fws2, {}, 0, 1); relays[relay] = newRelay; fws = fws2; fws2.stream.listen( diff --git a/pubspec.yaml b/pubspec.yaml index 9b9475d..47ab750 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,8 @@ homepage: https://github.com/vishalxl/nostr_console # fixed new issue of taking longer time when file was already there # increased channel fetches from 2 days from half a day +# reduced items fetched. 23/12 + environment: sdk: '>=2.17.3 <3.0.0'