async-curl 0.9.0

An asynchronous implementation to perform curl operations with tokio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::fmt::Debug;
use std::time::Duration;

use async_trait::async_trait;
use curl::easy::{Easy2, Handler};
use curl::multi::Multi;
use log::trace;
use tokio::runtime::{Builder, Runtime};
use tokio::sync::mpsc::{self, Receiver, Sender};
use tokio::sync::oneshot;
use tokio::task::LocalSet;

use crate::error::Error;

#[async_trait]
pub trait Actor<H>
where
    H: Handler + Debug + Send + 'static,
{
    async fn send_request(&self, easy2: Easy2<H>) -> Result<Easy2<H>, Error<H>>;
}

/// CurlActor is responsible for performing
/// the contructed Easy2 object at the background
/// to perform it asynchronously.
/// ```
/// use async_curl::actor::{Actor, CurlActor};
/// use curl::easy::{Easy2, Handler, WriteError};
///
/// #[derive(Debug, Clone, Default)]
/// pub struct ResponseHandler {
///     data: Vec<u8>,
/// }
///
/// impl Handler for ResponseHandler {
///     /// This will store the response from the server
///     /// to the data vector.
///     fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
///         self.data.extend_from_slice(data);
///         Ok(data.len())
///     }
/// }
///
/// impl ResponseHandler {
///     /// Instantiation of the ResponseHandler
///     /// and initialize the data vector.
///     pub fn new() -> Self {
///         Self::default()
///     }
///
///     /// This will consumed the object and
///     /// give the data to the caller
///     pub fn get_data(self) -> Vec<u8> {
///         self.data
///     }
/// }
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>>{
/// let curl = CurlActor::new();
/// let mut easy2 = Easy2::new(ResponseHandler::new());
///
/// easy2.url("https://www.rust-lang.org").unwrap();
/// easy2.get(true).unwrap();
///
/// let response = curl.send_request(easy2).await.unwrap();
/// eprintln!("{:?}", response.get_ref());
///
/// Ok(())
/// # }
/// ```
///
/// Example for multiple request executed
/// at the same time.
///
/// ```
/// use async_curl::actor::{Actor, CurlActor};
/// use curl::easy::{Easy2, Handler, WriteError};
///
/// #[derive(Debug, Clone, Default)]
/// pub struct ResponseHandler {
///     data: Vec<u8>,
/// }
///
/// impl Handler for ResponseHandler {
///     /// This will store the response from the server
///     /// to the data vector.
///     fn write(&mut self, data: &[u8]) -> Result<usize, WriteError> {
///         self.data.extend_from_slice(data);
///         Ok(data.len())
///     }
/// }
///
/// impl ResponseHandler {
///     /// Instantiation of the ResponseHandler
///     /// and initialize the data vector.
///     pub fn new() -> Self {
///         Self::default()
///     }
///
///     /// This will consumed the object and
///     /// give the data to the caller
///     pub fn get_data(self) -> Vec<u8> {
///         self.data
///     }
/// }
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let actor = CurlActor::new();
/// let mut easy2 = Easy2::new(ResponseHandler::new());
/// easy2.url("https://www.rust-lang.org").unwrap();
/// easy2.get(true).unwrap();
///
/// let actor1 = actor.clone();
/// let spawn1 = tokio::spawn(async move {
///     let response = actor1.send_request(easy2).await;
///     let mut response = response.unwrap();
///
///     // Response body
///     eprintln!(
///         "Task 1 : {}",
///         String::from_utf8_lossy(&response.get_ref().to_owned().get_data())
///     );
///     // Response status code
///     let status_code = response.response_code().unwrap();
///     eprintln!("Task 1 : {}", status_code);
/// });
///
/// let mut easy2 = Easy2::new(ResponseHandler::new());
/// easy2.url("https://www.rust-lang.org").unwrap();
/// easy2.get(true).unwrap();
///
/// let spawn2 = tokio::spawn(async move {
///     let response = actor.send_request(easy2).await;
///     let mut response = response.unwrap();
///
///     // Response body
///     eprintln!(
///         "Task 2 : {}",
///         String::from_utf8_lossy(&response.get_ref().to_owned().get_data())
///     );
///     // Response status code
///     let status_code = response.response_code().unwrap();
///     eprintln!("Task 2 : {}", status_code);
/// });
/// let (_, _) = tokio::join!(spawn1, spawn2);
///
/// Ok(())
/// # }
/// ```
///
use std::sync::Arc;
use std::thread::JoinHandle;

/// This contains the Easy2 object and a oneshot sender channel when passing into the
/// background task to perform Curl asynchronously.
#[derive(Debug)]
pub struct Request<H: Handler + Debug + Send + 'static>(
    Easy2<H>,
    oneshot::Sender<Result<Easy2<H>, Error<H>>>,
    TransferType,
);

