Add context timeouts for third-party requests
Some checks failed
/ golint (push) Failing after 3m56s
Some checks failed
/ golint (push) Failing after 3m56s
Avoid hanging a goroutine for a long time.
This commit is contained in:
parent
2093ea74c4
commit
15d8966971
11 changed files with 80 additions and 5 deletions
|
|
@ -3,6 +3,7 @@ package text
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
|
@ -11,6 +12,9 @@ import (
|
|||
)
|
||||
|
||||
func sendTextTwilio(ctx context.Context, source string, destination string, message string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
client := twilio.NewRestClient()
|
||||
|
||||
params := &twilioApi.CreateMessageParams{}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/lint"
|
||||
|
|
@ -25,6 +26,9 @@ type VoipMSResponse struct {
|
|||
}
|
||||
|
||||
func sendTextVoipms(ctx context.Context, to string, content string, media ...string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if len(content) > 2048 {
|
||||
return "", errors.New("Message content is more than 160 characters")
|
||||
}
|
||||
|
|
@ -63,7 +67,14 @@ func makeVoipMSRequest(params url.Values) (VoipMSResponse, error) {
|
|||
|
||||
// Make the HTTP request
|
||||
log.Debug().Str("full_url", full_url).Msg("Sending command to VoIP.ms")
|
||||
resp, err := http.Get(full_url)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", full_url, nil)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("Error creating request: %w", err)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("url", full_url).Msg("Failed to make request to Voip.MS")
|
||||
return result, fmt.Errorf("Error making request: %w", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue