Add timeout context to twilio client

This commit is contained in:
Eli Ribble 2026-05-19 16:03:42 +00:00
parent 7237f5f666
commit 1f798c7521
No known key found for this signature in database

View file

@ -3,20 +3,41 @@ package text
import ( import (
"context" "context"
"fmt" "fmt"
"net/http"
"time" "time"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/twilio/twilio-go" "github.com/twilio/twilio-go"
twilioClient "github.com/twilio/twilio-go/client"
twilioApi "github.com/twilio/twilio-go/rest/api/v2010" twilioApi "github.com/twilio/twilio-go/rest/api/v2010"
"source.gleipnir.technology/Gleipnir/nidus-sync/config" "source.gleipnir.technology/Gleipnir/nidus-sync/config"
) )
// forceContextRoundTripper sets the given context on every request and passes it to the wrapped RoundTripper
type forceContextRoundTripper struct {
roundTripper http.RoundTripper
ctx context.Context
}
func (f *forceContextRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = req.Clone(f.ctx)
return f.roundTripper.RoundTrip(req)
}
func sendTextTwilio(ctx context.Context, source string, destination string, message string) (string, error) { func sendTextTwilio(ctx context.Context, source string, destination string, message string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel() defer cancel()
client := twilio.NewRestClient() client := twilio.NewRestClient()
http_client := &http.Client{
Timeout: 30 * time.Second,
}
http_client.Transport = &forceContextRoundTripper{
ctx: ctx,
roundTripper: http_client.Transport,
}
client.RequestHandler.Client.(*twilioClient.Client).HTTPClient = http_client
params := &twilioApi.CreateMessageParams{} params := &twilioApi.CreateMessageParams{}
params.SetMessagingServiceSid(config.TwilioMessagingServiceSID) params.SetMessagingServiceSid(config.TwilioMessagingServiceSID)