Skip to content

Commit d9c4116

Browse files
committed
log rate limiter
1 parent a10aa44 commit d9c4116

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/main/java/me/cortex/voxy/common/thread/ServiceThreadPool.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,18 @@ private void worker(int threadId) {
138138

139139
long[] seed = new long[]{1234342^(threadId*124987198651981L+215987981111L)};
140140
int[] revolvingSelector = new int[1];
141-
141+
long[] logIO = new long[] {0, System.currentTimeMillis()};
142142
while (true) {
143143
this.jobCounter.acquireUninterruptibly();
144144
if (!this.running) {
145145
break;
146146
}
147147
//This is because of JIT moment (it cant really replace methods while they are executing afak)
148-
this.worker_work(threadId, seed, revolvingSelector);
148+
this.worker_work(threadId, seed, revolvingSelector, logIO);
149149
}
150150
}
151151

152-
private void worker_work(int threadId, long[] seedIO, int[] revolvingSelectorIO) {
152+
private void worker_work(int threadId, long[] seedIO, int[] revolvingSelectorIO, long[] logIO) {
153153
final int ATTEMPT_COUNT = 50;
154154
int attempts = ATTEMPT_COUNT;
155155
while (true) {
@@ -204,9 +204,15 @@ private void worker_work(int threadId, long[] seedIO, int[] revolvingSelectorIO)
204204
}
205205
}
206206
if (service == null) {
207-
Logger.warn("No available jobs, sleeping releasing returning");
207+
logIO[0]++;
208+
long delta = System.currentTimeMillis()-logIO[1];
209+
if (delta>30_000) {
210+
logIO[1] = System.currentTimeMillis();
211+
Logger.warn("No available jobs, sleeping releasing returning: " + (delta/1000) + " attempts " + logIO[0]);
212+
logIO[0] = 0;
213+
}
208214
try {
209-
Thread.sleep((long) (200*Math.random()+5));
215+
Thread.sleep((long) (300*Math.random()+5));
210216
} catch (InterruptedException e) {
211217
throw new RuntimeException(e);
212218
}

0 commit comments

Comments
 (0)