Skip to content

Instantly share code, notes, and snippets.

@kevin-cantwell
Created February 1, 2018 18:08
Show Gist options
  • Save kevin-cantwell/3e0fdd9d22ee31258878fb6c82d3aee4 to your computer and use it in GitHub Desktop.
Save kevin-cantwell/3e0fdd9d22ee31258878fb6c82d3aee4 to your computer and use it in GitHub Desktop.
PTTH Client
package ptth
import (
"errors"
"net"
"net/http"
"golang.org/x/net/http2"
)
// DialAndServe establishes a tcp connection to addr and
// serves incoming HTTP/2 requests. Only a single connection
// is used to multiplex requests. An error will be returned
// if any of the following occur: 1) A tcp connection cannot
// be established to the remote host; 2) The connection is
// closed by the remote host; or 3) A network error occurs.
func DialAndServe(addr string, handler http.Handler) error {
raddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return err
}
conn, err := net.DialTCP("tcp", nil, raddr)
if err != nil {
return err
}
s := http2.Server{}
s.ServeConn(conn, &http2.ServeConnOpts{
Handler: handler,
})
// if we've reached this point, then the underlying conn
// has been closed
return errors.New("ptth: remote address unavailable")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment