Huge startup time increase on large bots

This commit is contained in:
Kwoth 2016-12-30 05:46:13 +01:00
parent 5fc6c789da
commit 7ea45a16b4
2 changed files with 11 additions and 15 deletions

View File

@ -24,9 +24,8 @@ namespace NadekoBot.Modules.Games
public string Status { get; set; } public string Status { get; set; }
public string Response { get; set; } public string Response { get; set; }
} }
//user#discrim is the key
public static ConcurrentHashSet<string> ChannelsInConversation { get; } = new ConcurrentHashSet<string>(); public static ConcurrentDictionary<ulong, Lazy<ChatterBotSession>> CleverbotGuilds { get; } = new ConcurrentDictionary<ulong, Lazy<ChatterBotSession>>();
public static ConcurrentDictionary<ulong, ChatterBotSession> CleverbotGuilds { get; } = new ConcurrentDictionary<ulong, ChatterBotSession>();
static CleverBotCommands() static CleverBotCommands()
{ {
@ -36,10 +35,10 @@ namespace NadekoBot.Modules.Games
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
var bot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT); var bot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT);
CleverbotGuilds = new ConcurrentDictionary<ulong, ChatterBotSession>( CleverbotGuilds = new ConcurrentDictionary<ulong, Lazy<ChatterBotSession>>(
NadekoBot.AllGuildConfigs NadekoBot.AllGuildConfigs
.Where(gc => gc.CleverbotEnabled) .Where(gc => gc.CleverbotEnabled)
.ToDictionary(gc => gc.GuildId, gc => bot.CreateSession())); .ToDictionary(gc => gc.GuildId, gc => new Lazy<ChatterBotSession>(() => bot.CreateSession(), true)));
} }
sw.Stop(); sw.Stop();
@ -52,7 +51,7 @@ namespace NadekoBot.Modules.Games
if (channel == null) if (channel == null)
return false; return false;
ChatterBotSession cleverbot; Lazy<ChatterBotSession> cleverbot;
if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out cleverbot)) if (!CleverbotGuilds.TryGetValue(channel.Guild.Id, out cleverbot))
return false; return false;
@ -75,7 +74,7 @@ namespace NadekoBot.Modules.Games
await msg.Channel.TriggerTypingAsync().ConfigureAwait(false); await msg.Channel.TriggerTypingAsync().ConfigureAwait(false);
var response = await cleverbot.Think(message).ConfigureAwait(false); var response = await cleverbot.Value.Think(message).ConfigureAwait(false);
try try
{ {
await msg.Channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false); await msg.Channel.SendConfirmAsync(response.SanitizeMentions()).ConfigureAwait(false);
@ -94,7 +93,7 @@ namespace NadekoBot.Modules.Games
{ {
var channel = (ITextChannel)imsg.Channel; var channel = (ITextChannel)imsg.Channel;
ChatterBotSession throwaway; Lazy<ChatterBotSession> throwaway;
if (CleverbotGuilds.TryRemove(channel.Guild.Id, out throwaway)) if (CleverbotGuilds.TryRemove(channel.Guild.Id, out throwaway))
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
@ -109,7 +108,7 @@ namespace NadekoBot.Modules.Games
var cleverbot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT); var cleverbot = ChatterBotFactory.Create(ChatterBotType.CLEVERBOT);
var session = cleverbot.CreateSession(); var session = cleverbot.CreateSession();
CleverbotGuilds.TryAdd(channel.Guild.Id, session); CleverbotGuilds.TryAdd(channel.Guild.Id, new Lazy<ChatterBotSession>(() => session, true));
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {

View File

@ -27,13 +27,10 @@ namespace NadekoBot.Modules.Permissions
private static ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> activeCooldowns { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>>(); private static ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>> activeCooldowns { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<ActiveCooldown>>();
static CmdCdsCommands() static CmdCdsCommands()
{
using (var uow = DbHandler.UnitOfWork())
{ {
var configs = NadekoBot.AllGuildConfigs; var configs = NadekoBot.AllGuildConfigs;
commandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet<CommandCooldown>(v.CommandCooldowns))); commandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet<CommandCooldown>(v.CommandCooldowns)));
} }
}
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task CmdCooldown(IUserMessage imsg, Command command, int secs) public async Task CmdCooldown(IUserMessage imsg, Command command, int secs)