Skip to content

Commit d3758a5

Browse files
authored
Merge pull request #71 from ToughLad/master
Fix HTTP/2 build error by implementing RoundTripper adapter
2 parents 12370d3 + 69c4eb8 commit d3758a5

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

configure.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import (
1111
// ErrServerSupport indicates whether the server supports HTTP/2 or not.
1212
var ErrServerSupport = errors.New("server doesn't support HTTP/2")
1313

14+
// clientAdapter adapts a Client.Do method to implement fasthttp.RoundTripper
15+
type clientAdapter struct {
16+
client *Client
17+
}
18+
19+
// RoundTrip implements fasthttp.RoundTripper by calling the wrapped client's Do method
20+
func (ca *clientAdapter) RoundTrip(hc *fasthttp.HostClient, req *fasthttp.Request, resp *fasthttp.Response) (retry bool, err error) {
21+
err = ca.client.Do(req, resp)
22+
return false, err
23+
}
24+
1425
func configureDialer(d *Dialer) {
1526
if d.TLSConfig == nil {
1627
d.TLSConfig = &tls.Config{
@@ -67,7 +78,7 @@ func ConfigureClient(c *fasthttp.HostClient, opts ClientOpts) error {
6778
c.IsTLS = true
6879
c.TLSConfig = d.TLSConfig
6980

70-
c.Transport = cl
81+
c.Transport = &clientAdapter{client: cl}
7182

7283
return nil
7384
}
@@ -79,14 +90,14 @@ func ConfigureClient(c *fasthttp.HostClient, opts ClientOpts) error {
7990
// Future implementations may support HTTP/2 through plain TCP.
8091
//
8192
// This package currently supports the following fasthttp.Server settings:
82-
// - Handler: Obviously, the handler is taken from the Server.
83-
// - ReadTimeout: Will cancel a stream if the client takes more than ReadTimeout
84-
// to send a request. This option NEVER closes the connection.
85-
// - IdleTimeout: Will close the connection if the client doesn't send a request
86-
// within the IdleTimeout. This option ignores any PING/PONG mechanism.
87-
// To disable the option you can set it to zero. No value is taken by default,
88-
// which means that by default ALL connections are open until either endpoint
89-
// closes the connection.
93+
// - Handler: Obviously, the handler is taken from the Server.
94+
// - ReadTimeout: Will cancel a stream if the client takes more than ReadTimeout
95+
// to send a request. This option NEVER closes the connection.
96+
// - IdleTimeout: Will close the connection if the client doesn't send a request
97+
// within the IdleTimeout. This option ignores any PING/PONG mechanism.
98+
// To disable the option you can set it to zero. No value is taken by default,
99+
// which means that by default ALL connections are open until either endpoint
100+
// closes the connection.
90101
func ConfigureServer(s *fasthttp.Server, cnf ServerConfig) *Server {
91102
cnf.defaults()
92103

@@ -113,4 +124,4 @@ func ConfigureServerAndConfig(s *fasthttp.Server, tlsConfig *tls.Config) *Server
113124
return s2
114125
}
115126

116-
var ErrNotAvailableStreams = errors.New("ran out of available streams")
127+
var ErrNotAvailableStreams = errors.New("ran out of available streams")

0 commit comments

Comments
 (0)