.net core 报错 the configured user limit (128) on the number of inotify instances has been reached

xiaoxiao2021-02-28  75

部署一个asp.net core程序,用着用着占用线程数越来越多,看报错日志发现这么一行:the configured user limit (128) on the number of inotify instances has been reached。

谷歌查到Stackoverflow上也有人遇到这个问题,原来是读取json文件造成的。

var builder = new ConfigurationBuilder() .AddJsonFile($"appsettings.json", true, true) .AddJsonFile($"appsettings.{environmentName}.json", true, true) .AddEnvironmentVariables(); var configuration = builder.Build();

原因是:You are creating file watchers, every time you access an setting. The 3rd parameter is reloadOnChange.

大意是创建了一个文件监视器,由于第三个参数设为true,导致每次请求都要新开一个线程来读取文件。解决办法是将第三个参数设置为false,不过貌似不是最理想的解决办法,最好的办法人家给出了:

Best way is to abstract hat behind an interface and use dependency injection.

public interface IConfigurationManager { T GetAppConfig<T>(string key, T defaultValue = default(T)); } public class ConfigurationManager : IConfigurationManager { private readonly IConfigurationRoot config; public ConfigurationManager(IConfigurationRoot config) => this.config ?? throw new ArgumentNullException(nameof(config)); public T GetAppConfig<T>(string key, T defaultValue = default(T)) { T setting = (T)Convert.ChangeType(configuration[key], typeof(T)); value = setting; if (setting == null) value = defaultValue; } }

Then instantiate and register it

services.AddSingleton<IConfigurationManager>(new ConfigurationManager(this.Configuration)); and inject it into your services via constructor
转载请注明原文地址: https://www.6miu.com/read-2624929.html

最新回复(0)