httpperf 1.0rc2__tar.gz → 1.0rc3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {httpperf-1.0rc2/httpperf.egg-info → httpperf-1.0rc3}/PKG-INFO +2 -2
  2. {httpperf-1.0rc2 → httpperf-1.0rc3}/README.md +1 -1
  3. {httpperf-1.0rc2 → httpperf-1.0rc3}/httpperf/__init__.py +13 -2
  4. httpperf-1.0rc3/httpperf/version.py +1 -0
  5. {httpperf-1.0rc2 → httpperf-1.0rc3/httpperf.egg-info}/PKG-INFO +2 -2
  6. {httpperf-1.0rc2 → httpperf-1.0rc3}/httpperf.egg-info/SOURCES.txt +2 -0
  7. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/httpperf.c +264 -59
  8. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_api.py +1 -1
  9. httpperf-1.0rc3/tests/test_cancel.py +138 -0
  10. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_engine.py +5 -3
  11. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_http_versions.py +75 -1
  12. httpperf-1.0rc3/tests/test_limiter.py +187 -0
  13. httpperf-1.0rc2/httpperf/version.py +0 -1
  14. {httpperf-1.0rc2 → httpperf-1.0rc3}/LICENSE +0 -0
  15. {httpperf-1.0rc2 → httpperf-1.0rc3}/MANIFEST.in +0 -0
  16. {httpperf-1.0rc2 → httpperf-1.0rc3}/NOTICE +0 -0
  17. {httpperf-1.0rc2 → httpperf-1.0rc3}/httpperf.egg-info/dependency_links.txt +0 -0
  18. {httpperf-1.0rc2 → httpperf-1.0rc3}/httpperf.egg-info/top_level.txt +0 -0
  19. {httpperf-1.0rc2 → httpperf-1.0rc3}/pyproject.toml +0 -0
  20. {httpperf-1.0rc2 → httpperf-1.0rc3}/setup.cfg +0 -0
  21. {httpperf-1.0rc2 → httpperf-1.0rc3}/setup.py +0 -0
  22. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/.clang-format +0 -0
  23. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/ae.c +0 -0
  24. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/ae.h +0 -0
  25. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/ae_epoll.c +0 -0
  26. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/ae_evport.c +0 -0
  27. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/ae_kqueue.c +0 -0
  28. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/ae_select.c +0 -0
  29. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/atomicvar.h +0 -0
  30. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/config.h +0 -0
  31. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/zmalloc.c +0 -0
  32. {httpperf-1.0rc2 → httpperf-1.0rc3}/src/ae/zmalloc.h +0 -0
  33. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_connections.py +0 -0
  34. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_cookie.py +0 -0
  35. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_httpbin.py +0 -0
  36. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_leaks.py +0 -0
  37. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_response.py +0 -0
  38. {httpperf-1.0rc2 → httpperf-1.0rc3}/tests/test_tls.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpperf
3
- Version: 1.0rc2
3
+ Version: 1.0rc3
4
4
  Summary: An asyncio HTTP client for load testing, built on libcurl.
5
5
  Author-email: Tony Simpson <agjasimpson@gmail.com>
6
6
  Project-URL: Source, https://github.com/tonysimpson/httpperf
@@ -103,7 +103,7 @@ All arguments are keyword-only. A session can be created anywhere, even outside
103
103
  | `max_host_connections` | `0` | Per-session cap on connections per host. Needs `connections="session"` or `"none"`. |
104
104
  | `max_connections` | `0` | Per-session cap on connections in use. Needs `connections="session"` or `"none"`. |
105
105
  | `cert` | `None` | `(certfile, keyfile)` paths for a client certificate, used by every request unless the request overrides it. |
106
- | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Prefer this to `asyncio.wait_for` or cancelling the task: a cancelled request still runs to the end in curl and holds its connection until then, while a timeout stops it. |
106
+ | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Cancelling the task (or `asyncio.wait_for`) also stops the transfer and frees its connection slot; on HTTP/1.1 that closes the connection, on HTTP/2 and HTTP/3 it resets only that stream. |
107
107
  | `connect_timeout` | `None` | Seconds allowed to connect, TLS included. `None` means curl's default of 300 s. |
108
108
  | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CA bundle: `SSL_CERT_FILE` if set, otherwise libcurl's own default, or the usual system locations if it has none. A path: check against that CA bundle. Requests can override it. |
109
109
  | `json_dumps` | `None` | Function that turns a `json=` argument into the request body (`str` or `bytes`). `None` means the engine's. |
@@ -77,7 +77,7 @@ All arguments are keyword-only. A session can be created anywhere, even outside
77
77
  | `max_host_connections` | `0` | Per-session cap on connections per host. Needs `connections="session"` or `"none"`. |
78
78
  | `max_connections` | `0` | Per-session cap on connections in use. Needs `connections="session"` or `"none"`. |
79
79
  | `cert` | `None` | `(certfile, keyfile)` paths for a client certificate, used by every request unless the request overrides it. |
80
- | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Prefer this to `asyncio.wait_for` or cancelling the task: a cancelled request still runs to the end in curl and holds its connection until then, while a timeout stops it. |
80
+ | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Cancelling the task (or `asyncio.wait_for`) also stops the transfer and frees its connection slot; on HTTP/1.1 that closes the connection, on HTTP/2 and HTTP/3 it resets only that stream. |
81
81
  | `connect_timeout` | `None` | Seconds allowed to connect, TLS included. `None` means curl's default of 300 s. |
82
82
  | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CA bundle: `SSL_CERT_FILE` if set, otherwise libcurl's own default, or the usual system locations if it has none. A path: check against that CA bundle. Requests can override it. |
83
83
  | `json_dumps` | `None` | Function that turns a `json=` argument into the request body (`str` or `bytes`). `None` means the engine's. |
@@ -571,6 +571,16 @@ class Response:
571
571
  return self._header
572
572
 
573
573
 
574
+ class _Future(asyncio.Future):
575
+ __slots__ = ("_session", "_handle")
576
+
577
+ def cancel(self, *args):
578
+ if not super().cancel(*args):
579
+ return False
580
+ self._session.cancel(self._handle)
581
+ return True
582
+
583
+
574
584
  class Session:
