简体   繁体   English

如何修复应用服务中的 SNAT 耗尽

[英]How do I fix SNAT Exhaustion in App Service

I'm having a problem with SNAT exhaustion in one of our Azure App Service based APIs:我在我们的一个基于 Azure 应用服务的 API 中遇到了 SNAT 耗尽问题:

SNAT

Our HTTPClient is written into a singleton that should instance only once (C#/.net 4.72)...我们的 HTTPClient 被写入一个 singleton 应该只实例一次(C#/.net 4.72)...

    public class CSClient : HttpClient
    {
        private static readonly CSClient Inst = new CSClient();

        static CSClient()
        {
        }

        private CSClient() : base()
        {
            Timeout = TimeSpan.FromSeconds(60);
            BaseAddress = new Uri(ConfigurationManager.AppSettings["***.BaseURL"]);
        }

        public static HttpClient Instance
        {
            get
            {
                return Inst;
            }
        }
    }

Then called然后叫

public class ContentRepository : IContentRepository
    {
        protected HttpClient htc = CSClient.Instance;

        public async Task<Content> GetContentAsync(Content ct)
        {
            using (var req = new HttpRequestMessage(HttpMethod.Get, ConfigurationManager.AppSettings["***.BaseUrl"] + "/api/v2/nodes/" + ct.Id))
            {
                var response = await htc.SendAsync(req);

                if (response.IsSuccessStatusCode)
                {
                    var job = JObject.Parse(await response.Content.ReadAsStringAsync());
                    var respct = OTtoContent(job["results"]);
                    return respct;
                }
                else
                {
                    return null;
                }
            }
        }
    }

I'm not sure why the extra connections are being made.我不确定为什么要建立额外的连接。 Is my singleton correct?我的 singleton 是否正确? Anything else I can do with the httpclient?我还能用 httpclient 做什么? Anything to do with the app service, short of adding resources?与应用服务有什么关系,除了添加资源吗? Thanks for any help in advance.感谢您提前提供任何帮助。

This is potential solution: SNAT with App Service这是潜在的解决方案: SNAT with App Service

HttpClient allow multiple requests in parallel to the same endpoint(by default). HttpClient 允许多个请求并行到同一个端点(默认情况下)。

The following methods are thread safe : 以下方法是线程安全的

  1. CancelPendingRequests CancelPendingRequests
  2. DeleteAsync删除异步
  3. List item项目清单
  4. GetAsync获取异步
  5. GetByteArrayAsync GetByteArrayAsync
  6. GetStreamAsync GetStreamAsync
  7. GetStringAsync获取字符串异步
  8. PostAsync后异步
  9. PutAsync PutAsync
  10. SendAsync发送异步

So one instance of HttpClient can make several connections at a time via SendAsync .因此,一个HttpClient实例可以通过SendAsync一次建立多个连接。 You can try to control number of connections via ServicePointManager or HttpClientHandler.MaxConnectionsPerServer (on Core ) property.您可以尝试通过ServicePointManagerHttpClientHandler.MaxConnectionsPerServer (在Core )属性来控制连接数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM