question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpClient is created for each request which is anti-pattern

See original GitHub issue

Description

This line means that each property access creates on new HttpClient https://github.com/fsprojects/FSharp.Data.GraphQL/blob/0f34f8fda9387f1fbba2294d8ff5c2f4e01f90bc/src/FSharp.Data.GraphQL.Client/GraphQLProviderRuntimeContext.fs#L20 Hence Dispose is called on a new instance. And creating HttpClient for each request is an anti-pattern.

Expected behavior

  1. HttpClient is created once.
  2. User can provide his instance of HttpClient

Actual behavior

HttpClient is hardcoded and created on each property access.

Related information

  • FSharp.Data.GraphQL 1.0.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
iveltencommented, Apr 21, 2020

I was searching a bit about HttpClient, more specifically about a Singleton/Caching usage of it, and I came to this thread. I guess we could avoid implementing a default instance using those approaches, as we could potentially run into DNS changes depending on the user. Instead, I just removed the HttpClient re-instantiation on each property access, and allowed the user to manage its own connection instance by passing a factory to an overload of GetContext method.

Here is the PR with my proposal.

1reaction
xperiandricommented, Apr 3, 2020

I hope to present a fix for tomorrow

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improper Instantiation antipattern - Azure Architecture Center
An antipattern is a common response to a recurring problem that is usually ... A new HttpClient object is created for each user...
Read more >
HttpClient Best Practices and Anti-Patterns in C#.NET
It's recommended to create a single static HttpClient instance and use the same shared instance for all requests. HttpClient is designed to be...
Read more >
Is it cheap to create brand new HttpClient per request
A new HttpClient is created each time, but HttpClient is cheap to create. The internal HTTP handler that the HttpClient uses to make...
Read more >
Should we create a new single instance of HttpClient for all ...
This post states: An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every  ......
Read more >
You're using HttpClient wrong and it is destabilizing your ...
This means that under the covers it is reentrant and thread safe. Instead of creating a new instance of HttpClient for each execution...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found