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

在运行ASP.NET Core应用时遇到报错:configured user limit (128) on the number of inotify instances已达到。问题源于频繁读取JSON文件时创建了过多的文件监视器。解决方案包括修改文件监视器的配置或使用依赖注入抽象处理方式。
部署运行你感兴趣的模型镜像

部署一个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

您可能感兴趣的与本文相关的镜像

HunyuanVideo-Foley

HunyuanVideo-Foley

语音合成

HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值