Skip to content

Commit 1685bf2

Browse files
authored
fix: add http timeout to add external provider requests (#471)
* fix: add http timeout to add external provider requests * read timeout from env
1 parent 3eec285 commit 1685bf2

File tree

9 files changed

+39
-7
lines changed

9 files changed

+39
-7
lines changed

api/provider/apple.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const (
3232
// AppleProvider stores the custom config for apple provider
3333
type AppleProvider struct {
3434
*oauth2.Config
35-
httpClient *http.Client
3635
UserInfoURL string
3736
}
3837

api/provider/notion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (g notionProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*Us
7777
req.Header.Set("Notion-Version", notionApiVersion)
7878
req.Header.Set("Authorization", "Bearer "+tok.AccessToken)
7979

80-
client := &http.Client{}
80+
client := &http.Client{Timeout: defaultTimeout}
8181
resp, err := client.Do(req)
8282

8383
if err != nil {

api/provider/provider.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@ import (
55
"context"
66
"encoding/json"
77
"io/ioutil"
8+
"log"
89
"net/http"
10+
"os"
11+
"time"
912

1013
"golang.org/x/oauth2"
1114
)
1215

16+
var defaultTimeout time.Duration = time.Second * 10
17+
18+
func init() {
19+
timeoutStr := os.Getenv("GOTRUE_INTERNAL_HTTP_TIMEOUT")
20+
if timeoutStr != "" {
21+
if timeout, err := time.ParseDuration(timeoutStr); err != nil {
22+
log.Fatalf("error loading GOTRUE_INTERNAL_HTTP_TIMEOUT: %v", err.Error())
23+
} else if timeout != 0 {
24+
defaultTimeout = timeout
25+
}
26+
}
27+
}
28+
1329
type Claims struct {
1430
// Reserved claims
1531
Issuer string `json:"iss,omitempty"`
@@ -103,6 +119,7 @@ func chooseHost(base, defaultHost string) string {
103119

104120
func makeRequest(ctx context.Context, tok *oauth2.Token, g *oauth2.Config, url string, dst interface{}) error {
105121
client := g.Client(ctx, tok)
122+
client.Timeout = defaultTimeout
106123
res, err := client.Get(url)
107124
if err != nil {
108125
return err

api/provider/twitch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (t twitchProvider) GetUserData(ctx context.Context, tok *oauth2.Token) (*Us
9292
req.Header.Set("Client-Id", t.Config.ClientID)
9393
req.Header.Set("Authorization", "Bearer "+tok.AccessToken)
9494

95-
client := &http.Client{}
95+
client := &http.Client{Timeout: defaultTimeout}
9696
resp, err := client.Do(req)
9797

9898
if err != nil {

api/sms_provider/messagebird.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (t *MessagebirdProvider) SendSms(phone string, message string) error {
6464
"datacoding": {"unicode"},
6565
}
6666

67-
client := &http.Client{}
67+
client := &http.Client{Timeout: defaultTimeout}
6868
r, err := http.NewRequest("POST", t.APIPath, strings.NewReader(body.Encode()))
6969
if err != nil {
7070
return err

api/sms_provider/sms_provider.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@ package sms_provider
22

33
import (
44
"fmt"
5+
"log"
6+
"os"
7+
"time"
58

69
"github.com/netlify/gotrue/conf"
710
)
811

12+
var defaultTimeout time.Duration = time.Second * 10
13+
14+
func init() {
15+
timeoutStr := os.Getenv("GOTRUE_INTERNAL_HTTP_TIMEOUT")
16+
if timeoutStr != "" {
17+
if timeout, err := time.ParseDuration(timeoutStr); err != nil {
18+
log.Fatalf("error loading GOTRUE_INTERNAL_HTTP_TIMEOUT: %v", err.Error())
19+
} else if timeout != 0 {
20+
defaultTimeout = timeout
21+
}
22+
}
23+
}
24+
925
type SmsProvider interface {
1026
SendSms(phone, message string) error
1127
}

api/sms_provider/textlocal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (t *TextlocalProvider) SendSms(phone string, message string) error {
5252
"numbers": {phone},
5353
}
5454

55-
client := &http.Client{}
55+
client := &http.Client{Timeout: defaultTimeout}
5656
r, err := http.NewRequest("POST", t.APIPath, strings.NewReader(body.Encode()))
5757
if err != nil {
5858
return err

api/sms_provider/twilio.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (t *TwilioProvider) SendSms(phone string, message string) error {
6262
"Body": {message},
6363
}
6464

65-
client := &http.Client{}
65+
client := &http.Client{Timeout: defaultTimeout}
6666
r, err := http.NewRequest("POST", t.APIPath, strings.NewReader(body.Encode()))
6767
if err != nil {
6868
return err

api/sms_provider/vonage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (t *VonageProvider) SendSms(phone string, message string) error {
5252
"api_secret": {t.Config.ApiSecret},
5353
}
5454

55-
client := &http.Client{}
55+
client := &http.Client{Timeout: defaultTimeout}
5656
r, err := http.NewRequest("POST", t.APIPath, strings.NewReader(body.Encode()))
5757
if err != nil {
5858
return err

0 commit comments

Comments
 (0)