/// This enum is used to differentiate between the two types of transfers: Multi and Easy2.
#[derive(Debug, Clone)]
enum TransferType {
    Multi,
    Easy2,
}

struct Inner<H>
where
    H: Handler + Debug + Send + 'static,
{
    request_sender: Option<Sender<Request<H>>>,
    join_handle: Option<JoinHandle<()>>,
}

impl<H> Drop for Inner<H>
where
    H: Handler + Debug + Send + 'static,
{
    fn drop(&mut self) {
        // Take and drop the sender so the background actor sees channel closed.
        if let Some(sender) = self.request_sender.take() {
            trace!("Dropping request sender to signal background actor to shut down.");
            drop(sender);
            trace!("Request sender dropped, signaling background actor to shut down.");
        }
        // Join the background thread to ensure graceful shutdown.
        if let Some(handle) = self.join_handle.take() {
            trace!("Attempting to join background actor thread for graceful shutdown...");
            let _ = handle.join();
            trace!("Background actor thread joined successfully.");
        }
    }
}

#[derive(Clone)]
pub struct CurlActor<H>
where
    H: Handler + Debug + Send + 'static,
{
    inner: Arc<Inner<H>>,
    transfer_type: TransferType,
}

impl<H> Default for CurlActor<H>
where
    H: Handler + Debug + Send + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<H> Actor<H> for CurlActor<H>
where
    H: Handler + Debug + Send + 'static,
{
    /// This will send Easy2 into the background task that will perform
    /// curl asynchronously, await the response in the oneshot receiver and
    /// return Easy2 back to the caller.
    async fn send_request(&self, easy2: Easy2<H>) -> Result<Easy2<H>, Error<H>> {
        let (oneshot_sender, oneshot_receiver) = oneshot::channel::<Result<Easy2<H>, Error<H>>>();
        self.inner
            .request_sender
            .as_ref()
            .expect("request_sender missing")
            .send(Request(easy2, oneshot_sender, self.transfer_type.clone()))
            .await?;
        oneshot_receiver.await?
    }
}

