Skip to content

Commit d0e11c7

Browse files
committed
net_irc: Explicitly flush IRC channel logs at least occasionally.
It's a bad idea to flush writes for every single write, as that would have a significant performance impact for busy channels. However, for channels that aren't busy, a FILE*'s internal buffer may be large enough that it could be a very long time before buffered writes are flushed if we don't flush explicitly. If it's been a while since data was flushed to the log file, then go ahead and explicitly flush it.
1 parent da98562 commit d0e11c7

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

nets/net_irc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ struct irc_channel {
406406
unsigned int throttlecount; /* # of users that joined in the last throttle interval */
407407
struct stringlist invited; /* String list of invited nicks */
408408
FILE *fp; /* Optional log file to which to log all channel activity */
409+
time_t loglastflush; /* Time the log file was last flushed */
409410
RWLIST_ENTRY(irc_channel) entry; /* Next channel */
410411
struct bbs_rate_limit ratelimit; /* Time that last relayed message was sent */
411412
unsigned int relay:1; /* Enable relaying */
@@ -1188,6 +1189,14 @@ static int __channel_broadcast(int lock, struct irc_channel *channel, struct irc
11881189
localtime_r(&lognow, &logdate);
11891190
strftime(datestr, sizeof(datestr), "%Y-%m-%d %T", &logdate);
11901191
fprintf(channel->fp, "[%s] %s", datestr, buf); /* Assume it ends in CR LF (it better!) */
1192+
/* Flushing the log file to disk every message is unnecessarily expensive.
1193+
* However, if messages are only received occasionally, it may make sense to flush immediately,
1194+
* or in the worst case it might be days or weeks before the message is reflected in the logs.
1195+
* This also helps guards against data loss if a crash occurs for some reason. */
1196+
if (!channel->loglastflush || (channel->loglastflush < (lognow - 300))) {
1197+
fflush(channel->fp);
1198+
channel->loglastflush = lognow;
1199+
}
11911200
}
11921201
/* It's possible to send to 0 users only if there's only one user in the channel and user is non NULL (don't echo to sender) */
11931202
if (!sent && !user) {

0 commit comments

Comments
 (0)