Skip to content

Commit

Permalink
Merge pull request #11 from danp/dial-buffered-conn
Browse files Browse the repository at this point in the history
h2c/dialer: continue to use bufio.Reader for reading
  • Loading branch information
lstoll committed Feb 26, 2018
2 parents 4cbf15d + 333bd35 commit 9334f17
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions h2c/dialer.go
Expand Up @@ -62,7 +62,9 @@ func (d Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn
return nil, err
}

res, err := http.ReadResponse(bufio.NewReader(conn), req)
br := bufio.NewReader(conn)

res, err := http.ReadResponse(br, req)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +81,7 @@ func (d Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn
return nil, errors.New("h2c upgrade failed, upgrade response body was non empty")
}

return conn, nil
return &bufferedConn{br: br, Conn: conn}, nil
}

// Dial connects to the address on the named network.
Expand All @@ -92,3 +94,12 @@ func (d Dialer) DialGRPC(addr string, timeout time.Duration) (net.Conn, error) {
ctx, _ := context.WithTimeout(context.Background(), timeout)
return d.DialContext(ctx, "tcp", addr)
}

type bufferedConn struct {
br *bufio.Reader
net.Conn
}

func (b *bufferedConn) Read(p []byte) (int, error) {
return b.br.Read(p)
}

0 comments on commit 9334f17

Please sign in to comment.