575
585
  def __init__(
576
586
  self,
@@ -747,8 +757,9 @@ class Session:
747
757
  self._session.attach(engine._ae_loop)
748
758
  if loop is not engine._loop():
749
759
  engine._bind(loop)
750
- future = loop.create_future()
751
- self._session.request(
760
+ future = _Future(loop=loop)
761
+ future._session = self._session
762
+ future._handle = self._session.request(
752
763
  future,
753
764
  method,
754
765
  url,
@@ -0,0 +1 @@
1
+ __version__ = "1.0rc3"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpperf
3
- Version: 1.0rc2
3
+ Version: 1.0rc3
4
4
  Summary: An asyncio HTTP client for load testing, built on libcurl.
5
5
  Author-email: Tony Simpson <agjasimpson@gmail.com>
6
6
  Project-URL: Source, https://github.com/tonysimpson/httpperf
@@ -103,7 +103,7 @@ All arguments are keyword-only. A session can be created anywhere, even outside
103
103
  | `max_host_connections` | `0` | Per-session cap on connections per host. Needs `connections="session"` or `"none"`. |
104
104
  | `max_connections` | `0` | Per-session cap on connections in use. Needs `connections="session"` or `"none"`. |
105
105
  | `cert` | `None` | `(certfile, keyfile)` paths for a client certificate, used by every request unless the request overrides it. |
106
- | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Prefer this to `asyncio.wait_for` or cancelling the task: a cancelled request still runs to the end in curl and holds its connection until then, while a timeout stops it. |
106
+ | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Cancelling the task (or `asyncio.wait_for`) also stops the transfer and frees its connection slot; on HTTP/1.1 that closes the connection, on HTTP/2 and HTTP/3 it resets only that stream. |
107
107
  | `connect_timeout` | `None` | Seconds allowed to connect, TLS included. `None` means curl's default of 300 s. |
108
108
  | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CA bundle: `SSL_CERT_FILE` if set, otherwise libcurl's own default, or the usual system locations if it has none. A path: check against that CA bundle. Requests can override it. |
109
109
  | `json_dumps` | `None` | Function that turns a `json=` argument into the request body (`str` or `bytes`). `None` means the engine's. |
@@ -23,11 +23,13 @@ src/ae/config.h
23
23
  src/ae/zmalloc.c
24
24
  src/ae/zmalloc.h
25
25
  tests/test_api.py
26
+ tests/test_cancel.py
26
27
  tests/test_connections.py
27
28
  tests/test_cookie.py
28
29
  tests/test_engine.py
29
30
  tests/test_http_versions.py
30
31
  tests/test_httpbin.py
31
32
  tests/test_leaks.py
33
+ tests/test_limiter.py
32
34
  tests/test_response.py
33
35
  tests/test_tls.py
@@ -76,6 +76,7 @@ typedef struct {
76
76
  struct AcRequestData *unsent_tail;
77
77
  struct AcRequestData *live_head;
78
78
  bool running;
79
+ bool closing;
79
80
  struct Share *limiter;
80
81
  CURL *free_handles[HANDLE_POOL_SIZE];
81
82
  int free_handle_count;
@@ -84,10 +85,20 @@ typedef struct {
84
85
  typedef struct HostSlot {
85
86
  char *key;
86
87
  long active;
88
+ long waiting;
87
89
  bool multiplexed;
90
+ bool ready;
91
+ struct AcRequestData *waiting_head;
92
+ struct AcRequestData *waiting_tail;
93
+ struct HostSlot *prev_ready;
94
+ struct HostSlot *next_ready;
95
+ struct HostSlot *prev;
88
96
  struct HostSlot *next;
97
+ double last_used;
89
98
  } HostSlot;
90
99
 
100
+ static double host_idle_seconds = 60;
101
+
91
102
  typedef struct Share {
92
103
  CURLSH *share;
93
104
  pthread_mutex_t locks[CURL_LOCK_DATA_LAST];
@@ -96,8 +107,9 @@ typedef struct Share {
96
107
  long max_connections;
97
108
  long connections;
98
109
  HostSlot *hosts;
99
- struct AcRequestData *waiting_head;
100
- struct AcRequestData *waiting_tail;
110
+ HostSlot *hosts_tail;
111
+ HostSlot *ready_head;
112
+ HostSlot *ready_tail;
101
113
  bool dispatching;
102
114
  } Share;
103
115
 
@@ -280,14 +292,20 @@ typedef struct AcRequestData {
280
292
  Buffer header;
281
293
  Buffer body;
282
294
  HostSlot *slot;
295
+ struct AcRequestData *prev_waiting;
283
296
  struct AcRequestData *next_waiting;
284
297
  struct AcRequestData *next_unsent;
285
298
  struct AcRequestData *live_prev;
286
299
  struct AcRequestData *live_next;
287
300
  double queued_at;
288
301
  double queue_time;
302
+ atomic_int state;
303
+ bool waiting;
304
+ bool finished;
289
305
  } AcRequestData;
290
306
 
307
+ enum { REQUEST_RUNNING, REQUEST_SENT, REQUEST_CANCELLING };
308
+
291
309
  typedef struct {
292
310
  PyObject_HEAD Buffer header;
293
311
  Buffer body;
@@ -529,25 +547,70 @@ static char *host_key(const char *url) {
529
547
  return key;
530
548
  }
531
549
 
550
+ static void host_unlink(Share *share, HostSlot *h) {
551
+ if (h->prev != NULL) {
552
+ h->prev->next = h->next;
553
+ } else {
554
+ share->hosts = h->next;
555
+ }
556
+ if (h->next != NULL) {
557
+ h->next->prev = h->prev;
558
+ } else {
559
+ share->hosts_tail = h->prev;
560
+ }
561
+ h->prev = h->next = NULL;
562
+ }
563
+
564
+ static void host_touch(Share *share, HostSlot *h) {
565
+ h->last_used = monotonic_time();
566
+ if (share->hosts != h) {
567
+ if (h->prev != NULL) {
568
+ host_unlink(share, h);
569
+ }
570
+ h->next = share->hosts;
571
+ if (share->hosts != NULL) {
572
+ share->hosts->prev = h;
573
+ } else {
574
+ share->hosts_tail = h;
575
+ }
576
+ share->hosts = h;
577
+ }
578
+ }
579
+
580
+ static void host_prune(Share *share) {
581
+ double cutoff = monotonic_time() - host_idle_seconds;
582
+ for (int i = 0; i < 2; i++) {
583
+ HostSlot *h = share->hosts_tail;
584
+ if (h == NULL || h->active > 0 || h->waiting > 0 || h->last_used > cutoff) {
585
+ return;
586
+ }
587
+ host_unlink(share, h);
588
+ free(h->key);
589
+ free(h);
590
+ }
591
+ }
592
+
532
593
  static HostSlot *host_slot(Share *share, const char *url) {
594
+ host_prune(share);
533
595
  char *key = host_key(url);
534
596
  if (key == NULL) {
535
597
  return NULL;
536
598
  }
537
- for (HostSlot *h = share->hosts; h != NULL; h = h->next) {
538
- if (strcmp(h->key, key) == 0) {
539
- free(key);
540
- return h;
541
- }
599
+ HostSlot *h = share->hosts;
600
+ while (h != NULL && strcmp(h->key, key) != 0) {
601
+ h = h->next;
542
602
  }
543
- HostSlot *h = calloc(1, sizeof(HostSlot));
544
- if (h == NULL) {
603
+ if (h != NULL) {
545
604
  free(key);
546
- return NULL;
605
+ } else {
606
+ h = calloc(1, sizeof(HostSlot));
607
+ if (h == NULL) {
608
+ free(key);
609
+ return NULL;
610
+ }
611
+ h->key = key;
547
612
  }
548
- h->key = key;
549
- h->next = share->hosts;
550
- share->hosts = h;
613
+ host_touch(share, h);
551
614
  return h;
552
615
  }
553
616
 
@@ -561,21 +624,95 @@ static bool slot_free(Share *share, HostSlot *h) {
561
624
  return !share->max_connections || share->connections < share->max_connections;
562
625
  }
563
626
 
564
- static void slot_acquire(Share *share, HostSlot *h) {
565
- if (!h->multiplexed || h->active == 0) {
566
- share->connections++;
627
+ static long host_usage(HostSlot *h) { return h->multiplexed ? h->active > 0 : h->active; }
628
+
629
+ static void host_update(Share *share, HostSlot *h, long delta, bool multiplexed) {
630
+ share->connections -= host_usage(h);
631
+ h->active += delta;
632
+ h->multiplexed = multiplexed;
633
+ share->connections += host_usage(h);
634
+ }
635
+
636
+ static PyObject *limiter_state(Share *share) {
637
+ if (share == NULL) {
638
+ Py_RETURN_NONE;
639
+ }
640
+ PyObject *hosts = PyDict_New();
641
+ if (hosts == NULL) {
642
+ return NULL;
643
+ }
644
+ for (HostSlot *h = share->hosts; h != NULL; h = h->next) {
645
+ PyObject *state = Py_BuildValue("{s:l,s:l,s:O}", "active", h->active, "waiting", h->waiting, "multiplexed",
646
+ h->multiplexed ? Py_True : Py_False);
647
+ if (state == NULL || PyDict_SetItemString(hosts, h->key, state) == -1) {
648
+ Py_XDECREF(state);
649
+ Py_DECREF(hosts);
650
+ return NULL;
651
+ }
652
+ Py_DECREF(state);
653
+ }
654
+ return Py_BuildValue("{s:l,s:N}", "connections", share->connections, "hosts", hosts);
655
+ }
656
+
657
+ static void ready_remove(Share *share, HostSlot *h) {
658
+ if (h->prev_ready != NULL) {
659
+ h->prev_ready->next_ready = h->next_ready;
660
+ } else {
661
+ share->ready_head = h->next_ready;
662
+ }
663
+ if (h->next_ready != NULL) {
664
+ h->next_ready->prev_ready = h->prev_ready;
665
+ } else {
666
+ share->ready_tail = h->prev_ready;
667
+ }
668
+ h->prev_ready = h->next_ready = NULL;
669
+ h->ready = false;
670
+ }
671
+
672
+ static void ready_append(Share *share, HostSlot *h) {
673
+ h->prev_ready = share->ready_tail;
674
+ if (share->ready_tail != NULL) {
675
+ share->ready_tail->next_ready = h;
676
+ } else {
677
+ share->ready_head = h;
678
+ }
679
+ share->ready_tail = h;
680
+ h->ready = true;
681
+ }
682
+
683
+ static void queue_push(Share *share, AcRequestData *rd) {
684
+ HostSlot *h = rd->slot;
685
+ rd->prev_waiting = h->waiting_tail;
686
+ if (h->waiting_tail != NULL) {
687
+ h->waiting_tail->next_waiting = rd;
688
+ } else {
689
+ h->waiting_head = rd;
690
+ }
691
+ h->waiting_tail = rd;
692
+ h->waiting++;
693
+ rd->waiting = true;
694
+ if (!h->ready) {
695
+ ready_append(share, h);
567
696
  }
568
- h->active++;
569
697
  }
570
698
 
571
- static void slot_release(Share *share, HostSlot *h, bool multiplexed) {
572
- if (multiplexed && !h->multiplexed) {
573
- h->multiplexed = true;
574
- share->connections -= h->active - 1;
699
+ static void queue_remove(Share *share, AcRequestData *rd) {
700
+ HostSlot *h = rd->slot;
701
+ if (rd->prev_waiting != NULL) {
702
+ rd->prev_waiting->next_waiting = rd->next_waiting;
703
+ } else {
704
+ h->waiting_head = rd->next_waiting;
575
705
  }
576
- h->active--;
577
- if (!h->multiplexed || h->active == 0) {
578
- share->connections--;
706
+ if (rd->next_waiting != NULL) {
707
+ rd->next_waiting->prev_waiting = rd->prev_waiting;
708
+ } else {
709
+ h->waiting_tail = rd->prev_waiting;
710
+ }
711
+ rd->prev_waiting = rd->next_waiting = NULL;
712
+ h->waiting--;
713
+ rd->waiting = false;
714
+ if (h->waiting_head == NULL) {
715
+ ready_remove(share, h);
579
716
  }
580
717
  }
581
718
 
@@ -585,48 +722,47 @@ static void dispatch_waiting(EventLoop *loop, Share *share) {
585
722
  }
586
723
  share_incref(share);
587
724
  share->dispatching = true;
588
- bool started = true;
589
- while (started) {
590
- started = false;
591
- AcRequestData *prev = NULL;
592
- for (AcRequestData *rd = share->waiting_head; rd != NULL; prev = rd, rd = rd->next_waiting) {
593
- if (!slot_free(share, rd->slot)) {
594
- continue;
595
- }
596
- if (prev != NULL) {
597
- prev->next_waiting = rd->next_waiting;
598
- } else {
599
- share->waiting_head = rd->next_waiting;
600
- }
601
- if (share->waiting_tail == rd) {
602
- share->waiting_tail = prev;
603
- }
604
- rd->next_waiting = NULL;
605
- rd->queue_time = monotonic_time() - rd->queued_at;
606
- slot_acquire(share, rd->slot);
607
- add_transfer(loop, rd);
608
- started = true;
609
- break;
725
+ HostSlot *h = share->ready_head;
726
+ while (h != NULL) {
727
+ if (!slot_free(share, h)) {
728
+ h = h->next_ready;
729
+ continue;
610
730
  }
731
+ AcRequestData *rd = h->waiting_head;
732
+ queue_remove(share, rd);
733
+ if (h->ready) {
734
+ ready_remove(share, h);
735
+ ready_append(share, h);
736
+ }
737
+ rd->queue_time = monotonic_time() - rd->queued_at;
738
+ host_update(share, h, 1, h->multiplexed);
739
+ add_transfer(loop, rd);
740
+ h = share->ready_head;
611
741
  }
612
742
  share->dispatching = false;
613
743
  share_decref(share);
614
744
  }
615
745
 
616
- static void complete_request(EventLoop *loop, AcRequestData *rd) {
746
+ static void release_request(EventLoop *loop, AcRequestData *rd) {
617
747
  curl_easy_setopt(rd->curl, CURLOPT_SHARE, NULL);
618
748
  curl_easy_setopt(rd->curl, CURLOPT_ERRORBUFFER, NULL);
619
749
  if (rd->slot != NULL) {
620
- long version = 0;
750
+ bool multiplexed = rd->slot->multiplexed;
621
751
  if (rd->result == CURLE_OK) {
752
+ long version = 0;
622
753
  curl_easy_getinfo(rd->curl, CURLINFO_HTTP_VERSION, &version);
754
+ multiplexed = version == CURL_HTTP_VERSION_2_0 || version == CURL_HTTP_VERSION_3;
623
755
  }
624
- slot_release(rd->limiter, rd->slot, version == CURL_HTTP_VERSION_2_0 || version == CURL_HTTP_VERSION_3);
756
+ host_update(rd->limiter, rd->slot, -1, multiplexed);
757
+ host_touch(rd->limiter, rd->slot);
625
758
  rd->slot = NULL;
626
759
  dispatch_waiting(loop, rd->limiter);
627
760
  }
628
761
  share_decref(rd->share);
629
762
  rd->share = NULL;
763
+ }
764
+
765
+ static void send_request(EventLoop *loop, AcRequestData *rd) {
630
766
  if (loop->unsent_head == NULL) {
631
767
  if (pipe_write(loop->req_out_write, &rd, sizeof(rd))) {
632
768
  return;
@@ -645,6 +781,37 @@ static void complete_request(EventLoop *loop, AcRequestData *rd) {
645
781
  loop->unsent_tail = rd;
646
782
  }
647
783
 
784
+ static void complete_request(EventLoop *loop, AcRequestData *rd) {
785
+ release_request(loop, rd);
786
+ int expected = REQUEST_RUNNING;
787
+ if (atomic_compare_exchange_strong(&rd->state, &expected, REQUEST_SENT)) {
788
+ send_request(loop, rd);
789
+ } else {
790
+ rd->finished = true;
791
+ }
792
+ }
793
+
794
+ static void cancel_request(EventLoop *loop, AcRequestData *rd) {
795
+ if (rd->finished) {
796
+ send_request(loop, rd);
797
+ return;
798
+ }
799
+ if (rd->waiting) {
800
+ queue_remove(rd->limiter, rd);
801
+ rd->slot = NULL;
802
+ } else {
803
+ check_multi(loop, curl_multi_remove_handle(loop->multi, rd->curl));
804
+ }
805
+ rd->result = CURLE_ABORTED_BY_CALLBACK;
806
+ curl_slist_free_all(rd->headers);
807
+ rd->headers = NULL;
808
+ free(rd->req_data_buf);
809
+ rd->req_data_buf = NULL;
810
+ rd->req_data_len = 0;
811
+ release_request(loop, rd);
812
+ send_request(loop, rd);
813
+ }
814
+
648
815
  static void send_completed(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask) {
649
816
  EventLoop *loop = (EventLoop *)clientData;
650
817
  while (loop->unsent_head != NULL) {
@@ -726,6 +893,10 @@ void start_request(struct aeEventLoop *eventLoop, int fd, void *clientData, int
726
893
  if (pipe_read(loop->req_in_read, &rd, sizeof(rd)) != 1) {
727
894
  return;
728
895
  }
896
+ if ((uintptr_t)rd & 1) {
897
+ cancel_request(loop, (AcRequestData *)((uintptr_t)rd & ~(uintptr_t)1));
898
+ return;
899
+ }
729
900
  REQUEST_TRACE_PRINT("start_request", rd);
730
901
  DEBUG_PRINT("read AcRequestData");
731
902
  rd->curl = loop->free_handle_count ? loop->free_handles[--loop->free_handle_count] : curl_easy_init();
@@ -819,15 +990,10 @@ void start_request(struct aeEventLoop *eventLoop, int fd, void *clientData, int
819
990
  rd->cookies_str = NULL;
820
991
  if (rd->slot != NULL && !slot_free(rd->limiter, rd->slot)) {
821
992
  rd->queued_at = monotonic_time();
822
- if (rd->limiter->waiting_tail != NULL) {
823
- rd->limiter->waiting_tail->next_waiting = rd;
824
- } else {
825
- rd->limiter->waiting_head = rd;
826
- }
827
- rd->limiter->waiting_tail = rd;
993
+ queue_push(rd->limiter, rd);
828
994
  } else {
829
995
  if (rd->slot != NULL) {
830
- slot_acquire(rd->limiter, rd->slot);
996
+ host_update(rd->limiter, rd->slot, 1, rd->slot->multiplexed);
831
997
  }
832
998
  add_transfer(loop, rd);
833
999
  }
@@ -1103,8 +1269,8 @@ static PyObject *EventLoop_abandon(EventLoop *self, PyObject *args) {
1103
1269
  self->live_head = rd->live_next;
1104
1270
  PyList_Append(futures, rd->future);
1105
1271
  Py_DECREF(rd->future);
1106
- if (rd->slot != NULL) {
1107
- rd->limiter->waiting_head = rd->limiter->waiting_tail = NULL;
1272
+ if (rd->waiting) {
1273
+ queue_remove(rd->limiter, rd);
1108
1274
  }
1109
1275
  if (rd->curl != NULL) {
1110
1276
  curl_easy_cleanup(rd->curl);
@@ -1133,7 +1299,10 @@ static PyObject *EventLoop_abandon(EventLoop *self, PyObject *args) {
1133
1299
  return futures;
1134
1300
  }
1135
1301
 
1302
+ static PyObject *EventLoop_limiter_state(EventLoop *self, PyObject *args) { return limiter_state(self->limiter); }
1303
+
1136
1304
  static PyObject *EventLoop_stop(PyObject *self, PyObject *args) {
1305
+ ((EventLoop *)self)->closing = true;
1137
1306
  if (!pipe_write(((EventLoop *)self)->stop_write, "\0", 1)) {
1138
1307
  PyErr_SetFromErrno(PyExc_OSError);
1139
1308
  return NULL;
@@ -1210,6 +1379,7 @@ static PyMethodDef EventLoop_methods[] = {
1210
1379
  {"main", (PyCFunction)EventLoop_main, METH_NOARGS, "Run the event loop"},
1211
1380
  {"abandon", (PyCFunction)EventLoop_abandon, METH_NOARGS, "Free requests in flight once stopped"},
1212
1381
  {"stop", EventLoop_stop, METH_NOARGS, "Stop the event loop"},
1382
+ {"_limiter_state", (PyCFunction)EventLoop_limiter_state, METH_NOARGS, "The limiter's counts, for tests"},
1213
1383
  {"get_out_fd", Eventloop_get_out_fd, METH_NOARGS, "Get the outbound file dscriptor"},
1214
1384
  {"get_completed", Eventloop_get_completed, METH_NOARGS, "Get the user_object, response and error"},
1215
1385
  {NULL, NULL, 0, NULL}};
@@ -1577,7 +1747,7 @@ static PyObject *Session_request(Session *self, PyObject *args, PyObject *kwds)
1577
1747
  }
1578
1748
  self->loop->live_head = rd;
1579
1749
  DEBUG_PRINT("scheduling request");
1580
- Py_RETURN_NONE;
1750
+ return PyLong_FromVoidPtr(rd);
1581
1751
 
1582
1752
  error_cleanup:
1583
1753
  if (rd->headers) {
@@ -1600,6 +1770,29 @@ error_cleanup:
1600
1770
  return NULL;
1601
1771
  }
1602
1772
 
1773
+ static PyObject *Session_limiter_state(Session *self, PyObject *args) {
1774
+ bool limited = self->shared != NULL && (self->shared->max_host_connections || self->shared->max_connections);
1775
+ return limiter_state(limited ? self->shared : NULL);
1776
+ }
1777
+
1778
+ static PyObject *Session_cancel(Session *self, PyObject *handle) {
1779
+ AcRequestData *rd = PyLong_AsVoidPtr(handle);
1780
+ if (rd == NULL) {
1781
+ return NULL;
1782
+ }
1783
+ if (self->loop == NULL || self->loop->closing) {
1784
+ Py_RETURN_NONE;
1785
+ }
1786
+ int expected = REQUEST_RUNNING;
1787
+ if (atomic_compare_exchange_strong(&rd->state, &expected, REQUEST_CANCELLING)) {
1788
+ uintptr_t message = (uintptr_t)rd | 1;
1789
+ if (!pipe_write(self->loop->req_in_write, &message, sizeof(message))) {
1790
+ return PyErr_SetFromErrno(PyExc_OSError);
1791
+ }
1792
+ }
1793
+ Py_RETURN_NONE;
1794
+ }
1795
+
1603
1796
  static PyObject *Session_close(Session *self, PyObject *args) {
1604
1797
  if (self->shared != NULL) {
1605
1798
  queue_cleanup(self->loop, NULL, self->shared);
@@ -1661,6 +1854,8 @@ static PyMethodDef Session_methods[] = {
1661
1854
  {"request", (PyCFunction)Session_request, METH_VARARGS | METH_KEYWORDS, "Send a request"},
1662
1855
  {"get_cookielist", (PyCFunction)Session_get_cookielist, METH_NOARGS, "Get the session's cookies"},
1663
1856
  {"apply_cookielist", (PyCFunction)Session_apply_cookielist, METH_VARARGS, "Apply CURLOPT_COOKIELIST lines"},
1857
+ {"cancel", (PyCFunction)Session_cancel, METH_O, "Stop a request in flight"},
1858
+ {"_limiter_state", (PyCFunction)Session_limiter_state, METH_NOARGS, "The limiter's counts, for tests"},
1664
1859
  {"close", (PyCFunction)Session_close, METH_NOARGS, "Close the session's connections"},
1665
1860
  {"attach", (PyCFunction)Session_attach, METH_VARARGS, "Attach the session to an EventLoop"},
1666
1861
  {NULL, NULL, 0, NULL}};
@@ -1726,7 +1921,17 @@ static PyObject *module_default_ca_file(PyObject *self, PyObject *args) {
1726
1921
  #endif
1727
1922
  }
1728
1923
 
1924
+ static PyObject *module_set_host_idle_seconds(PyObject *self, PyObject *arg) {
1925
+ double seconds = PyFloat_AsDouble(arg);
1926
+ if (seconds == -1 && PyErr_Occurred()) {
1927
+ return NULL;
1928
+ }
1929
+ host_idle_seconds = seconds;
1930
+ Py_RETURN_NONE;
1931
+ }
1932
+
1729
1933
  static PyMethodDef module_methods[] = {
1934
+ {"_set_host_idle_seconds", module_set_host_idle_seconds, METH_O, "How long idle host records are kept, for tests"},
1730
1935
  {"default_ca_file", module_default_ca_file, METH_NOARGS, "libcurl's built-in CA bundle path, or None"},
1731
1936
  {NULL, NULL, 0, NULL}};
1732
1937
 
@@ -306,7 +306,7 @@ def test_response_properties(httpbin):
306
306
  ]
307
307
  assert times == sorted(times)
308
308
  assert r.appconnect_time == 0
309
- assert r.posttransfer_time >= 0
309
+ assert r.posttransfer_time is None or r.posttransfer_time >= 0
310
310
  assert r.upload_size == 0
311
311
  assert r.download_size == len(r.body)
312
312
  assert r.primary_ip == "127.0.0.1"
@@ -0,0 +1,138 @@
1
+ import asyncio
2
+ import random
3
+
4
+ import pytest
5
+
6
+ import httpperf
7
+
8
+
9
+ def run(main):
10
+ errors = []
11
+
12
+ async def wrapper():
13
+ asyncio.get_running_loop().set_exception_handler(lambda loop, context: errors.append(context))
14
+ return await main()
15
+
16
+ result = asyncio.run(wrapper())
17
+ assert errors == []
18
+ return result
19
+
20
+
21
+ def test_cancel_stops_the_transfer(server):
22
+ async def main():
23
+ async with httpperf.Session(http_version="HTTP/1.1") as session:
24
+ task = asyncio.create_task(session.get(server.url + "/slow"))
25
+ await asyncio.sleep(0.2)
26
+ task.cancel()
27
+ with pytest.raises(asyncio.CancelledError):
28
+ await task
29
+ return await asyncio.get_running_loop().run_in_executor(None, server.wait_for, lambda: server.hung_up == 1)
30
+
31
+ assert run(main)
32
+
33
+
34
+ def test_cancel_frees_the_connection_slot(server):
35
+ async def main():
36
+ async with httpperf.Session(http_version="HTTP/1.1", connections="session", max_host_connections=1) as session:
37
+ slow = asyncio.create_task(session.get(server.url + "/slow"))
38
+ await asyncio.sleep(0.2)
39
+ slow.cancel()
40
+ r = await asyncio.wait_for(session.get(server.url + "/fast"), 2)
41
+ return r.status_code
42
+
43
+ assert run(main) == 200
44
+
45
+
46
+ def test_cancel_a_queued_request(server):
47
+ async def main():
48
+ async with httpperf.Session(http_version="HTTP/1.1", connections="session", max_host_connections=1) as session:
49
+ slow = asyncio.create_task(session.get(server.url + "/slow"))
50
+ await asyncio.sleep(0.2)
51
+ queued = asyncio.create_task(session.get(server.url + "/queued"))
52
+ after = asyncio.create_task(session.get(server.url + "/after"))
53
+ await asyncio.sleep(0.2)
54
+ queued.cancel()
55
+ await asyncio.sleep(0.1)
56
+ server.release.set()
57
+ results = [(await slow).status_code, (await after).status_code]
58
+ return results, queued.cancelled()
59
+
60
+ assert run(main) == ([200, 200], True)
61
+ assert server.started == ["/slow", "/after"]
62
+
63
+
64
+ def test_cancel_before_the_engine_starts_it(server):
65
+ async def main():
66
+ async with httpperf.Session(http_version="HTTP/1.1") as session:
67
+ task = asyncio.create_task(session.get(server.url + "/slow"))
68
+ await asyncio.sleep(0)
69
+ task.cancel()
70
+ with pytest.raises(asyncio.CancelledError):
71
+ await task
72
+ await asyncio.sleep(0.2)
73
+ r = await session.get(server.url + "/fast")
74
+ return r.status_code
75
+
76
+ assert run(main) == 200
77
+ assert "/slow" not in server.started
78
+
79
+
80
+ def test_wait_for_stops_the_transfer(server):
81
+ async def main():
82
+ async with httpperf.Session(http_version="HTTP/1.1") as session:
83
+ with pytest.raises(asyncio.TimeoutError):
84
+ await asyncio.wait_for(session.get(server.url + "/slow"), 0.2)
85
+ return await asyncio.get_running_loop().run_in_executor(None, server.wait_for, lambda: server.hung_up == 1)
86
+
87
+ assert run(main)
88
+
89
+
90
+ def test_cancel_many(server):
91
+ async def main():
92
+ async with httpperf.Session(http_version="HTTP/1.1") as session:
93
+ tasks = [asyncio.create_task(session.get(server.url + "/slow")) for _ in range(100)]
94
+ await asyncio.sleep(0.5)
95
+ for task in tasks:
96
+ task.cancel()
97
+ await asyncio.gather(*tasks, return_exceptions=True)
98
+ r = await session.get(server.url + "/fast")
99
+ return r.status_code, all(t.cancelled() for t in tasks)
100
+
101
+ assert run(main) == (200, True)
102
+
103
+
104
+ def test_cancel_races_completion(server):
105
+ rng = random.Random(1)
106
+
107
+ async def main():
108
+ ok = 0
109
+ async with httpperf.Session(http_version="HTTP/1.1") as session:
110
+ for _ in range(30):
111
+ tasks = [asyncio.create_task(session.get(server.url + "/fast")) for _ in range(50)]
112
+ cancelled = set(rng.sample(range(50), 25))
113
+ await asyncio.sleep(rng.random() * 0.005)
114
+ for i in cancelled:
115
+ tasks[i].cancel()
116
+ results = await asyncio.wait_for(asyncio.gather(*tasks, return_exceptions=True), 5)
117
+ for i, result in enumerate(results):
118
+ if i not in cancelled:
119
+ assert result.status_code == 200
120
+ ok += 1
121
+ return ok
122
+
123
+ assert run(main) == 30 * 25
124
+
125
+
126
+ def test_cancel_after_the_engine_closed(server):
127
+ async def main():
128
+ engine = httpperf.Engine()
129
+ async with httpperf.Session(engine=engine, http_version="HTTP/1.1") as session:
130
+ task = asyncio.create_task(session.get(server.url + "/slow"))
131
+ await asyncio.sleep(0.2)
132
+ with pytest.warns(ResourceWarning):
133
+ engine.close()
134
+ task.cancel()
135
+ with pytest.raises((asyncio.CancelledError, httpperf.RequestError)):
136
+ await task
137
+
138
+ run(main)
@@ -8,6 +8,7 @@ import subprocess
8
8
  import sys
9
9
  import threading
10
10
  import time
11
+ import warnings
11
12
 
12
13
  import pytest
13
14
 
@@ -238,15 +239,16 @@ def test_close_with_requests_in_flight_warns(silent_url):
238
239
  asyncio.run(main())
239
240
 
240
241
 
241
- def test_dropped_loop_with_requests_in_flight_frees_them(silent_url):
242
+ def test_dropped_loop_after_cancelling_does_not_warn(silent_url):
242
243
  gc.collect()
243
244
  before = wait_for_engine_threads(engine_threads())
244
245
  loop = asyncio.new_event_loop()
245
246
  task = loop.create_task(httpperf.Session().get(silent_url))
246
247
  loop.run_until_complete(asyncio.sleep(0.1))
247
248
  task.cancel()
248
- loop.run_until_complete(asyncio.sleep(0))
249
- with pytest.warns(ResourceWarning, match="1 requests in flight"):
249
+ loop.run_until_complete(asyncio.sleep(0.1))
250
+ with warnings.catch_warnings():
251
+ warnings.simplefilter("error")
250
252
  loop.close()
251
253
  del task, loop
252
254
  gc.collect()
@@ -15,7 +15,7 @@ import httpperf
15
15
  async def app(scope, receive, send):
16
16
  if scope["type"] != "http":
17
17
  return
18
- await asyncio.sleep(0.05)
18
+ await asyncio.sleep(1 if scope["path"] == "/slow" else 0.05)
19
19
  await send({"type": "http.response.start", "status": 200, "headers": [(b"content-type", b"text/plain")]})
20
20
  await send({"type": "http.response.body", "body": scope["http_version"].encode()})
21
21
 
@@ -125,3 +125,77 @@ def test_session_host_limit_with_multiplexing(h2_url):
125
125
  assert sum(r.connect_time > 0 for r in first + second) == 1
126
126
  assert sum(r.queue_time > 0 for r in first) == 4
127
127
  assert all(r.queue_time == 0 for r in second)
128
+
129
+
130
+ @pytest.mark.parametrize("version", [pytest.param("HTTP/3", marks=needs_http3), "HTTP/2"])
131
+ def test_cancel_one_stream_keeps_the_connection(h2_url, version):
132
+ async def main(el):
133
+ s = httpperf.Session(engine=el, connections="session", http_version=version)
134
+ tasks = [asyncio.ensure_future(s.get(h2_url + "slow")) for _ in range(6)]
135
+ await asyncio.sleep(0.3)
136
+ for task in tasks[::2]:
137
+ task.cancel()
138
+ kept = await asyncio.gather(*tasks[1::2])
139
+ after = await s.get(h2_url)
140
+ el.close()
141
+ return kept + [after], [t.cancelled() for t in tasks[::2]]
142
+
143
+ responses, cancelled = run(main)
144
+ assert cancelled == [True] * 3
145
+ assert [r.status_code for r in responses] == [200] * 4
146
+ assert sum(r.connect_time > 0 for r in responses) == 0
147
+
148
+
149
+ def limiter_host(state):
150
+ (host,) = state["hosts"].values()
151
+ usage = (1 if host["active"] else 0) if host["multiplexed"] else host["active"]
152
+ assert state["connections"] == usage
153
+ return host
154
+
155
+
156
+ @pytest.mark.parametrize("version", [pytest.param("HTTP/3", marks=needs_http3), "HTTP/2"])
157
+ def test_limiter_counts_a_multiplexed_host_once(h2_url, version):
158
+ async def main(el):
159
+ s = httpperf.Session(engine=el, connections="session", http_version=version, max_host_connections=2)
160
+ await s.get(h2_url)
161
+ tasks = [asyncio.ensure_future(s.get(h2_url + "slow")) for _ in range(6)]
162
+ await asyncio.sleep(0.3)
163
+ busy = limiter_host(s._session._limiter_state())
164
+ await asyncio.gather(*tasks)
165
+ idle = limiter_host(s._session._limiter_state())
166
+ el.close()
167
+ return busy, idle
168
+
169
+ busy, idle = run(main)
170
+ assert busy == {"active": 6, "waiting": 0, "multiplexed": True}
171
+ assert idle == {"active": 0, "waiting": 0, "multiplexed": True}
172
+
173
+
174
+ def test_limiter_keeps_multiplexing_after_a_failure(h2_url):
175
+ async def main(el):
176
+ s = httpperf.Session(engine=el, connections="session", max_host_connections=2)
177
+ await s.get(h2_url)
178
+ with pytest.raises(httpperf.Timeout):
179
+ await s.get(h2_url + "slow", timeout=0.1)
180
+ state = s._session._limiter_state()
181
+ s.close()
182
+ el.close()
183
+ return state
184
+
185
+ assert limiter_host(run(main))["multiplexed"] is True
186
+
187
+
188
+ def test_limiter_follows_a_host_back_to_http1(h2_url):
189
+ async def main(el):
190
+ engine = httpperf.Engine(max_host_connections=4)
191
+ h2 = httpperf.Session(engine=engine)
192
+ h1 = httpperf.Session(engine=engine, http_version="HTTP/1.1")
193
+ await h2.get(h2_url)
194
+ before = limiter_host(engine._ae_loop._limiter_state())
195
+ await h1.get(h2_url)
196
+ after = limiter_host(engine._ae_loop._limiter_state())
197
+ engine.close()
198
+ el.close()
199
+ return before["multiplexed"], after["multiplexed"]
200
+
201
+ assert run(main) == (True, False)
@@ -0,0 +1,187 @@
1
+ import asyncio
2
+ import socket
3
+
4
+ import pytest
5
+
6
+ import httpperf
7
+ from httpperf import _httpperf
8
+
9
+
10
+ def usage(host):
11
+ if host["multiplexed"]:
12
+ return 1 if host["active"] else 0
13
+ return host["active"]
14
+
15
+
16
+ def check(state):
17
+ assert state["connections"] == sum(usage(h) for h in state["hosts"].values())
18
+ return state
19
+
20
+
21
+ def key(url):
22
+ return url if url.count(":") == 2 else url + ":80"
23
+
24
+
25
+ async def wait_until(get_state, condition, timeout=5):
26
+ deadline = asyncio.get_running_loop().time() + timeout
27
+ while True:
28
+ state = check(get_state())
29
+ if condition(state):
30
+ return state
31
+ assert asyncio.get_running_loop().time() < deadline, state
32
+ await asyncio.sleep(0.01)
33
+
34
+
35
+ def test_host_limit_queues_and_drains(server):
36
+ async def main():
37
+ async with httpperf.Session(http_version="HTTP/1.1", connections="session", max_host_connections=2) as s:
38
+ state = s._session._limiter_state
39
+ tasks = [asyncio.create_task(s.get(server.url + "/slow")) for _ in range(5)]
40
+ busy = await wait_until(state, lambda st: st["hosts"] and st["hosts"][key(server.url)]["waiting"] == 3)
41
+ server.release.set()
42
+ responses = await asyncio.gather(*tasks)
43
+ return busy, check(state()), responses
44
+
45
+ busy, idle, responses = asyncio.run(main())
46
+ assert busy["connections"] == 2 and busy["hosts"][key(server.url)]["active"] == 2
47
+ assert idle["connections"] == 0
48
+ assert idle["hosts"][key(server.url)] == {"active": 0, "waiting": 0, "multiplexed": False}
49
+ assert sorted(r.queue_time > 0 for r in responses) == [False, False, True, True, True]
50
+
51
+
52
+ def test_total_limit_across_hosts(make_server):
53
+ a, b = make_server(), make_server()
54
+
55
+ async def main():
56
+ async with httpperf.Engine(max_total_connections=3) as engine:
57
+ state = engine._ae_loop._limiter_state
58
+ async with httpperf.Session(engine=engine, http_version="HTTP/1.1") as s:
59
+ first = [asyncio.create_task(s.get(a.url + "/slow")) for _ in range(3)]
60
+ await wait_until(state, lambda st: st["connections"] == 3)
61
+ second = [asyncio.create_task(s.get(b.url + "/fast")) for _ in range(2)]
62
+ busy = await wait_until(
63
+ state, lambda st: key(b.url) in st["hosts"] and st["hosts"][key(b.url)]["waiting"] == 2
64
+ )
65
+ a.release.set()
66
+ await asyncio.gather(*first, *second)
67
+ return busy, check(state())
68
+
69
+ busy, idle = asyncio.run(main())
70
+ assert busy["connections"] == 3
71
+ assert busy["hosts"][key(a.url)]["active"] == 3
72
+ assert idle["connections"] == 0
73
+ assert all(h["active"] == h["waiting"] == 0 for h in idle["hosts"].values())
74
+
75
+
76
+ def test_failed_requests_release_their_slots():
77
+ async def main():
78
+ async with httpperf.Session(connections="session", max_host_connections=1) as s:
79
+ results = await asyncio.gather(*(s.get("http://127.0.0.1:9/") for _ in range(5)), return_exceptions=True)
80
+ return results, check(s._session._limiter_state())
81
+
82
+ results, state = asyncio.run(main())
83
+ assert all(isinstance(r, httpperf.RequestError) for r in results)
84
+ assert state["connections"] == 0
85
+ assert state["hosts"]["http://127.0.0.1:9"] == {"active": 0, "waiting": 0, "multiplexed": False}
86
+
87
+
88
+ def test_cancelled_requests_release_their_slots(server):
89
+ async def main():
90
+ async with httpperf.Session(http_version="HTTP/1.1", connections="session", max_host_connections=2) as s:
91
+ state = s._session._limiter_state
92
+ tasks = [asyncio.create_task(s.get(server.url + "/slow")) for _ in range(6)]
93
+ await wait_until(state, lambda st: st["hosts"] and st["hosts"][key(server.url)]["waiting"] == 4)
94
+ for task in tasks[::2]:
95
+ task.cancel()
96
+ await wait_until(state, lambda st: st["hosts"][key(server.url)]["waiting"] == 1)
97
+ server.release.set()
98
+ await asyncio.gather(*tasks, return_exceptions=True)
99
+ return check(state()), [t.cancelled() for t in tasks]
100
+
101
+ state, cancelled = asyncio.run(main())
102
+ assert state["connections"] == 0
103
+ assert state["hosts"][key(server.url)] == {"active": 0, "waiting": 0, "multiplexed": False}
104
+ assert cancelled == [True, False] * 3
105
+
106
+
107
+ def test_no_limiter_without_limits():
108
+ assert httpperf.Session()._session._limiter_state() is None
109
+ assert httpperf.Engine()._ae_loop._limiter_state() is None
110
+
111
+
112
+ def test_hosts_take_turns_under_the_total_limit(server):
113
+ other = server.url.replace("127.0.0.1", "localhost")
114
+
115
+ async def main():
116
+ async with httpperf.Engine(max_total_connections=1) as engine:
117
+ state = engine._ae_loop._limiter_state
118
+ async with httpperf.Session(engine=engine, http_version="HTTP/1.1") as s:
119
+ first = asyncio.create_task(s.get(server.url + "/slow"))
120
+ await wait_until(state, lambda st: st["connections"] == 1)
121
+ tasks = [asyncio.create_task(s.get(server.url + "/a%d" % i)) for i in range(3)]
122
+ await wait_until(state, lambda st: st["hosts"][key(server.url)]["waiting"] == 3)
123
+ tasks += [asyncio.create_task(s.get(other + "/b%d" % i)) for i in range(3)]
124
+ await wait_until(
125
+ state, lambda st: key(other) in st["hosts"] and st["hosts"][key(other)]["waiting"] == 3
126
+ )
127
+ server.release.set()
128
+ await asyncio.gather(first, *tasks)
129
+
130
+ asyncio.run(main())
131
+ assert server.started == ["/slow", "/a0", "/b0", "/a1", "/b1", "/a2", "/b2"]
132
+
133
+
134
+ @pytest.fixture
135
+ def short_host_idle():
136
+ _httpperf._set_host_idle_seconds(0.1)
137
+ yield
138
+ _httpperf._set_host_idle_seconds(60)
139
+
140
+
141
+ def refused_urls(n):
142
+ sockets = [socket.socket() for _ in range(n)]
143
+ for sock in sockets:
144
+ sock.bind(("127.0.0.1", 0))
145
+ urls = ["http://127.0.0.1:%d/" % sock.getsockname()[1] for sock in sockets]
146
+ for sock in sockets:
147
+ sock.close()
148
+ return urls
149
+
150
+
151
+ def test_idle_host_records_are_freed(short_host_idle):
152
+ async def main():
153
+ async with httpperf.Session(connections="session", max_host_connections=1) as s:
154
+ state = s._session._limiter_state
155
+ for url in refused_urls(100):
156
+ with pytest.raises(httpperf.RequestError):
157
+ await s.get(url)
158
+ before = len(check(state())["hosts"])
159
+ await asyncio.sleep(0.2)
160
+ (url,) = refused_urls(1)
161
+ for _ in range(60):
162
+ with pytest.raises(httpperf.RequestError):
163
+ await s.get(url)
164
+ return before, check(state())["hosts"]
165
+
166
+ before, after = asyncio.run(main())
167
+ assert before == 100
168
+ assert len(after) == 1
169
+
170
+
171
+ def test_busy_host_records_are_kept(server, short_host_idle):
172
+ async def main():
173
+ async with httpperf.Session(http_version="HTTP/1.1", connections="session", max_host_connections=1) as s:
174
+ state = s._session._limiter_state
175
+ slow = asyncio.create_task(s.get(server.url + "/slow"))
176
+ queued = asyncio.create_task(s.get(server.url + "/slow"))
177
+ await wait_until(state, lambda st: st["hosts"] and st["hosts"][key(server.url)]["waiting"] == 1)
178
+ await asyncio.sleep(0.2)
179
+ for url in refused_urls(10):
180
+ with pytest.raises(httpperf.RequestError):
181
+ await s.get(url)
182
+ busy = check(state())["hosts"][key(server.url)]
183
+ server.release.set()
184
+ await asyncio.gather(slow, queued)
185
+ return busy
186
+
187
+ assert asyncio.run(main()) == {"active": 1, "waiting": 1, "multiplexed": False}
@@ -1 +0,0 @@
1
- __version__ = "1.0rc2"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes