You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide explains how to use use `Async::HTTP::Faraday` as a drop-in replacement for improved concurrency.
4
+
5
+
## Installation
6
+
7
+
Add the gem to your project:
8
+
9
+
~~~bash
10
+
$ bundle add async-http-faraday
11
+
~~~
12
+
13
+
## Usage
14
+
15
+
The simplest way to use `Async::HTTP::Faraday` is to set it as the default adapter for Faraday. This will make all requests asynchronous.
16
+
17
+
~~~ruby
18
+
require'async/http/faraday/default'
19
+
~~~
20
+
21
+
This will configure `Faraday.default_adapter`.
22
+
23
+
### Custom Connection
24
+
25
+
You can configure a custom connection to use the async adapter:
26
+
27
+
```ruby
28
+
# Per connection:
29
+
connection =Faraday.new(...) do |builder|
30
+
builder.adapter :async_http
31
+
end
32
+
```
33
+
34
+
Here is how you make a request:
35
+
36
+
```ruby
37
+
response = connection.get("/index")
38
+
```
39
+
40
+
### Thread Safety
41
+
42
+
By default, the faraday adapter uses a per-thread persistent client cache. This is safe to use in multi-threaded environments, in other words, if you have a single global faraday connection, and use that everywhere, it will be thread-safe. However, a consequence of that is you may experience elevated memory usage if you have many threads, as each thread will have its own connection pool. This is a desirable share-nothing architecture which helps to isolate problems, but if you don't use a multi-threaded environment, you may want to avoid the overhead. You can do this by configuring the `clients` option:
43
+
44
+
~~~ruby
45
+
connection =Faraday.new(...) do |builder|
46
+
# The default `clients:` is `Async::HTTP::Faraday::PerThreadPersistentClients`.
The value of isolation cannot be overstated - if you can design you program using a share-nothing (between threads) architecture, you will have a much easier time debugging and reasoning about your program, however this comes at the cost of increased resource usage.
52
+
53
+
Alternatively, if you do not want to cache client connections, you can use the `Async::HTTP::Faraday::Clients` interface, which closes the connection after each request:
Copy file name to clipboardExpand all lines: readme.md
+6-45Lines changed: 6 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -1,56 +1,17 @@
1
1
# Async::HTTP::Faraday
2
2
3
-
Provides an adaptor for [Faraday](https://github.com/lostisland/faraday) to perform async HTTP requests. If you are designing a new library, you should probably just use `Async::HTTP::Client` directly.
3
+
Provides an adaptor for [Faraday](https://github.com/lostisland/faraday) to perform async HTTP requests. If you are designing a new library, you should probably just use `Async::HTTP::Client` directly. However, for existing projects and libraries that use Faraday as an abstract interface, this can be a drop-in replacement to improve concurrency. It should be noted that the default `Net::HTTP` adapter works perfectly okay with Async, however it does not use persistent connections by default.
Please see the [project documentation](https://socketry.github.io/async-http/) for more details.
50
13
51
-
```ruby
52
-
require'async/http/faraday/default'
53
-
```
14
+
-[Getting Started](https://socketry.github.io/async-http/guides/getting-started/index) - This guide explains how to use use `Async::HTTP::Faraday` as a drop-in replacement for improved concurrency.
0 commit comments