Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-5239

THttpTransport should support passing in an HttpClient

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.14.0
    • Component/s: netstd - Library
    • Labels:
      None

      Description

      This issue is very similar to the Java one documented here: https://issues.apache.org/jira/browse/THRIFT-970

      .NET Core supports using an IHttpClientFactory to request instances of HttpClient. This factory will pool clients to avoid exhausting sockets in the OS. Microsoft currently do not recommend creating a new HttpClient per request, consumers currently have to find a way to pool Thrift clients in order to conform to this practice.

      The line which instantiates a new HttpClient can be found here.

      This proposal would require a new constructor which allowed an instance of HttpClient to be passed in. The consumer could use this constructor like this:

      var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
      var httpClient = httpClientFactory.CreateClient("thrift");
      var transport = new THttpTransport(httpClient);
      

      After registering their own named IHttpClientFactory in the ServiceCollection:

      var certificates = GetCertificates();
      
      services.AddHttpClient("thrift", c =>
      {
          c.BaseAddress = new Uri("https://foo.bar/Service.thrift");
      
          c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-thrift"));
          c.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
          c.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
      
          c.DefaultRequestHeaders.Add("CUSTOM-HEADER", "HEADER-VALUE");
      })
        .ConfigurePrimaryHttpMessageHandler(() =>
        {
            var handler = new HttpClientHandler()
            {
                AutomaticDecompression = System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.GZip
            };
            handler.ClientCertificates.AddRange(certificates);
            return handler;
        });
      

      Rather than relying on users to properly configure request headers and decompression methods themselves, an extension method to ServiceCollection could be defined:

      services.AddThriftHttpClient("thrift", uri, customRequestHeaders, userAgent, certificates);
      

      This would more closely match the current constructor of THttpTransport.

      We've forked the THttpTransport file to support this behaviour for internal purposes, so I could put up a sample PR if requested.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                lewisjackson Lewis Jackson
                Reporter:
                lewisjackson Lewis Jackson
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: