Add context timeouts for third-party requests
Some checks failed
/ golint (push) Failing after 3m56s

Avoid hanging a goroutine for a long time.
This commit is contained in:
Eli Ribble 2026-05-19 00:24:16 +00:00
parent 2093ea74c4
commit 15d8966971
No known key found for this signature in database
11 changed files with 80 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package email
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/rs/zerolog/log"
@ -69,6 +70,9 @@ type emailResponse struct {
var FORWARDEMAIL_EMAIL_POST_API = "https://api.forwardemail.net/v1/emails"
func Send(ctx context.Context, email Request) (result emailResponse, err error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
client := resty.New()
var err_resp emailResponseError

View file

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

View file

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

View file

@ -2,6 +2,7 @@ package labelstudio
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@ -35,6 +36,9 @@ var ACCESS_TOKEN_DURATION_SECONDS time.Duration = 240 * time.Second
// GetAccessToken converts the API key into an access token
func (c *Client) GetAccessToken() error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Create request body
reqBody := map[string]string{
"refresh": c.APIKey,
@ -47,7 +51,7 @@ func (c *Client) GetAccessToken() error {
}
// Create request
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/token/refresh", c.BaseURL), bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/api/token/refresh", c.BaseURL), bytes.NewBuffer(jsonBody))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
@ -91,6 +95,9 @@ func (c *Client) GetAccessToken() error {
}
func (c *Client) makeRequest(method string, path string, payload []byte) (*http.Response, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Check if we have an access token, if not try to get it
if c.AccessToken == "" || time.Now().After(c.AccessTokenExpires) {
if err := c.GetAccessToken(); err != nil {
@ -99,7 +106,7 @@ func (c *Client) makeRequest(method string, path string, payload []byte) (*http.
}
// Create request
url := fmt.Sprintf("%s/%s", c.BaseURL, path)
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(payload))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

View file

@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"time"
"github.com/rs/zerolog/log"
"resty.dev/v3"
@ -133,6 +134,9 @@ func (re ResponseError) Error() string {
}
func (l *Lob) AddressCreate(ctx context.Context, req RequestAddressCreate) (Address, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
var result Address
var error_response ResponseError
resp, err := l.client.R().
@ -152,6 +156,9 @@ func (l *Lob) AddressCreate(ctx context.Context, req RequestAddressCreate) (Addr
return result, nil
}
func (l *Lob) AddressList(ctx context.Context) ([]Address, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
var result ResponseAddressList
var error_response ResponseError
@ -172,6 +179,9 @@ func (l *Lob) AddressList(ctx context.Context) ([]Address, error) {
}
func (l *Lob) LetterCreate(ctx context.Context, req RequestLetterCreate) (Letter, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
var error_response ResponseError
var result Letter
color_str := "false"
@ -205,6 +215,9 @@ func (l *Lob) LetterCreate(ctx context.Context, req RequestLetterCreate) (Letter
return result, nil
}
func (l *Lob) LetterList(ctx context.Context) ([]Letter, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
var error_response ResponseError
var result ResponseLetterList

View file

@ -3,6 +3,7 @@ package geocode
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/nidus-sync/db/models"
"github.com/Gleipnir-Technology/nidus-sync/stadia"
@ -17,6 +18,9 @@ type AutocompleteResult struct {
}
func Autocomplete(ctx context.Context, org *models.Organization, address string) ([]*AutocompleteResult, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
req := stadia.RequestGeocodeAutocomplete{
Text: address,
}

View file

@ -3,6 +3,7 @@ package geocode
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/nidus-sync/db"
"github.com/Gleipnir-Technology/nidus-sync/h3utils"
@ -11,6 +12,9 @@ import (
)
func ByGID(ctx context.Context, gid string) (*GeocodeResult, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
req := stadia.RequestGeocodeByGID{
GIDs: []string{gid},
}

View file

@ -80,6 +80,9 @@ func restyMiddleware(rclient *resty.Client, response *resty.Response) error {
}
func GeocodeRaw(ctx context.Context, org *models.Organization, address string) (*GeocodeResult, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
req := stadia.RequestGeocodeRaw{
Text: address,
}
@ -95,6 +98,9 @@ func GeocodeRaw(ctx context.Context, org *models.Organization, address string) (
return toGeocodeResult(resp.Features, address, addresses)
}
func GeocodeStructured(ctx context.Context, org *models.Organization, a types.Address) (*GeocodeResult, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
street := fmt.Sprintf("%s %s", a.Number, a.Street)
req := stadia.RequestGeocodeStructured{
Address: &street,
@ -133,6 +139,9 @@ func GetParcel(ctx context.Context, txn bob.Executor, a types.Address) (*models.
return result, nil
}
func ReverseGeocode(ctx context.Context, location types.Location) (*GeocodeResult, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
req := stadia.RequestReverseGeocode{
Latitude: location.Latitude,
Longitude: location.Longitude,
@ -149,6 +158,9 @@ func ReverseGeocode(ctx context.Context, location types.Location) (*GeocodeResul
}
func ReverseGeocodeClosest(ctx context.Context, location types.Location) (*GeocodeResult, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
req := stadia.RequestReverseGeocode{
Latitude: location.Latitude,
Longitude: location.Longitude,

View file

@ -35,10 +35,13 @@ type OAuthTokenResponse struct {
}
func DoTokenRequest(ctx context.Context, form url.Values) (*OAuthTokenResponse, error) {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
form.Set("client_id", config.ClientID)
baseURL := "https://www.arcgis.com/sharing/rest/oauth2/token/"
req, err := http.NewRequest("POST", baseURL, strings.NewReader(form.Encode()))
req, err := http.NewRequestWithContext(ctx, "POST", baseURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, fmt.Errorf("Failed to create request: %w", err)
}

View file

@ -3,6 +3,7 @@ package pdf
import (
"context"
"fmt"
"time"
"github.com/Gleipnir-Technology/nidus-sync/config"
"github.com/chromedp/cdproto/page"
@ -13,7 +14,9 @@ import (
func GeneratePDF(ctx context.Context, path string) ([]byte, error) {
// create context
chromedp.Env("CHROME_FLAGS=--no-sandbox --disable-gpu --disable-dev-shm-usage")
chrome_ctx, cancel := chromedp.NewContext(context.Background())
chromeCtx, chromeCancel := context.WithTimeout(context.Background(), 60*time.Second)
defer chromeCancel()
chrome_ctx, cancel := chromedp.NewContext(chromeCtx)
defer cancel()
// capture pdf

View file

@ -11,6 +11,7 @@ import (
"net/http"
"os"
"path/filepath"
"time"
"github.com/Gleipnir-Technology/arcgis-go"
"github.com/Gleipnir-Technology/arcgis-go/fieldseeker"
@ -185,6 +186,9 @@ func getTileFlyover(ctx context.Context, w http.ResponseWriter, org *models.Orga
return writeTile(w, image)
}
func getTileSatellite(ctx context.Context, w http.ResponseWriter, z, y, x uint) error {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
map_service_id := "stadia"
map_service, err := models.TileServices.Query(
models.SelectWhere.TileServices.Name.EQ(map_service_id),
@ -218,6 +222,9 @@ func getTileSatellite(ctx context.Context, w http.ResponseWriter, z, y, x uint)
return writeTile(w, &tile)
}
func imageAtPoint(ctx context.Context, org *models.Organization, level uint, lat, lng float64) (*TileRaster, error) {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
fssync, err := getFieldseeker(ctx, org)
if err != nil {
return nil, fmt.Errorf("create fssync: %w", err)
@ -288,6 +295,9 @@ type TileRaster struct {
}
func ImageAtTile(ctx context.Context, org *models.Organization, level, y, x uint) (*TileRaster, error) {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
oauth, err := oauth.GetOAuthForOrg(ctx, org)
if err != nil {
return nil, fmt.Errorf("get oauth for org: %w", err)