Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Use httpcontrol library to manage timeouts #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"errors"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"

"github.com/facebookgo/httpcontrol"
)

// Api is the interface for interacting with Firebase.
Expand Down Expand Up @@ -285,21 +286,12 @@ func (f *f) Call(method, path, auth string, body []byte, params map[string]strin
return ret, nil
}

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, cTimeout)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil
}
}

func newTimeoutClient(connectTimeout time.Duration, readWriteTimeout time.Duration) *http.Client {
return &http.Client{
Transport: &http.Transport{
Dial: TimeoutDialer(connectTimeout, readWriteTimeout),
Transport: &httpcontrol.Transport{
RequestTimeout: readWriteTimeout,
DialTimeout: connectTimeout,
MaxTries: 3,
},
}
}