Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing same http client instance #340

Merged
merged 6 commits into from
Sep 5, 2022

Conversation

DeepanshuA
Copy link
Contributor

Signed-off-by: Deepanshu Agarwal deepanshu.agarwal1984@gmail.com

Description

Change to reuse same HTTPClient instance for multiple invocations.
Basically, HTTPClient is acting as a Singleton here.

Tested in stand-alone mode with Actors. I tried to invoke 100000 actors.

Before: It led to multiple http client instances getting formed and this each time a new http agent being formed.
It resulted in "Temporary error when accepting new connections: accept tcp [::]:3500: accept4: too many open files"

After: 100000 actor invocations got successful, without the above issue.

Some potential discussions:

  1. Though, DaprClient is not re-using http.Agent when using static getInstance #320 talks specifically about http. Do we also need to cater grpc in same way?
  2. HTTPClient is being exported and the contract may change - as it makes HTTPClient constructor as private but rather requires getInstance() static method to be called. I thought of using if/else inside constructor but: a) It looked like a bad design b) With new HTTPClient, a user expects new instance rather than, same instance being returned behind-the-scenes. c) It also led to couple of errors with other variables (their formation being conditional)
    We can think of letting HTTPClient constructor to remain non-private but it can lead to leakage.

Issue reference

Please reference the issue this PR will close: #320

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@DeepanshuA DeepanshuA requested review from a team as code owners August 21, 2022 11:47
@DeepanshuA DeepanshuA changed the title Reusing same http client instance [WIP]Reusing same http client instance Aug 21, 2022
@XavierGeerinck
Copy link
Contributor

Amazing PR! 🤩 Could you fix the linking issues so the test pass? Then I can merge

@DeepanshuA
Copy link
Contributor Author

Amazing PR! 🤩 Could you fix the linking issues so the test pass? Then I can merge

Thanks! Sure, will get them sorted.

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@codecov
Copy link

codecov bot commented Aug 22, 2022

Codecov Report

Merging #340 (7f8562f) into main (c3f6347) will increase coverage by 11.74%.
The diff coverage is 66.66%.

@@             Coverage Diff             @@
##             main     #340       +/-   ##
===========================================
+ Coverage   25.63%   37.38%   +11.74%     
===========================================
  Files          78       78               
  Lines        6194     6364      +170     
  Branches      205      265       +60     
===========================================
+ Hits         1588     2379      +791     
+ Misses       4602     3943      -659     
- Partials        4       42       +38     
Impacted Files Coverage Δ
src/implementation/Client/HTTPClient/HTTPClient.ts 28.40% <66.66%> (+19.10%) ⬆️
src/implementation/Client/GRPCClient/state.ts 5.00% <0.00%> (+1.00%) ⬆️
.../implementation/Client/GRPCClient/configuration.ts 7.27% <0.00%> (+1.81%) ⬆️
src/proto/dapr/proto/runtime/v1/dapr_pb.js 30.68% <0.00%> (+2.57%) ⬆️
src/implementation/Client/GRPCClient/lock.ts 16.00% <0.00%> (+4.00%) ⬆️
src/utils/Settings.util.ts 70.83% <0.00%> (+4.16%) ⬆️
src/implementation/Client/GRPCClient/metadata.ts 16.66% <0.00%> (+4.16%) ⬆️
src/implementation/Client/GRPCClient/secret.ts 13.63% <0.00%> (+4.54%) ⬆️
src/proto/dapr/proto/runtime/v1/dapr_grpc_pb.js 10.85% <0.00%> (+6.85%) ⬆️
src/implementation/Client/HTTPClient/state.ts 15.38% <0.00%> (+7.69%) ⬆️
... and 40 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@XavierGeerinck
Copy link
Contributor

LGTM @shubham1172 additional review possible?

@XavierGeerinck
Copy link
Contributor

Can we also add gRPC in this scope? It will require the same

src/implementation/Client/HTTPClient/HTTPClient.ts Outdated Show resolved Hide resolved
@@ -41,7 +41,7 @@ export default class ActorClient {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the gRPC client re-use the same instance internally? If not, can we cache it the same way as HTTP?

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@amulyavarote
Copy link
Contributor

Changes look good to me! Thanks @DeepanshuA

amulyavarote
amulyavarote previously approved these changes Aug 29, 2022
@DeepanshuA
Copy link
Contributor Author

Can we also add gRPC in this scope? It will require the same

As discussed offline: It doesn't look quite straight-forward to cache grpc client. Instead, we plan to cache DaprClient and current PR would address only http client. I have created a separate issue #349 to track the discussed proposal.

@DeepanshuA DeepanshuA changed the title [WIP]Reusing same http client instance Reusing same http client instance Sep 1, 2022
shubham1172
shubham1172 previously approved these changes Sep 2, 2022
Copy link
Member

@shubham1172 shubham1172 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a minor comment left.

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
@DeepanshuA DeepanshuA temporarily deployed to production September 2, 2022 07:14 Inactive
shubham1172
shubham1172 previously approved these changes Sep 2, 2022
const keepAlive = this.options.isKeepAlive;
const keepAliveMsecs = 30 * 1000; // it is applicable only when keepAlive is set to true

if(!HTTPClient.httpAgent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve correct indentation please, seems to be 4 spaces instead of 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad - corrected now.

Copy link
Contributor

@XavierGeerinck XavierGeerinck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit change regarding identation

Signed-off-by: Deepanshu Agarwal <deepanshu.agarwal1984@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DaprClient is not re-using http.Agent when using static getInstance
4 participants