impl<H> CurlActor<H>
where
    H: Handler + Debug + Send + 'static,
{
    /// This creates the new instance of CurlActor to handle Curl perform asynchronously using Curl Multi
    /// in a background thread to avoid blocking of other tasks.
    pub fn new() -> Self {
        let runtime = Builder::new_current_thread().enable_all().build().unwrap();
        let (request_sender, request_receiver) = mpsc::channel::<Request<H>>(1);

        let handle = Self::spawn_actor(runtime, request_receiver);

        Self {
            inner: Arc::new(Inner {
                request_sender: Some(request_sender),
                join_handle: Some(handle),
            }),
            transfer_type: TransferType::Easy2,
        }
    }

    /// This creates the new instance of CurlActor to handle Curl perform asynchronously using Curl Multi
    /// in a background thread to avoid blocking of other tasks. The user can provide a custom runtime
    /// to use for the background task.
    pub fn new_runtime(runtime: Runtime) -> Self {
        let (request_sender, request_receiver) = mpsc::channel::<Request<H>>(1);

        let handle = Self::spawn_actor(runtime, request_receiver);

        Self {
            inner: Arc::new(Inner {
                request_sender: Some(request_sender),
                join_handle: Some(handle),
            }),
            transfer_type: TransferType::Easy2,
        }
    }

    /// Create a new CurlActor with a user-provided runtime and configurable channel capacity.
    pub fn new_runtime_with_capacity(runtime: Runtime, capacity: usize) -> Self {
        let (request_sender, request_receiver) = mpsc::channel::<Request<H>>(capacity);

        let handle = Self::spawn_actor(runtime, request_receiver);

        Self {
            inner: Arc::new(Inner {
                request_sender: Some(request_sender),
                join_handle: Some(handle),
            }),
            transfer_type: TransferType::Easy2,
        }
    }

    fn spawn_actor(runtime: Runtime, mut request_receiver: Receiver<Request<H>>) -> JoinHandle<()> {
        std::thread::spawn(move || {
            let local = LocalSet::new();
            local.spawn_local(async move {
                while let Some(Request(easy2, oneshot_sender, transfer_type)) =
                    request_receiver.recv().await
                {
                    tokio::task::spawn_local(async move {
                        let response = match transfer_type {
                            TransferType::Easy2 => perform_curl_easy2(easy2).await,
                            TransferType::Multi => perform_curl_multi(easy2).await,
                        };
                        if let Err(res) = oneshot_sender.send(response) {
                            trace!("Warning! The receiver has been dropped. {:?}", res);
                        }
                    });
                }
            });
            runtime.block_on(local);
        })
    }

    /// This method allows the user to switch the transfer type to Curl Multi for the CurlActor.
    pub fn use_multi_transfer(self) -> Self {
        Self {
            inner: self.inner,
            transfer_type: TransferType::Multi,
        }
    }

    /// This method allows the user to switch the transfer type to Curl Easy2 for the CurlActor.
    pub fn use_easy2_transfer(self) -> Self {
        Self {
            inner: self.inner,
            transfer_type: TransferType::Easy2,
        }
    }
}

async fn perform_curl_multi<H: Handler + Debug + Send + 'static>(
    easy2: Easy2<H>,
) -> Result<Easy2<H>, Error<H>> {
    trace!("perform_curl_multi: starting curl multi operation");
    tokio::task::spawn_blocking(move || -> Result<Easy2<H>, Error<H>> {
        let multi = Multi::new();
        let handle = multi.add2(easy2).map_err(|e| Error::Multi(e))?;

        while multi.perform().map_err(|e| Error::Multi(e))? != 0 {
            let timeout_result = multi
                .get_timeout()
                .map(|d| d.unwrap_or_else(|| Duration::from_secs(2)));

            let timeout = match timeout_result {
                Ok(duration) => duration,
                Err(multi_error) => {
                    if !multi_error.is_call_perform() {
                        return Err(Error::Multi(multi_error));
                    }
                    Duration::ZERO
                }
            };

            if !timeout.is_zero() {
                trace!(
                    "perform_curl_multi: waiting for IO or timeout {:?}",
                    timeout
                );
                let ready = multi.wait(&mut [], timeout).map_err(Error::Multi)?;
                trace!(
                    "perform_curl_multi: wait completed, {} sockets ready",
                    ready
                );
            }
        }

        // Inspect messages for transfer-level errors.
        let mut transfer_error: Option<Error<H>> = None;
        multi.messages(|msg| {
            if let Some(Err(e)) = msg.result() {
                transfer_error = Some(Error::Curl(e));
            }
        });

        // Always attempt to remove the handle to clean up resources. If there was
        // a transfer error prefer returning that error, but still try to perform
        // the removal and log any cleanup failure.
        let cleanup = multi.remove2(handle).map_err(|e| Error::Multi(e));

        if let Some(e) = transfer_error {
            if let Err(ref clean_err) = cleanup {
                trace!(
                    "perform_curl_multi: remove2 failed during cleanup: {:?}",
                    clean_err
                );
            }
            Err(e)
        } else {
            cleanup
        }
    })
    .await
    .map_err(Error::JoinError)?
}

async fn perform_curl_easy2<H: Handler + Debug + Send + 'static>(
    easy2: Easy2<H>,
) -> Result<Easy2<H>, Error<H>> {
    trace!("perform_curl_easy2: starting curl easy2 operation");
    tokio::task::spawn_blocking(move || -> Result<Easy2<H>, Error<H>> {
        easy2.perform().map_err(|e| Error::Curl(e))?;
        Ok(easy2)
    })
    .await
    .map_err(Error::JoinError)?
}