Skip to content

Make render interval slightly more configurable #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class CoreConfig {

private int renderThreadCount = 1;

private int updateRegionAfterInactivity = 5;
private int updateRegionMaxInactivityWait = 60;

private boolean metrics = true;

private Path data = Path.of("bluemap");
Expand All @@ -57,6 +60,14 @@ public int resolveRenderThreadCount() {
return Math.max(Runtime.getRuntime().availableProcessors() + renderThreadCount, 1);
}

public int getUpdateRegionAfterInactivity() {
return updateRegionAfterInactivity;
}

public int getUpdateRegionMaxInactivityWait() {
return updateRegionMaxInactivityWait;
}

public boolean isMetrics() {
return metrics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
package de.bluecolored.bluemap.common.plugin;

import com.flowpowered.math.vector.Vector2i;
import de.bluecolored.bluemap.common.BlueMapConfiguration;
import de.bluecolored.bluemap.common.rendermanager.RenderManager;
import de.bluecolored.bluemap.common.rendermanager.WorldRegionRenderTask;
import de.bluecolored.bluemap.core.logger.Logger;
import de.bluecolored.bluemap.core.map.BmMap;
import de.bluecolored.bluemap.core.util.WatchService;

import java.io.IOException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
Expand All @@ -42,19 +44,35 @@ public class MapUpdateService extends Thread {
private final BmMap map;
private final RenderManager renderManager;
private final WatchService<Vector2i> watchService;
private final int inactivityWait;
private final int inactivityMaxWait;

private volatile boolean closed;

private Timer delayTimer;

private final Map<Vector2i, TimerTask> scheduledUpdates;
private final Map<Vector2i, RegionUpdateInfo> scheduledUpdates;

public MapUpdateService(RenderManager renderManager, BmMap map) throws IOException {
private static class RegionUpdateInfo {
TimerTask task;
Instant waitingSince;

RegionUpdateInfo(TimerTask task, Instant waitingSince) {
this.task = task;
this.waitingSince = waitingSince;
}
}

public MapUpdateService(BlueMapConfiguration configService, RenderManager renderManager, BmMap map) throws IOException {
this.renderManager = renderManager;
this.map = map;
this.closed = false;
this.scheduledUpdates = new HashMap<>();
this.watchService = map.getWorld().createRegionWatchService();

var coreConfig = configService.getCoreConfig();
inactivityWait = coreConfig.getUpdateRegionAfterInactivity();
inactivityMaxWait = coreConfig.getUpdateRegionMaxInactivityWait();
}

@Override
Expand Down Expand Up @@ -83,11 +101,17 @@ public void run() {
private synchronized void updateRegion(Vector2i regionPos) {
if (closed) return;

// we only want to start the render when there were no changes on a file for 5 seconds
TimerTask task = scheduledUpdates.remove(regionPos);
if (task != null) task.cancel();
// we only want to start the render when there were no changes on a file for inactivityWait seconds
RegionUpdateInfo previous = scheduledUpdates.get(regionPos);
if (previous != null) {
if (previous.waitingSince.plusSeconds(inactivityMaxWait).isBefore(Instant.now())) {
// exceeded max inactivity timeout, don't reschedule
return;
}
previous.task.cancel();
}

task = new TimerTask() {
TimerTask task = new TimerTask() {
@Override
public void run() {
synchronized (MapUpdateService.this) {
Expand All @@ -99,8 +123,13 @@ public void run() {
}
}
};
scheduledUpdates.put(regionPos, task);
delayTimer.schedule(task, 5000);

if (previous == null) {
scheduledUpdates.put(regionPos, new RegionUpdateInfo(task, Instant.now()));
} else {
previous.task = task;
}
delayTimer.schedule(task, inactivityWait * 1000L);
}

public synchronized void close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public synchronized void startWatchingMap(BmMap map) {
stopWatchingMap(map);

try {
MapUpdateService watcher = new MapUpdateService(renderManager, map);
MapUpdateService watcher = new MapUpdateService(blueMap.getConfig(), renderManager, map);
watcher.start();
mapUpdateServices.put(map.getId(), watcher);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ data: "${data}"
# Default is 1
render-thread-count: ${render-thread-count}

# Wait for a region to be inactive for this many seconds before rerendering.
update-region-after-inactivity: 5
# Max time to wait for region inactivity (in seconds) before proceeding anyways.
update-region-max-inactivity-wait: 60

# Controls whether BlueMap should try to find and load mod-resources and datapacks from the server/world-directories.
# Default is true
scan-for-mod-resources: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void renderMaps(BlueMapService blueMap, boolean watch, TileUpdateStrategy
if (watch) {
for (BmMap map : maps.values()) {
try {
MapUpdateService watcher = new MapUpdateService(renderManager, map);
MapUpdateService watcher = new MapUpdateService(blueMap.getConfig(), renderManager, map);
watcher.start();
mapUpdateServices.add(watcher);
} catch (IOException ex) {
Expand Down
Loading