Not_Sure
Not_Sure

Reputation: 87

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:

SNAT

Our HTTPClient is written into a singleton that should instance only once (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? Anything else I can do with the httpclient? Anything to do with the app service, short of adding resources? Thanks for any help in advance.

Upvotes: 2

Views: 1569

Answers (2)

heavenwing
heavenwing

Reputation: 598

This is potential solution: SNAT with App Service

Upvotes: 1

Guru Stron
Guru Stron

Reputation: 142008

HttpClient allow multiple requests in parallel to the same endpoint(by default).

The following methods are thread safe:

  1. CancelPendingRequests
  2. DeleteAsync
  3. List item
  4. GetAsync
  5. GetByteArrayAsync
  6. GetStreamAsync
  7. GetStringAsync
  8. PostAsync
  9. PutAsync
  10. SendAsync

So one instance of HttpClient can make several connections at a time via SendAsync. You can try to control number of connections via ServicePointManager or HttpClientHandler.MaxConnectionsPerServer(on Core) property.

Upvotes: 0

Related Questions