(
config: &HttpConfig,
shared_agent: Option<&ureq::Agent>,
work: HttpWork,
)
| 433 | .name(format!("perro-http-{worker_index}")) |
| 434 | .spawn(move || http_worker(worker_config, worker_rx, worker_event_tx, worker_agent)) |
| 435 | .expect("failed to spawn HTTP worker"); |
| 436 | } |
| 437 | drop(event_tx); |
| 438 | |
| 439 | Self { |
| 440 | config, |
| 441 | queue_config, |
| 442 | next_id: 0, |
| 443 | tx: work_tx, |
| 444 | rx: event_rx, |
| 445 | local_events: VecDeque::new(), |
| 446 | rejected_requests: 0, |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | pub fn request(&mut self, request: HttpRequest) -> HttpID { |
| 451 | match self.try_request(request) { |
| 452 | Ok(id) => id, |
| 453 | Err(error) => { |
| 454 | let id = error.id; |
| 455 | self.local_events |
| 456 | .push_back(HttpEvent::Failed(error.into_http_error())); |
| 457 | id |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | pub fn try_request(&mut self, request: HttpRequest) -> Result<HttpID, HttpSubmitError> { |
| 463 | let id = self.next_http_id(); |
| 464 | let url = request.url.clone(); |
| 465 | if url.is_empty() { |
| 466 | self.rejected_requests = self.rejected_requests.saturating_add(1); |
| 467 | return Err(HttpSubmitError::new( |
| 468 | id, |
| 469 | url, |
| 470 | HttpSubmitErrorKind::InvalidRequest, |
| 471 | "empty url", |
| 472 | )); |
| 473 | } |
| 474 | match self.tx.try_send(HttpWork { id, request }) { |
| 475 | Ok(()) => Ok(id), |
| 476 | Err(TrySendError::Full(work)) => { |
| 477 | self.rejected_requests = self.rejected_requests.saturating_add(1); |
| 478 | Err(HttpSubmitError::new( |
| 479 | id, |
| 480 | work.request.url, |
| 481 | HttpSubmitErrorKind::QueueFull, |
| 482 | "http request queue full", |
| 483 | )) |
| 484 | } |
| 485 | Err(TrySendError::Disconnected(work)) => { |
| 486 | self.rejected_requests = self.rejected_requests.saturating_add(1); |
| 487 | Err(HttpSubmitError::new( |
| 488 | id, |
no test coverage detected