did not delete old entries, rather just replaced the relay with new variable

to avoid crashes that i was seeing. with this change, no crash seen in a while...
This commit is contained in:
Vishal 2022-12-24 21:45:49 +05:30
parent b794840c24
commit c37783acee
2 changed files with 19 additions and 18 deletions

View File

@ -404,6 +404,7 @@ Future<void> main(List<String> arguments) async {
if( gDebug > 0) log.info("Received ptag events events."); if( gDebug > 0) log.info("Received ptag events events.");
resetRelays(); resetRelays();
relays = Relays({}, {}, {}); // reset relay value
String req = '["REQ","cn",{"limit":40000,"kinds":[0,1,3,4,5,6,7,40,41,42,104,140,141,142],"since":${getTimeSecondsAgo(2*3600).toString()}}]'; String req = '["REQ","cn",{"limit":40000,"kinds":[0,1,3,4,5,6,7,40,41,42,104,140,141,142],"since":${getTimeSecondsAgo(2*3600).toString()}}]';
sendRequest(gListRelayUrls1, req); sendRequest(gListRelayUrls1, req);

View File

@ -8,7 +8,7 @@ import 'package:web_socket_channel/io.dart';
class Relay { class Relay {
String url; String url;
IOWebSocketChannel socket; IOWebSocketChannel socket;
Set<String> users; // is used so that duplicate requests aren't sent for same user for this same relay Set<String> users; // is used so that duplicate requests aren't sent for same user for this same relay; unused for now
int numReceived; int numReceived;
int numRequestsSent; int numRequestsSent;
Relay(this.url, this.socket, this.users, this.numReceived, this.numRequestsSent); Relay(this.url, this.socket, this.users, this.numReceived, this.numRequestsSent);
@ -36,7 +36,7 @@ class Relays {
relay.close(); relay.close();
}); });
relays.clear(); //relays.clear();
} }
void printInfo() { void printInfo() {
@ -148,27 +148,27 @@ class Relays {
/* /*
* Send the given string to the given relay. Is used to send both requests, and to send evnets. * Send the given string to the given relay. Is used to send both requests, and to send evnets.
*/ */
void sendRequest(String relay, String request) { void sendRequest(String relayUrl, String request) {
if(relay == "" ) { if(relayUrl == "" ) {
if( gDebug != 0) print ("Invalid or empty relay given"); if( gDebug != 0) print ("Invalid or empty relay given");
return; return;
} }
if( gDebug > 0) print ("\nIn relay.sendRequest for relay $relay"); if( gDebug > 0) print ("\nIn relay.sendRequest for relay $relayUrl");
IOWebSocketChannel? fws; IOWebSocketChannel? fws;
if(relays.containsKey(relay)) { if(relays.containsKey(relayUrl)) {
fws = relays[relay]?.socket; fws = relays[relayUrl]?.socket;
relays[relay]?.numRequestsSent++; relays[relayUrl]?.numRequestsSent++;
} }
else { else {
if(gDebug !=0) print('connecting to $relay'); if(gDebug !=0) print('connecting to $relayUrl');
try { try {
IOWebSocketChannel fws2 = IOWebSocketChannel.connect(relay); IOWebSocketChannel fws2 = IOWebSocketChannel.connect(relayUrl);
Relay newRelay = Relay(relay, fws2, {}, 0, 1); Relay newRelay = Relay(relayUrl, fws2, {}, 0, 1);
relays[relay] = newRelay; relays[relayUrl] = newRelay;
fws = fws2; fws = fws2;
fws2.stream.listen( fws2.stream.listen(
(d) { (d) {
@ -185,7 +185,7 @@ class Relays {
return; return;
} }
e = Event.fromJson(d, relay); e = Event.fromJson(d, relayUrl);
if( rEvents.add(e) ) { if( rEvents.add(e) ) {
uniqueIdsRecieved.add(id); uniqueIdsRecieved.add(id);
@ -197,24 +197,24 @@ class Relays {
//dynamic json = jsonDecode(d); //dynamic json = jsonDecode(d);
return; return;
} }
}, },
onError: (err) { printWarning("\nWarning: Error in creating connection to $relay. Kindly check your internet connection. Or maybe only this relay is down."); }, onError: (err) { printWarning("Warning: Error in creating connection to $relayUrl. Kindly check your internet connection. Or maybe only this relay is down."); },
onDone: () { if( gDebug > 0) print('Info: In onDone'); } onDone: () { if( gDebug > 0) print('Info: In onDone'); }
); );
} on WebSocketException { } on WebSocketException {
print('WebSocketException exception for relay $relay'); print('WebSocketException exception for relay $relayUrl');
return; return;
} on Exception catch(ex) { } on Exception catch(ex) {
printWarning("Invalid event\n"); printWarning("Invalid event\n");
} }
catch(err) { catch(err) {
if( gDebug >= 0) printWarning('exception generic $err for relay $relay\n'); if( gDebug >= 0) printWarning('exception generic $err for relay $relayUrl\n');
return; return;
} }
} }
if(gDebug > 0) log.info('Sending request: \n$request\n to $relay\n\n'); if(gDebug > 0) log.info('Sending request: \n$request\n to $relayUrl\n\n');
fws?.sink.add(request); fws?.sink.add(request);
} }