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

Mint adapter #297

Merged
merged 16 commits into from
Aug 19, 2019
Merged

Mint adapter #297

merged 16 commits into from
Aug 19, 2019

Conversation

RyanSiu1995
Copy link
Contributor

Regarding issue #282,
sorry for misopen of PR without verifying the build last time.
Several problems for mint integration

  1. Mint is using two layers of keyword list for the connection opts. Also, SSL and TCP opts shares in the same opt list and the options from SSL is not compatible to TCP. It will make badarg if the user uses SSL options in TCP. So I do not use config :tesla, Mint, xxxxx to let user to have a global adapter configuration for Mint.
  2. Ca cert for SSL in the unit test need to be imported from HTTPParrot so I have exposed the cacert in config :tesla, :cacert, [xxxx] for passing the unit test. I don't like the hacky way I do in the pr. Any idea on making this much more precise and concise?
  3. The request body streaming in mint is not really good at all. It requires the user to specify the correct content length in order to pass all the unit tests. So I have made a function to get the full body before starting the request so I can always get the content length.
    Welcome to have any kinds of comments. Thank you!!!!

@codecov
Copy link

codecov bot commented May 11, 2019

Codecov Report

Merging #297 into master will increase coverage by 0.08%.
The diff coverage is 96.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #297      +/-   ##
==========================================
+ Coverage   95.43%   95.51%   +0.08%     
==========================================
  Files          23       24       +1     
  Lines         460      513      +53     
==========================================
+ Hits          439      490      +51     
- Misses         21       23       +2
Impacted Files Coverage Δ
lib/tesla/adapter/mint.ex 96.22% <96.22%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 40dc5ee...f57df1a. Read the comment docs.

@codecov
Copy link

codecov bot commented May 11, 2019

Codecov Report

Merging #297 into master will decrease coverage by 0.31%.
The diff coverage is 92.59%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #297      +/-   ##
==========================================
- Coverage   95.43%   95.11%   -0.32%     
==========================================
  Files          23       24       +1     
  Lines         460      512      +52     
==========================================
+ Hits          439      487      +48     
- Misses         21       25       +4
Impacted Files Coverage Δ
lib/tesla/adapter/mint.ex 92.59% <92.59%> (ø)
lib/tesla/middleware/decode_rels.ex 100% <0%> (ø) ⬆️
lib/tesla/builder.ex 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 40dc5ee...85f94c6. Read the comment docs.

@teamon teamon requested a review from amatalai May 31, 2019 10:38
lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved
lib/tesla/adapter/mint.ex Show resolved Hide resolved
mix.exs Outdated Show resolved Hide resolved
@teamon
Copy link
Member

teamon commented May 31, 2019

Wow! 👏

@ericmj would you mind taking a look at mint usage?

@amatalai
Copy link
Collaborator

If I have not overlooked anything, this implementation will create a new connection for every request and there will be no way to reuse it. The question is how fast will it work in comparison to other adapters.

@teamon
Copy link
Member

teamon commented May 31, 2019

I know, but given #301 I think this is a good starting point.

Copy link
Member

@teamon teamon left a comment

Choose a reason for hiding this comment

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

Could you explain why do we need this get_default_ca at all? Is this for production use or for tests only? You explained that already, 🤦‍♂

Let me see if I can find any other non-hackish way to do this

@@ -19,5 +19,5 @@ if Mix.env() == :test do

config :tesla, MockClient, adapter: Tesla.Mock

config :tesla, cacert: ["./deps/httparrot/priv/ssl/server-ca.crt"]
config :tesla, Mint, cacert: ["./deps/httparrot/priv/ssl/server-ca.crt"]
Copy link
Member

Choose a reason for hiding this comment

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

I'd use

config :tesla, Tesla.Adapter.Mint, cacert: ["./deps/httparrot/priv/ssl/server-ca.crt"]

Copy link

@ericmj ericmj left a comment

Choose a reason for hiding this comment

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

