Skip to content

Commit

Permalink
Added different warnings when concurrent messages occur.
Browse files Browse the repository at this point in the history
New method in Language.

Switched to Java 8
  • Loading branch information
Tillerino committed Oct 23, 2015
1 parent 01f7199 commit 08034e0
Show file tree
Hide file tree
Showing 26 changed files with 161 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: java
jdk:
- openjdk7
- oraclejdk8
before_install:
- git submodule init
- git submodule update
Expand Down
4 changes: 2 additions & 2 deletions tillerinobot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<version>3.0</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Expand Down
48 changes: 40 additions & 8 deletions tillerinobot/src/main/java/tillerino/tillerinobot/IRCBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
import javax.inject.Inject;
Expand Down Expand Up @@ -144,13 +145,17 @@ public void onAction(ActionEvent event) throws Exception {

/**
* This wrapper around a Semaphore keeps track of when it was last acquired
* via {@link #tryAcquire()}.
* via {@link #tryAcquire()} and what happened since.
*/
public static class TimingSemaphore {
private long lastAcquired = 0;

private Thread lastAcquiredThread = null;

private int attemptsSinceLastAcquired = 0;

private boolean sentWarning = false;

private final Semaphore semaphore;

public TimingSemaphore(int permits, boolean fair) {
Expand All @@ -163,6 +168,8 @@ public synchronized boolean tryAcquire() {
}
lastAcquired = System.currentTimeMillis();
lastAcquiredThread = Thread.currentThread();
attemptsSinceLastAcquired = 0;
sentWarning = false;
return true;
}

Expand All @@ -177,6 +184,18 @@ public Thread getLastAcquiredThread() {
public void release() {
semaphore.release();
}

public int getAttemptsSinceLastAcquired() {
return ++attemptsSinceLastAcquired;
}

public boolean isSentWarning() {
if(!sentWarning) {
sentWarning = true;
return false;
}
return true;
}
}

/**
Expand All @@ -189,12 +208,25 @@ public TimingSemaphore load(String arg0) throws Exception {
}
});

void handleSemaphoreInUse(String purpose, TimingSemaphore semaphore) {
void handleSemaphoreInUse(String purpose, TimingSemaphore semaphore, Language lang, IRCBotUser user) {
double processing = (System.currentTimeMillis() - semaphore.getLastAcquired()) / 1000d;
StackTraceElement[] stackTrace = semaphore.getLastAcquiredThread().getStackTrace();
Throwable t = new Throwable("Processing thread's stack trace");
t.setStackTrace(stackTrace);
log.warn(purpose + " - request has been processing for " + processing, t);
if(processing > 5) {
StackTraceElement[] stackTrace = semaphore.getLastAcquiredThread().getStackTrace();
stackTrace = Stream.of(stackTrace)
.filter(elem -> elem.getClassName().contains("tillerino"))
.toArray(StackTraceElement[]::new);
Throwable t = new Throwable("Processing thread's stack trace");
t.setStackTrace(stackTrace);
log.warn(purpose + " - request has been processing for " + processing, t);
if(!semaphore.isSentWarning()) {
user.message(lang.getPatience());
}
} else {
log.debug(purpose);
}
if(semaphore.getAttemptsSinceLastAcquired() >= 3 && !semaphore.isSentWarning()) {
user.message("[http://i.imgur.com/Ykfua8r.png ...]");
}
}

void processPrivateAction(IRCBotUser user, String message) {
Expand All @@ -204,7 +236,7 @@ void processPrivateAction(IRCBotUser user, String message) {

TimingSemaphore semaphore = perUserLock.getUnchecked(user.getNick());
if(!semaphore.tryAcquire()) {
handleSemaphoreInUse("concurrent action", semaphore);
handleSemaphoreInUse("concurrent action", semaphore, lang, user);
return;
}

Expand Down Expand Up @@ -354,7 +386,7 @@ void processPrivateMessage(final IRCBotUser user, String originalMessage) {

TimingSemaphore semaphore = perUserLock.getUnchecked(user.getNick());
if(!semaphore.tryAcquire()) {
handleSemaphoreInUse("concurrent message", semaphore);
handleSemaphoreInUse("concurrent message", semaphore, lang, user);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,9 @@ public String noRecentPlays() {
public String isSetId() {
return "This references a set of beatmaps, not a single beatmap.";
}

@Override
public String getPatience() {
return "Just a second...";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,9 @@ public String noRecentPlays() {
public String isSetId() {
return "Das bezieht sich auf ein Set von Beatmaps statt auf eine einzelne Beatmap.";
}

@Override
public String getPatience() {
return "Gib mir einen Moment...";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.tillerino.osuApiModel.Mods;
import org.tillerino.osuApiModel.OsuApiUser;

import tillerino.tillerinobot.BeatmapMeta;
import tillerino.tillerinobot.IRCBot.IRCBotUser;
import tillerino.tillerinobot.RecommendationsManager.Recommendation;
Expand Down Expand Up @@ -304,4 +305,11 @@ public interface Language {
* @return
*/
String isSetId();

/**
* Bot is doing things. Please have patience.
*
* @return
*/
String getPatience();
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,9 @@ public String noRecentPlays() {
public String isSetId() {
return new Default().isSetId();
}

@Override
public String getPatience() {
return new Default().getPatience();
}
}

0 comments on commit 08034e0

Please sign in to comment.