aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorAdnan Maolood <[email protected]>2021-02-23 16:01:29 -0500
committerAdnan Maolood <[email protected]>2021-02-23 16:01:29 -0500
commitd07e9d99d1c528a47ac1e133344a24581c09eaeb (patch)
treeec1258f14d39f2af6f68480c528b25a338e31601 /client.go
parentexamples/client: Stream response body (diff)
downloadgo-gemini-d07e9d99d1c528a47ac1e133344a24581c09eaeb.tar.xz
go-gemini-d07e9d99d1c528a47ac1e133344a24581c09eaeb.zip
client: Move context handling to do
Diffstat (limited to 'client.go')
-rw-r--r--client.go34
1 files changed, 17 insertions, 17 deletions
diff --git a/client.go b/client.go
index 4372464..386cc7d 100644
--- a/client.go
+++ b/client.go
@@ -125,22 +125,7 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
res := make(chan result, 1)
go func() {
- ctx, cancel := context.WithCancel(ctx)
- done := ctx.Done()
- cw := &contextWriter{
- ctx: ctx,
- done: done,
- cancel: cancel,
- wc: conn,
- }
- cr := &contextReader{
- ctx: ctx,
- done: done,
- cancel: cancel,
- rc: conn,
- }
-
- resp, err := c.do(cw, cr, req)
+ resp, err := c.do(ctx, conn, req)
res <- result{resp, err}
}()
@@ -153,7 +138,22 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
}
}
-func (c *Client) do(w io.Writer, rc io.ReadCloser, req *Request) (*Response, error) {
+func (c *Client) do(ctx context.Context, conn net.Conn, req *Request) (*Response, error) {
+ ctx, cancel := context.WithCancel(ctx)
+ done := ctx.Done()
+ w := &contextWriter{
+ ctx: ctx,
+ done: done,
+ cancel: cancel,
+ wc: conn,
+ }
+ rc := &contextReader{
+ ctx: ctx,
+ done: done,
+ cancel: cancel,
+ rc: conn,
+ }
+
// Write the request
if err := req.Write(w); err != nil {
return nil, err