httpx

package
v0.1.73 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 6 Imported by: 14

README

ListenAndServe in the Go http package is impossible to use safely in an asynchronous way. What we frequently want is to start the http server and have it run, asynchronously, in perpetuity, but not have the call return until the server is able to receive requests.

httpx provides ListenAndServeAsync, which starts an http server in a background goroutine, but does not return until the listening socket is established. Therefore, once this function returns, the server can have an HTTP GET run against it and the GET should succeed (or at least call the appropriate server code).

Documentation

Overview

Package httpx provides generic functions which extend the capabilities of the http package.

The code here eliminates an annoying race condition in net/http that prevents you from knowing when it is safe to connect to the server socket. For the functions in this package, the listening socket is fully estabished when the function returns, and it is safe to run an HTTP GET immediately.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServeAsync

func ListenAndServeAsync(server *http.Server) error

ListenAndServeAsync starts an http server. The server will run until Shutdown() or Close() is called, but this function will return once the listening socket is established. This means that when this function returns, the server is immediately available for an http GET to be run against it.

Returns a non-nil error if the listening socket can't be established. Logs a fatal error if the server dies for a reason besides ErrServerClosed. If the server.Addr is set to :0, then after this function returns server.Addr will contain the address and port which this server is listening on.

func ListenAndServeTLSAsync

func ListenAndServeTLSAsync(server *http.Server, certFile, keyFile string) error

ListenAndServeTLSAsync starts an https server. The server will run until Shutdown() or Close() is called, but this function will return once the listening socket is established. This means that when this function returns, the server is immediately available for an https GET to be run against it.

Returns a non-nil error if the listening socket can't be established. Logs a fatal error if the server dies for a reason besides ErrServerClosed.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL