2
2
3
3
import io .prometheus .client .Collector ;
4
4
import io .prometheus .client .CollectorRegistry ;
5
- import org .bukkit .Bukkit ;
6
- import org .bukkit .plugin .Plugin ;
7
-
8
5
import java .util .concurrent .CompletableFuture ;
9
- import java .util .concurrent .ExecutionException ;
10
6
import java .util .logging .Logger ;
7
+ import org .bukkit .Bukkit ;
8
+ import org .bukkit .plugin .Plugin ;
9
+ import org .jetbrains .annotations .Nullable ;
11
10
12
11
public abstract class Metric {
13
12
@@ -27,42 +26,36 @@ protected Plugin getPlugin() {
27
26
return plugin ;
28
27
}
29
28
29
+ @ Nullable
30
30
public CompletableFuture <Void > collect () {
31
- return CompletableFuture .runAsync (() -> {
32
- if (!enabled ) {
33
- return ;
34
- }
35
-
36
- /* If metric is capable of async execution run it on a thread pool */
37
- if (isAsyncCapable ()) {
31
+ if (!isEnabled ()) {
32
+ return null ;
33
+ }
38
34
35
+ if (isAsyncCapable ()) {
36
+ CompletableFuture .runAsync (() -> {
39
37
try {
40
38
doCollect ();
41
- }
42
- catch (Exception e ) {
39
+ } catch (Exception e ) {
43
40
logException (e );
44
41
}
45
- return ;
46
- }
42
+ }) ;
43
+ }
47
44
48
- /*
49
- * Otherwise run the metric on the main thread and make the
50
- * thread on thread pool wait for completion
51
- */
45
+ CompletableFuture < Void > future = new CompletableFuture <>();
46
+
47
+ // don't call .get() - this blocks the ForkJoinPool.commonPool and may deadlock the server in some cases
48
+ Bukkit . getScheduler (). callSyncMethod ( plugin , () -> {
52
49
try {
53
- Bukkit .getScheduler ().callSyncMethod (plugin , () -> {
54
- try {
55
- doCollect ();
56
- }
57
- catch (Exception e ) {
58
- logException (e );
59
- }
60
- return null ;
61
- }).get ();
50
+ doCollect ();
62
51
} catch (Exception e ) {
63
52
logException (e );
64
53
}
54
+ future .complete (null );
55
+ return null ;
65
56
});
57
+
58
+ return future ;
66
59
}
67
60
68
61
protected abstract void doCollect ();
0 commit comments