There are performance downsides to this adapter. It starts a new connection for each request, unlike with for example hackney that uses a pool connection. Because of this, clients that use pools will likely be faster.

Since the connection is never reused you may want to set the connection: close header and explicitly close the connection. Otherwise you are likely to leak messages since you may still be getting messages from the socket after the request is finished.

lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved
lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved
lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved
lib/tesla/adapter/mint.ex Show resolved Hide resolved
lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved

defp get_default_ca() do
env = Application.get_env(:tesla, Mint)
Keyword.get(env, :cacert)
Copy link

Choose a reason for hiding this comment

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

Not sure how this works, but make sure the CA store Mint uses is not removed if a user does not explicitly the :cacert config.

Copy link
Member

Choose a reason for hiding this comment

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

There should be either some error reporting or some default, this will blow if Application.get_env(:tesla, Tesla.Adapter.Mint) returns nil

lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved
@ericmj
Copy link

ericmj commented Jun 4, 2019

The request body streaming in mint is not really good at all. It requires the user to specify the correct content length in order to pass all the unit tests.

I have opened an issue to support chunked encoding for the request: elixir-mint/mint#174.

@RyanSiu1995
Copy link
Contributor Author

Thank you @ericmj and @teamon !
I have updated the code according to the comment.
For the cacert, I make it as a global configuration for Mint Adapter. It will serve as a global default cacert file for the adapter. Meaning every single call by that client will use that cacert.
For the request pooling, I will continue the development on issue #301.
Anyway, thank you very much!

@codecov-io
Copy link

codecov-io commented Jun 4, 2019

Codecov Report

Merging #297 into master will increase coverage by 0.09%.
The diff coverage is 96.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #297      +/-   ##
==========================================
+ Coverage   95.19%   95.28%   +0.09%     
==========================================
  Files          25       26       +1     
  Lines         520      573      +53     
==========================================
+ Hits          495      546      +51     
- Misses         25       27       +2
Impacted Files Coverage Δ
lib/tesla/adapter/mint.ex 96.22% <96.22%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9795075...b1e5c11. Read the comment docs.

@RyanSiu1995
Copy link
Contributor Author

And for the checks, I don't know how to fix the codecov. Some of the paths cannot be simply tested by HTTParrot.

@RyanSiu1995
Copy link
Contributor Author

@teamon @ericmj can you please help review the latest commit <3 Thank you!!

@@ -18,4 +18,7 @@ if Mix.env() == :test do
sasl_error_logger: false

config :tesla, MockClient, adapter: Tesla.Mock

config :tesla, Tesla.Adapter.Mint,
cacert: ["./deps/httparrot/priv/ssl/server-ca.crt"]
Copy link
Member

Choose a reason for hiding this comment

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

@ericmj This config won't end up in production builds, right? I mean, it will be nil by default so it should be safe to reference httparrot here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it will be nil by default.

Copy link

Choose a reason for hiding this comment

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

Correct, config.exs is not used from dependencies. So the Tesla.Adapter.Mint key will not be set.


defp get_default_ca() do
env = Application.get_env(:tesla, Mint)
Keyword.get(env, :cacert)
Copy link
Member

Choose a reason for hiding this comment

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

There should be either some error reporting or some default, this will blow if Application.get_env(:tesla, Tesla.Adapter.Mint) returns nil

lib/tesla/adapter/mint.ex Outdated Show resolved Hide resolved
@RyanSiu1995
Copy link
Contributor Author

I have done a simple research on the build issue.
Seem OTP 18.3 on xenial is no longer supported by travis ci.
So it got a failure while setting up the environment for opt_release: 18.3

Ref: https://travis-ci.community/t/erlang-18-3-couldnt-be-pulled/3836

@teamon
Copy link
Member

teamon commented Jul 22, 2019

@RyanSiu1995 please rebase on recent master that contains fixes for otp 18

@RyanSiu1995 RyanSiu1995 reopened this Aug 17, 2019
@teamon teamon merged commit 755708c into elixir-tesla:master Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants