Skip to content

Commit

Permalink
Fix turtle expirations
Browse files Browse the repository at this point in the history
Tunnels, search requests and search replies would not expire and prevent normal functions.
  • Loading branch information
zapek committed Nov 18, 2024
1 parent 988b03f commit d2f57e6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class TurtleRsService extends RsService implements RsServiceMaster<Turtle
/**
* Time between checks of normal tunnels.
*/
private static final Duration REGULAR_TUNNELS_DIGGING_TIME = Duration.ofSeconds(300);
private static final Duration REGULAR_TUNNELS_DIGGING_TIME = Duration.ofMinutes(5);

/**
* Time between tunnels cleanup.
Expand Down Expand Up @@ -116,12 +116,12 @@ public class TurtleRsService extends RsService implements RsServiceMaster<Turtle
/**
* Lifetime of search requests in the cache.
*/
private static final Duration SEARCH_REQUEST_LIFETIME = Duration.ofSeconds(600);
private static final Duration SEARCH_REQUEST_LIFETIME = Duration.ofMinutes(10);

/**
* Lifetime of tunnel requests in the cache.
*/
private static final Duration TUNNEL_REQUEST_LIFETIME = Duration.ofSeconds(600);
private static final Duration TUNNEL_REQUEST_LIFETIME = Duration.ofMinutes(10);

/**
* Lifetime of an ongoing search requests. Results coming after that time are dropped.
Expand Down Expand Up @@ -563,7 +563,7 @@ private void handleSearchRequest(PeerConnection sender, TurtleSearchRequestItem
return;
}

if (searchRequestsOrigins.size() > MAX_SEARCH_REQUEST_IN_CACHE)
if (searchRequestsOrigins.size() > MAX_SEARCH_REQUEST_IN_CACHE) // XXX: no expiration for those??
{
log.debug("Request cache is full. Check if a peer is flooding.");
return;
Expand Down Expand Up @@ -853,15 +853,15 @@ private void cleanTunnelsIfNeeded()

// Search requests
searchRequestsOrigins.entrySet().removeIf(entry ->
Duration.between(now, entry.getValue().getLastUsed()).compareTo(SEARCH_REQUEST_LIFETIME) > 0);
Duration.between(entry.getValue().getLastUsed(), now).compareTo(SEARCH_REQUEST_LIFETIME) > 0);

// Tunnel requests
tunnelRequestsOrigins.entrySet().removeIf(entry ->
Duration.between(now, entry.getValue().getLastUsed()).compareTo(TUNNEL_REQUEST_LIFETIME) > 0);
Duration.between(entry.getValue().getLastUsed(), now).compareTo(TUNNEL_REQUEST_LIFETIME) > 0);

// Tunnels
localTunnels.entrySet().stream()
.filter(entry -> Duration.between(now, entry.getValue().getLastUsed()).compareTo(MAX_TUNNEL_IDLE_TIME) > 0)
.filter(entry -> Duration.between(entry.getValue().getLastUsed(), now).compareTo(MAX_TUNNEL_IDLE_TIME) > 0)
.forEach(entry -> closeTunnel(entry.getKey(), virtualPeersToRemove));

// Remove all the virtual peer ids from the clients
Expand Down
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: "com.vaadin.external.google", module: "android-json"
}
testImplementation "com.tngtech.archunit:archunit-junit5:$archunitVersion"
testFixturesImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: "com.vaadin.external.google", module: "android-json"
}
Expand Down
57 changes: 57 additions & 0 deletions common/src/test/java/io/xeres/common/CommonCodingRulesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.common;

import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaModifier;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import io.xeres.common.id.Identifier;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

@AnalyzeClasses(packagesOf = AppName.class, importOptions = ImportOption.DoNotIncludeTests.class)
class CommonCodingRulesTest
{
/**
* The serializer uses the 'LENGTH' field of identifiers to be able to deserialize them.
* Make sure they all implement one.
*/
@ArchTest
private final ArchRule identifierPublicLengthField = classes()
.that().areAssignableTo(Identifier.class)
.and().doNotBelongToAnyOf(Identifier.class)
.should(new ArchCondition<>("have a public field called LENGTH")
{
@Override
public void check(JavaClass javaClass, ConditionEvents events)
{
boolean satisfied = javaClass.getField("LENGTH").getModifiers().contains(JavaModifier.PUBLIC);
String message = javaClass.getDescription() + (satisfied ? " has" : " does not have")
+ " a public field called LENGTH";
events.add(new SimpleConditionEvent(javaClass, satisfied, message));
}
});
}
12 changes: 12 additions & 0 deletions ui/src/test/java/io/xeres/ui/UiCodingRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@
import io.xeres.ui.controller.WindowController;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.library.GeneralCodingRules.ACCESS_STANDARD_STREAMS;
import static com.tngtech.archunit.library.GeneralCodingRules.NO_CLASSES_SHOULD_USE_FIELD_INJECTION;

@AnalyzeClasses(packagesOf = JavaFxApplication.class, importOptions = ImportOption.DoNotIncludeTests.class)
class UiCodingRulesTest
{
@ArchTest
private final ArchRule noAccessToStandardStreams = noClasses()
.should(ACCESS_STANDARD_STREAMS)
.because("We use loggers");

@ArchTest
private final ArchRule noFieldInjection = NO_CLASSES_SHOULD_USE_FIELD_INJECTION
.because("Constructor injection allow detection of cyclic dependencies");

@ArchTest
private final ArchRule windowNaming = classes()
.that().implement(WindowController.class)
Expand Down

0 comments on commit d2f57e6

Please sign in to comment.