make-http-connection and connection reuse

35 views
Skip to first unread message

Alex Harsányi

unread,
May 2, 2021, 7:13:56 PM5/2/21
to Racket Users
The documentation for make-http-connection indicates that the same connection can be reused repeatedly, unfortunately, this does not seem to work.


Here is an example program:

#lang racket
(require net/url net/url-connect net/head)

(define (tile->osm-url zoom x y)
  (string->url (format "https://tile.openstreetmap.org/~a/~a/~a.png" zoom x y)))

(define user-agent "racket")

(define (run-test download-count)
  (parameterize ((current-https-protocol 'secure))
    (let ((connection (make-http-connection)))
      (for ([try (in-range 1 download-count)])

        (printf "download ~a..." url)(flush-output)
        (define-values (port headers)
          (get-pure-port/headers
           (tile->osm-url 14 try 1)
           (list user-agent)
           #:connection connection))

        (define content-type (extract-field "Content-Type" headers))
        (define data (port->bytes port))
        (unless (equal? content-type "image/png")
          (printf "expected a PNG image~%"))
        (printf " done.~%")(flush-output)
        ;; discard the data and start a new loop
        )
      (http-connection-close connection))))

(run-test 1000)

If you run the above program and monitor the racket process (I used procexp.exe on Windows, but you can use lsof on Linux), you'll notice that each request creates a separate socket connection, rather than reusing it, and these connections hang around for a long time before being closed.

Is this a bug in make-http-connection and/or get-pure-port/headers, or am I using it incorrectly in the example above?

This was noticed by someone on my map-widget package, and switching to `http-easy` solved the immediate problem (thanks Bogdan for that package!), but I thought I will ask here anyway, in case this is a bug.

Thanks,
Alex.


Reply all
Reply to author
Forward
0 new messages