Set default http client timeout to avoid deadlocks (#132)

* Set default http client timeout to avoid deadlocks
* Return error instead of unexpected fatal
This commit is contained in:
andig 2020-05-12 12:29:15 +02:00 • committed by GitHub
parent 12ca72ec2c
commit 8117f8c0b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
type HTTPHelper struct {
@ -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
}

View file

@ -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
}

View file

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
@ -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
},
}