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

View File

@ -28,11 +28,8 @@ namespace NadekoBot.Modules.Permissions
static CmdCdsCommands()
{
using (var uow = DbHandler.UnitOfWork())
{
var configs = NadekoBot.AllGuildConfigs;
commandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet<CommandCooldown>(v.CommandCooldowns)));
}
var configs = NadekoBot.AllGuildConfigs;
commandCooldowns = new ConcurrentDictionary<ulong, ConcurrentHashSet<CommandCooldown>>(configs.ToDictionary(k => k.GuildId, v => new ConcurrentHashSet<CommandCooldown>(v.CommandCooldowns)));
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]