Skip to content

Commit

Permalink
chore(gorush): add timeout for http client (#473)
Browse files Browse the repository at this point in the history
* chore(gorush): add timeout for http client

dispatch feedback url

See: #449

* docs: update readme
  • Loading branch information
appleboy authored Feb 24, 2020
1 parent e85252b commit 5262255
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ core:
max_notification: 100
sync: false # set true if you need get error message from fail push notification in API response.
feedback_hook_url: "" # set a hook url if you need get error message asynchronously from fail push notification in API response.
feedback_timeout: 10 # default is 10 second
mode: "release"
ssl: false
cert_path: "cert.pem"
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ core:
max_notification: 100
sync: false # set true if you need get error message from fail push notification in API response.
feedback_hook_url: "" # set webhook url if you need get error message asynchronously from fail push notification in API response.
feedback_timeout: 10 # default is 10 second
mode: "release"
ssl: false
cert_path: "cert.pem"
Expand Down Expand Up @@ -118,6 +119,7 @@ type SectionCore struct {
KeyBase64 string `yaml:"key_base64"`
HTTPProxy string `yaml:"http_proxy"`
FeedbackURL string `yaml:"feedback_hook_url"`
FeedbackTimeout int64 `yaml:"feedback_timeout"`
PID SectionPID `yaml:"pid"`
AutoTLS SectionAutoTLS `yaml:"auto_tls"`
}
Expand Down Expand Up @@ -262,6 +264,7 @@ func LoadConf(confPath string) (ConfYaml, error) {
conf.Core.Mode = viper.GetString("core.mode")
conf.Core.Sync = viper.GetBool("core.sync")
conf.Core.FeedbackURL = viper.GetString("core.feedback_hook_url")
conf.Core.FeedbackTimeout = int64(viper.GetInt("core.feedback_timeout"))
conf.Core.SSL = viper.GetBool("core.ssl")
conf.Core.CertPath = viper.GetString("core.cert_path")
conf.Core.KeyPath = viper.GetString("core.key_path")
Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "release", suite.ConfGorushDefault.Core.Mode)
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.Sync)
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.FeedbackURL)
assert.Equal(suite.T(), int64(10), suite.ConfGorushDefault.Core.FeedbackTimeout)
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.SSL)
assert.Equal(suite.T(), "cert.pem", suite.ConfGorushDefault.Core.CertPath)
assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Core.KeyPath)
Expand Down Expand Up @@ -120,6 +121,7 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), "release", suite.ConfGorush.Core.Mode)
assert.Equal(suite.T(), false, suite.ConfGorush.Core.Sync)
assert.Equal(suite.T(), "", suite.ConfGorush.Core.FeedbackURL)
assert.Equal(suite.T(), int64(10), suite.ConfGorush.Core.FeedbackTimeout)
assert.Equal(suite.T(), false, suite.ConfGorush.Core.SSL)
assert.Equal(suite.T(), "cert.pem", suite.ConfGorush.Core.CertPath)
assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Core.KeyPath)
Expand Down
1 change: 1 addition & 0 deletions config/testdata/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ core:
max_notification: 100
sync: false # set true if you need get error message from fail push notification in API response.
feedback_hook_url: "" # set a hook url if you need get error message asynchronously from fail push notification in API response.
feedback_timeout: 10 # default is 10 second
mode: "release"
ssl: false
cert_path: "cert.pem"
Expand Down
18 changes: 15 additions & 3 deletions gorush/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"bytes"
"encoding/json"
"errors"
"net"
"net/http"
"time"
)

// DispatchFeedback sends a feedback to the configured gateway.
func DispatchFeedback(log LogPushEntry, url string) error {
func DispatchFeedback(log LogPushEntry, url string, timeout int64) error {

if url == "" {
return errors.New("The url can't be empty")
Expand All @@ -23,8 +25,18 @@ func DispatchFeedback(log LogPushEntry, url string) error {
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json; charset=utf-8")

HTTPClient := &http.Client{}
resp, err := HTTPClient.Do(req)
var transport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
var client = &http.Client{
Timeout: time.Duration(timeout) * time.Second,
Transport: transport,
}

resp, err := client.Do(req)

if resp != nil {
defer resp.Body.Close()
Expand Down
7 changes: 4 additions & 3 deletions gorush/feedback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/appleboy/gorush/config"

"github.com/stretchr/testify/assert"
)

Expand All @@ -21,7 +22,7 @@ func TestEmptyFeedbackURL(t *testing.T) {
Error: "",
}

err := DispatchFeedback(logEntry, PushConf.Core.FeedbackURL)
err := DispatchFeedback(logEntry, PushConf.Core.FeedbackURL, PushConf.Core.FeedbackTimeout)
assert.NotNil(t, err)
}

Expand All @@ -37,7 +38,7 @@ func TestHTTPErrorInFeedbackCall(t *testing.T) {
Error: "",
}

err := DispatchFeedback(logEntry, config.Core.FeedbackURL)
err := DispatchFeedback(logEntry, config.Core.FeedbackURL, config.Core.FeedbackTimeout)
assert.NotNil(t, err)
}

Expand Down Expand Up @@ -70,6 +71,6 @@ func TestSuccessfulFeedbackCall(t *testing.T) {
Error: "",
}

err := DispatchFeedback(logEntry, config.Core.FeedbackURL)
err := DispatchFeedback(logEntry, config.Core.FeedbackURL, config.Core.FeedbackTimeout)
assert.Nil(t, err)
}
6 changes: 3 additions & 3 deletions gorush/notification_apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,12 @@ Retry:
if PushConf.Core.Sync {
req.AddLog(getLogPushEntry(FailedPush, token, req, err))
} else if PushConf.Core.FeedbackURL != "" {
go func(logger *logrus.Logger, log LogPushEntry, url string) {
err := DispatchFeedback(log, url)
go func(logger *logrus.Logger, log LogPushEntry, url string, timeout int64) {
err := DispatchFeedback(log, url, timeout)
if err != nil {
logger.Error(err)
}
}(LogError, getLogPushEntry(FailedPush, token, req, err), PushConf.Core.FeedbackURL)
}(LogError, getLogPushEntry(FailedPush, token, req, err), PushConf.Core.FeedbackURL, PushConf.Core.FeedbackTimeout)
}

StatStorage.AddIosError(1)
Expand Down
6 changes: 3 additions & 3 deletions gorush/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ Retry:
if PushConf.Core.Sync {
req.AddLog(getLogPushEntry(FailedPush, to, req, result.Error))
} else if PushConf.Core.FeedbackURL != "" {
go func(logger *logrus.Logger, log LogPushEntry, url string) {
err := DispatchFeedback(log, url)
go func(logger *logrus.Logger, log LogPushEntry, url string, timeout int64) {
err := DispatchFeedback(log, url, timeout)
if err != nil {
logger.Error(err)
}
}(LogError, getLogPushEntry(FailedPush, to, req, result.Error), PushConf.Core.FeedbackURL)
}(LogError, getLogPushEntry(FailedPush, to, req, result.Error), PushConf.Core.FeedbackURL, PushConf.Core.FeedbackTimeout)
}
continue
}
Expand Down

0 comments on commit 5262255

Please sign in to comment.