Search code examples
c#nestdotnet-httpclient

Can an HttpClient Get request be executed without reading the response and taking memory


I am using NEST to make a Search call, and everything is working well but I want to do some testing on a non-primary cluster so that I can test if it has enough power to handle our production load alongside our current primary production elasticsearch cluster.

I have code that sends all of the indexing and searching calls to the non-primary cluster on a background thread, but I would also like to avoid the memory consumption of the returned elasticsearch results from my "GET"/Searches.

Is this possible to do? I currently am using the discard paradigm to just throw them away and they will get cleaned up quickly but is there anything better than I can do?

Task.Run(() => 
{
    NonPrimarySearchClustersList.ForEach(currentSearcher => {
            //throwaway but want to test doing the search on elastic non-primary
            _ = currentSearcher.Search(searchParams, size);
        });
});

Solution

  • If you don't care about the result you can specify this in two different ways.

    1. You can ask the HttpClient to fetch only the headers by:
    await client.GetAsync("your_url", HttpCompletionOption.ResponseHeadersRead);
    
    1. You can directly issue a HEAD request:
    await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, "your_url"));
    

    the latter works only if the server is configured to support the HEAD verb