Skip to content

Commit

Permalink
Very high message rate cap :)
Browse files Browse the repository at this point in the history
Custom listener manager to cancel pending tasks after having quit. These
will most likely be waiting for a ping response or the sender semaphore,
so interrupting them will work just fine.

Ping and duration of request completion in MDC.
  • Loading branch information
Tillerino committed Feb 13, 2015
1 parent 3ed2c4f commit 5baf60a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.ExecutorService;

import javax.annotation.CheckForNull;
import javax.inject.Inject;
Expand All @@ -22,11 +23,13 @@
import org.pircbotx.Configuration.Builder;
import org.pircbotx.hooks.events.PartEvent;
import org.pircbotx.hooks.events.QuitEvent;
import org.pircbotx.snapshot.ChannelSnapshot;
import org.pircbotx.hooks.managers.ThreadedListenerManager;
import org.pircbotx.snapshot.UserChannelDaoSnapshot;
import org.pircbotx.snapshot.UserSnapshot;
import org.pircbotx.PircBotX;

import com.google.common.collect.Lists;

@Slf4j
@Singleton
public class BotRunnerImpl implements BotRunner, TidyObject {
Expand Down Expand Up @@ -120,6 +123,32 @@ public void processCommand(String target, String sourceNick, String sourceLogin,
}
}

static class CustomThreadedListenerManager extends ThreadedListenerManager<PircBotX> {
public CustomThreadedListenerManager() {
super();
}

public CustomThreadedListenerManager(ExecutorService pool) {
super(pool);
}

@Override
public void shutdown(PircBotX bot) {
List<ManagedFutureTask> remainingTasks;
synchronized (runningListeners) {
remainingTasks = Lists.newArrayList(runningListeners.get(bot));
}

for (ManagedFutureTask curFuture : remainingTasks) {
try {
curFuture.cancel(true);
} catch (Exception e) {
log.error("exception cancelling future", e);
}
}
}
}

volatile CloseableBot bot = null;

@Inject
Expand Down Expand Up @@ -168,7 +197,8 @@ public void run() {
try {
@SuppressWarnings("unchecked")
Builder<PircBotX> configurationBuilder = new Configuration.Builder<PircBotX>()
.setServer(server, port).setMessageDelay(1000)
.setServer(server, port).setMessageDelay(250)
.setListenerManager(new CustomThreadedListenerManager())
.setName(nickname).addListener(listener)
.setEncoding(Charset.forName("UTF-8"))
.setAutoReconnect(false)
Expand Down
56 changes: 38 additions & 18 deletions tillerinobot/src/main/java/tillerino/tillerinobot/IRCBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.MDC;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.hooks.CoreHooks;
import org.pircbotx.hooks.Event;
Expand Down Expand Up @@ -129,7 +130,12 @@ public void onAction(ActionEvent event) throws Exception {
return;

if (event.getChannel() == null || event.getUser().getNick().equals("Tillerino")) {
processPrivateAction(fromIRC(event.getUser()), event.getMessage());
MDC.put("user", event.getUser().getNick());
try {
processPrivateAction(fromIRC(event.getUser(), event), event.getMessage());
} finally {
MDC.remove("user");
}
}
}

Expand All @@ -144,7 +150,6 @@ public Semaphore load(String arg0) throws Exception {
});

void processPrivateAction(IRCBotUser user, String message) {
MDC.put("user", user.getNick());
log.info("action: " + message);

Language lang = new Default();
Expand Down Expand Up @@ -218,14 +223,19 @@ public void onPrivateMessage(PrivateMessageEvent event) throws Exception {
if(silent)
return;

processPrivateMessage(fromIRC(event.getUser()), event.getMessage());
MDC.put("user", event.getUser().getNick());
try {
processPrivateMessage(fromIRC(event.getUser(), event), event.getMessage());
} finally {
MDC.remove("user");
}
}

Semaphore senderSemaphore = new Semaphore(1, true);

final Pinger pinger;

IRCBotUser fromIRC(final User user) {
IRCBotUser fromIRC(final User user, final Event<PircBotX> event) {
return new IRCBotUser() {

@Override
Expand All @@ -239,6 +249,7 @@ public boolean message(String msg) {
pinger.ping((CloseableBot) user.getBot());

user.send().message(msg);
MDC.put("duration", System.currentTimeMillis() - event.getTimestamp());
log.info("sent: " + msg);
botInfo.setLastSentMessage(System.currentTimeMillis());
return true;
Expand All @@ -247,6 +258,8 @@ public boolean message(String msg) {
return false;
} finally {
senderSemaphore.release();
MDC.remove("ping");
MDC.remove("duration");
}
}

Expand All @@ -268,6 +281,7 @@ public boolean action(String msg) {
return false;
} finally {
senderSemaphore.release();
MDC.remove("ping");
}
}

Expand All @@ -281,7 +295,6 @@ public String getNick() {
}

void processPrivateMessage(final IRCBotUser user, String originalMessage) {
MDC.put("user", user.getNick());
log.info("received: " + originalMessage);

Language lang = new Default();
Expand Down Expand Up @@ -365,16 +378,19 @@ public void onDisconnect(DisconnectEvent event) throws Exception {
@Override
public void onEvent(Event event) throws Exception {
MDC.put("event", lastSerial.incrementAndGet());

botInfo.setLastInteraction(System.currentTimeMillis());

if(lastListTime < System.currentTimeMillis() - 60 * 60 * 1000) {
lastListTime = System.currentTimeMillis();

event.getBot().sendRaw().rawLine("NAMES #osu");
try {
botInfo.setLastInteraction(System.currentTimeMillis());

if (lastListTime < System.currentTimeMillis() - 60 * 60 * 1000) {
lastListTime = System.currentTimeMillis();

event.getBot().sendRaw().rawLine("NAMES #osu");
}

super.onEvent(event);
} finally {
MDC.remove("event");
}

super.onEvent(event);
}

@Override
Expand Down Expand Up @@ -408,10 +424,14 @@ public void onJoin(JoinEvent event) throws Exception {
}

MDC.put("user", nick);
IRCBotUser user = fromIRC(event.getUser());
welcomeIfDonator(user);

scheduleRegisterActivity(nick);
try {
IRCBotUser user = fromIRC(event.getUser(), event);
welcomeIfDonator(user);

scheduleRegisterActivity(nick);
} finally {
MDC.remove("user");
}
}

void welcomeIfDonator(IRCBotUser user) {
Expand Down
3 changes: 3 additions & 0 deletions tillerinobot/src/main/java/tillerino/tillerinobot/Pinger.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.management.AttributeChangeNotification;
import javax.management.MBeanNotificationInfo;

import org.apache.log4j.MDC;
import org.pircbotx.Utils;
import org.pircbotx.hooks.events.UnknownEvent;

Expand Down Expand Up @@ -119,10 +120,12 @@ void ping(CloseableBot bot) throws IOException, InterruptedException {
Utils.sendRawLineToServer(bot, "PING " + pingMessage);

if(!pingLatch.await(10, TimeUnit.SECONDS)) {
MDC.put("ping", 10000);
throw new IOException("ping timed out");
}

bean.setLastPing(System.currentTimeMillis() - time);
MDC.put("ping", bean.getLastPing());

if (bean.getLastPing() > 1500) {
if (botInfoService != null) {
Expand Down

0 comments on commit 5baf60a

Please sign in to comment.