1package gocb
2
3import (
4	"net/http"
5	"time"
6)
7
8// Wrapper around net.http.Client.Do().
9// This allows a per-request timeout without setting the timeout on the Client object
10// directly.
11// The third parameter is the duration for the request itself.
12func doHttpWithTimeout(cli *http.Client, req *http.Request, timeout time.Duration) (resp *http.Response, err error) {
13	if timeout.Seconds() == 0 {
14		// No timeout
15		resp, err = cli.Do(req)
16		return
17	}
18
19	tmoch := make(chan struct{})
20	timer := time.AfterFunc(timeout, func() {
21		tmoch <- struct{}{}
22	})
23
24	req.Cancel = tmoch
25	resp, err = cli.Do(req)
26	timer.Stop()
27	return
28}
29