Skip to content

Commit cde3ba0

Browse files
committed
Add a cooldown for updating the same region multiple times in a row, closes #698
1 parent c230f27 commit cde3ba0

File tree

6 files changed

+39
-161
lines changed

6 files changed

+39
-161
lines changed

common/src/main/java/de/bluecolored/bluemap/common/config/CoreConfig.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
*/
2525
package de.bluecolored.bluemap.common.config;
2626

27+
import lombok.Getter;
2728
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
2829

2930
import java.nio.file.Path;
3031

3132
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
3233
@ConfigSerializable
34+
@Getter
3335
public class CoreConfig {
3436

3537
private boolean acceptDownload = false;
@@ -39,54 +41,22 @@ public class CoreConfig {
3941
private boolean metrics = true;
4042

4143
private Path data = Path.of("bluemap");
42-
4344
private boolean scanForModResources = true;
4445

4546
private LogConfig log = new LogConfig();
4647

47-
public boolean isAcceptDownload() {
48-
return acceptDownload;
49-
}
50-
51-
public int getRenderThreadCount() {
52-
return renderThreadCount;
53-
}
54-
5548
public int resolveRenderThreadCount() {
5649
if (renderThreadCount > 0) return renderThreadCount;
5750
return Math.max(Runtime.getRuntime().availableProcessors() + renderThreadCount, 1);
5851
}
5952

60-
public boolean isMetrics() {
61-
return metrics;
62-
}
63-
64-
public Path getData() {
65-
return data;
66-
}
67-
68-
public boolean isScanForModResources() {
69-
return scanForModResources;
70-
}
71-
72-
public LogConfig getLog() {
73-
return log;
74-
}
75-
7653
@ConfigSerializable
54+
@Getter
7755
public static class LogConfig {
7856

7957
private String file = null;
8058
private boolean append = false;
8159

82-
public String getFile() {
83-
return file;
84-
}
85-
86-
public boolean isAppend() {
87-
return append;
88-
}
89-
9060
}
9161

9262
}

common/src/main/java/de/bluecolored/bluemap/common/config/PluginConfig.java

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
*/
2525
package de.bluecolored.bluemap.common.config;
2626

27+
import lombok.Getter;
2728
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
2829

30+
import java.time.Duration;
2931
import java.util.ArrayList;
3032
import java.util.List;
3133

3234
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
3335
@ConfigSerializable
36+
@Getter
3437
public class PluginConfig {
3538

3639
private boolean livePlayerMarkers = true;
@@ -50,58 +53,15 @@ public class PluginConfig {
5053

5154
private int playerRenderLimit = -1;
5255

56+
private int updateCooldown = 60;
5357
private int fullUpdateInterval = 1440;
5458

55-
public boolean isLivePlayerMarkers() {
56-
return livePlayerMarkers;
59+
public Duration getUpdateCooldown() {
60+
return Duration.ofSeconds(updateCooldown);
5761
}
5862

59-
public List<String> getHiddenGameModes() {
60-
return hiddenGameModes;
61-
}
62-
63-
public boolean isHideVanished() {
64-
return hideVanished;
65-
}
66-
67-
public boolean isHideInvisible() {
68-
return hideInvisible;
69-
}
70-
71-
public boolean isHideSneaking() {
72-
return hideSneaking;
73-
}
74-
75-
public boolean isHideDifferentWorld() {
76-
return hideDifferentWorld;
77-
}
78-
79-
public int getHideBelowSkyLight() {
80-
return hideBelowSkyLight;
81-
}
82-
83-
public int getHideBelowBlockLight() {
84-
return hideBelowBlockLight;
85-
}
86-
87-
public int getWriteMarkersInterval() {
88-
return writeMarkersInterval;
89-
}
90-
91-
public int getWritePlayersInterval() {
92-
return writePlayersInterval;
93-
}
94-
95-
public boolean isSkinDownload() {
96-
return skinDownload;
97-
}
98-
99-
public int getPlayerRenderLimit() {
100-
return playerRenderLimit;
101-
}
102-
103-
public int getFullUpdateInterval() {
104-
return fullUpdateInterval;
63+
public Duration getFullUpdateInterval() {
64+
return Duration.ofMinutes(fullUpdateInterval);
10565
}
10666

10767
}

common/src/main/java/de/bluecolored/bluemap/common/config/WebappConfig.java

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package de.bluecolored.bluemap.common.config;
2626

27+
import lombok.Getter;
2728
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
2829

2930
import java.nio.file.Path;
@@ -33,6 +34,7 @@
3334

3435
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
3536
@ConfigSerializable
37+
@Getter
3638
public class WebappConfig {
3739

3840
private boolean enabled = true;
@@ -65,80 +67,8 @@ public class WebappConfig {
6567
private Set<String> scripts = new HashSet<>();
6668
private Set<String> styles = new HashSet<>();
6769

68-
public boolean isEnabled() {
69-
return enabled;
70-
}
71-
72-
public Path getWebroot() {
73-
return webroot;
74-
}
75-
76-
public boolean isUpdateSettingsFile() {
77-
return updateSettingsFile;
78-
}
79-
80-
public boolean isUseCookies() {
81-
return useCookies;
82-
}
83-
84-
public boolean isDefaultToFlatView() {
85-
return defaultToFlatView;
86-
}
87-
8870
public Optional<String> getStartLocation() {
8971
return Optional.ofNullable(startLocation);
9072
}
9173

92-
public float getResolutionDefault() {
93-
return resolutionDefault;
94-
}
95-
96-
public int getMinZoomDistance() {
97-
return minZoomDistance;
98-
}
99-
100-
public int getMaxZoomDistance() {
101-
return maxZoomDistance;
102-
}
103-
104-
public int getHiresSliderMax() {
105-
return hiresSliderMax;
106-
}
107-
108-
public int getHiresSliderDefault() {
109-
return hiresSliderDefault;
110-
}
111-
112-
public int getHiresSliderMin() {
113-
return hiresSliderMin;
114-
}
115-
116-
public int getLowresSliderMax() {
117-
return lowresSliderMax;
118-
}
119-
120-
public int getLowresSliderDefault() {
121-
return lowresSliderDefault;
122-
}
123-
124-
public int getLowresSliderMin() {
125-
return lowresSliderMin;
126-
}
127-
128-
public String getMapDataRoot() {
129-
return mapDataRoot;
130-
}
131-
132-
public String getLiveDataRoot() {
133-
return liveDataRoot;
134-
}
135-
136-
public Set<String> getScripts() {
137-
return scripts;
138-
}
139-
140-
public Set<String> getStyles() {
141-
return styles;
142-
}
143-
14474
}

common/src/main/java/de/bluecolored/bluemap/common/plugin/MapUpdateService.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
package de.bluecolored.bluemap.common.plugin;
2626

2727
import com.flowpowered.math.vector.Vector2i;
28+
import com.github.benmanes.caffeine.cache.Cache;
29+
import com.github.benmanes.caffeine.cache.Caffeine;
2830
import de.bluecolored.bluemap.common.rendermanager.RenderManager;
2931
import de.bluecolored.bluemap.common.rendermanager.WorldRegionRenderTask;
3032
import de.bluecolored.bluemap.core.logger.Logger;
3133
import de.bluecolored.bluemap.core.map.BmMap;
3234
import de.bluecolored.bluemap.core.util.WatchService;
3335

3436
import java.io.IOException;
37+
import java.time.Duration;
3538
import java.util.HashMap;
3639
import java.util.Map;
3740
import java.util.Timer;
@@ -41,19 +44,25 @@ public class MapUpdateService extends Thread {
4144

4245
private final BmMap map;
4346
private final RenderManager renderManager;
47+
private final Duration regionUpdateCooldown;
4448
private final WatchService<Vector2i> watchService;
4549

4650
private volatile boolean closed;
4751

4852
private Timer delayTimer;
4953

5054
private final Map<Vector2i, TimerTask> scheduledUpdates;
55+
private final Cache<Vector2i, Long> lastUpdateTimes;
5156

52-
public MapUpdateService(RenderManager renderManager, BmMap map) throws IOException {
57+
public MapUpdateService(RenderManager renderManager, BmMap map, Duration regionUpdateCooldown) throws IOException {
5358
this.renderManager = renderManager;
5459
this.map = map;
60+
this.regionUpdateCooldown = regionUpdateCooldown;
5561
this.closed = false;
5662
this.scheduledUpdates = new HashMap<>();
63+
this.lastUpdateTimes = Caffeine.newBuilder()
64+
.expireAfterWrite(regionUpdateCooldown)
65+
.build();
5766
this.watchService = map.getWorld().createRegionWatchService();
5867
}
5968

@@ -83,7 +92,7 @@ public void run() {
8392
private synchronized void updateRegion(Vector2i regionPos) {
8493
if (closed) return;
8594

86-
// we only want to start the render when there were no changes on a file for 5 seconds
95+
// we only want to start the render when there were no changes on a file for at least 5 seconds
8796
TimerTask task = scheduledUpdates.remove(regionPos);
8897
if (task != null) task.cancel();
8998

@@ -94,13 +103,19 @@ public void run() {
94103
WorldRegionRenderTask task = new WorldRegionRenderTask(map, regionPos);
95104
scheduledUpdates.remove(regionPos);
96105
renderManager.scheduleRenderTask(task);
106+
lastUpdateTimes.put(regionPos, System.currentTimeMillis());
97107

98108
Logger.global.logDebug("Scheduled update for region-file: " + regionPos + " (Map: " + map.getId() + ")");
99109
}
100110
}
101111
};
112+
113+
Long lastUpdateTime = lastUpdateTimes.getIfPresent(regionPos);
114+
if (lastUpdateTime == null) lastUpdateTime = 0L;
115+
long timeSinceLastUpdate = System.currentTimeMillis() - lastUpdateTime;
116+
long delay = Math.max(regionUpdateCooldown.toMillis() - timeSinceLastUpdate, 5000);
102117
scheduledUpdates.put(regionPos, task);
103-
delayTimer.schedule(task, 5000);
118+
delayTimer.schedule(task, delay);
104119
}
105120

106121
public synchronized void close() {

common/src/main/java/de/bluecolored/bluemap/common/plugin/Plugin.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import java.net.UnknownHostException;
6868
import java.nio.file.Files;
6969
import java.nio.file.Path;
70+
import java.time.Duration;
7071
import java.time.Instant;
7172
import java.time.ZoneId;
7273
import java.time.ZonedDateTime;
@@ -328,8 +329,8 @@ public void run() {
328329
daemonTimer.schedule(fileWatcherRestartTask, TimeUnit.HOURS.toMillis(1), TimeUnit.HOURS.toMillis(1));
329330

330331
//periodically update all (non frozen) maps
331-
if (pluginConfig.getFullUpdateInterval() > 0) {
332-
long fullUpdateTime = TimeUnit.MINUTES.toMillis(pluginConfig.getFullUpdateInterval());
332+
long fullUpdateInterval = pluginConfig.getFullUpdateInterval().toMillis();
333+
if (fullUpdateInterval > 0) {
333334
TimerTask updateAllMapsTask = new TimerTask() {
334335
@Override
335336
public void run() {
@@ -340,7 +341,7 @@ public void run() {
340341
.toArray(RenderTask[]::new));
341342
}
342343
};
343-
daemonTimer.scheduleAtFixedRate(updateAllMapsTask, 0, fullUpdateTime);
344+
daemonTimer.scheduleAtFixedRate(updateAllMapsTask, 0, fullUpdateInterval);
344345
}
345346

346347
//metrics
@@ -565,8 +566,10 @@ public void savePlayerStates() {
565566
public synchronized void startWatchingMap(BmMap map) {
566567
stopWatchingMap(map);
567568

569+
if (blueMap == null) return;
570+
568571
try {
569-
MapUpdateService watcher = new MapUpdateService(renderManager, map);
572+
MapUpdateService watcher = new MapUpdateService(renderManager, map, blueMap.getConfig().getPluginConfig().getUpdateCooldown());
570573
watcher.start();
571574
mapUpdateServices.put(map.getId(), watcher);
572575
} catch (IOException ex) {

implementations/cli/src/main/java/de/bluecolored/bluemap/cli/BlueMapCLI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void renderMaps(BlueMapService blueMap, boolean watch, TileUpdateStrategy
103103
if (watch) {
104104
for (BmMap map : maps.values()) {
105105
try {
106-
MapUpdateService watcher = new MapUpdateService(renderManager, map);
106+
MapUpdateService watcher = new MapUpdateService(renderManager, map, blueMap.getConfig().getPluginConfig().getUpdateCooldown());
107107
watcher.start();
108108
mapUpdateServices.add(watcher);
109109
} catch (IOException ex) {

0 commit comments

Comments
 (0)