Skip to content

Commit

Permalink
Set default http client timeout to avoid deadlocks (#132)
Browse files Browse the repository at this point in the history
* Set default http client timeout to avoid deadlocks
* Return error instead of unexpected fatal
  • Loading branch information
andig committed May 12, 2020
1 parent 12ca72e commit 8117f8c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion util/http.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"
)

type HTTPHelper struct {
Expand All @@ -17,7 +18,7 @@ type HTTPHelper struct {
func NewHTTPHelper(log *Logger) *HTTPHelper {
r := &HTTPHelper{
Log: log,
Client: &http.Client{},
Client: &http.Client{Timeout: 10 * time.Second},
}
return r
}
Expand Down
1 change: 1 addition & 0 deletions vehicle/bmw.go
Expand Up @@ -77,6 +77,7 @@ func (v *BMW) login(user, password string) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

client := &http.Client{
Timeout: v.HTTPHelper.Client.Timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, // don't follow redirects
}

Expand Down
8 changes: 4 additions & 4 deletions vehicle/porsche.go
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
Expand Down Expand Up @@ -87,14 +86,15 @@ func NewPorscheFromConfig(log *util.Logger, other map[string]interface{}) api.Ve
func (v *Porsche) login(user, password string) error {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
return err
}

// the flow is using Oauth2 and >10 redirects
client := &http.Client{
Jar: jar,
Jar: jar,
Timeout: v.HTTPHelper.Client.Timeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return nil
return nil // allow >10 redirects
},
}

Expand Down

0 comments on commit 8117f8c

Please sign in to comment.