Add sync catcher to the blocking pipeline methods

This commit is contained in:
Alexander Söderberg 2020-05-17 23:26:19 +02:00
parent 58989c9311
commit b5f92f5003
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.util;
import com.plotsquared.core.PlotSquared;
import lombok.experimental.UtilityClass;
@UtilityClass public class ThreadUtils {
/**
* Throws {@link IllegalStateException} if the method
* is called from the server main thread
*
* @param message Message describing the issue
*/
public void catchSync(final String message) {
if (PlotSquared.get().isMainThread(Thread.currentThread())) {
throw new IllegalStateException(message);
}
}
/**
* Throws {@link IllegalStateException} if the method
* is not called from the server main thread
*
* @param message Message describing the issue
*/
public void catchAsync(final String message) {
if (!PlotSquared.get().isMainThread(Thread.currentThread())) {
throw new IllegalStateException(message);
}
}
}

View File

@ -27,6 +27,7 @@ package com.plotsquared.core.uuid;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.util.ThreadUtils;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -144,6 +145,7 @@ public class UUIDPipeline {
* @return The mapped uuid. Will return null if the request timed out. * @return The mapped uuid. Will return null if the request timed out.
*/ */
@Nullable public UUID getSingle(@NotNull final String username, final long timeout) { @Nullable public UUID getSingle(@NotNull final String username, final long timeout) {
ThreadUtils.catchSync("Blocking UUID retrieval from the main thread");
try { try {
this.getUUIDs(Collections.singletonList(username)).get(timeout, TimeUnit.MILLISECONDS); this.getUUIDs(Collections.singletonList(username)).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
@ -162,6 +164,7 @@ public class UUIDPipeline {
* @return The mapped username. Will return null if the request timeout. * @return The mapped username. Will return null if the request timeout.
*/ */
@Nullable public String getSingle(@NotNull final UUID uuid, final long timeout) { @Nullable public String getSingle(@NotNull final UUID uuid, final long timeout) {
ThreadUtils.catchSync("Blocking username retrieval from the main thread");
try { try {
this.getNames(Collections.singletonList(uuid)).get(timeout, TimeUnit.MILLISECONDS); this.getNames(Collections.singletonList(uuid)).get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {