Skip to content

Commit

Permalink
Merge pull request rapidpro#343 from nyaruka/local-fail-cache
Browse files Browse the repository at this point in the history
Local fail cache
  • Loading branch information
nicpottier authored Dec 7, 2020
2 parents a1b1e5e + e966ce6 commit 23a0f54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/nyaruka/gocommon v1.6.1
github.com/nyaruka/librato v1.0.0
github.com/nyaruka/null v1.1.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ github.com/nyaruka/null v1.1.1 h1:kRy1Luj7jUHWEFqc2J6VXrKYi/beLEZdS1C7rA6vqTE=
github.com/nyaruka/null v1.1.1/go.mod h1:HSAFbLNOaEhHnoU0VCveCPz0GDtJ3GEtFWhvnBNkhPE=
github.com/nyaruka/phonenumbers v1.0.58 h1:IAlGDA4wuGQXe2lwOQvkZfBvA1DlAik+MX5k9k5C2IU=
github.com/nyaruka/phonenumbers v1.0.58/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
22 changes: 12 additions & 10 deletions handlers/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/rcache"
"github.com/nyaruka/gocommon/urns"
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand All @@ -31,18 +32,21 @@ const (
channelTypeD3 = "D3"
channelTypeTXW = "TXW"

mediaCacheKeyPattern = "whatsapp_media_%s"
failureMediaCacheKeyPattern = "whatsapp_failed_media_%s"
mediaCacheKeyPattern = "whatsapp_media_%s"
)

var (
retryParam = ""
)

var failedMediaCache *cache.Cache

func init() {
courier.RegisterHandler(newWAHandler(courier.ChannelType(channelTypeWa), "WhatsApp"))
courier.RegisterHandler(newWAHandler(courier.ChannelType(channelTypeD3), "360Dialog"))
courier.RegisterHandler(newWAHandler(courier.ChannelType(channelTypeTXW), "TextIt"))

failedMediaCache = cache.New(15*time.Minute, 15*time.Minute)
}

type handler struct {
Expand Down Expand Up @@ -631,13 +635,11 @@ func (h *handler) fetchMediaID(msg courier.Msg, mimeType, mediaURL string) (stri
}

// check in failure cache
failureCacheKey := fmt.Sprintf(failureMediaCacheKeyPattern, msg.Channel().UUID().String())
failed, err := rcache.Get(rc, failureCacheKey, mediaURL)
if err != nil {
return "", logs, errors.Wrapf(err, "error reading from failed cache: %s : %s", failureCacheKey, mediaURL)
}
failKey := fmt.Sprintf("%s-%s", msg.Channel().UUID().String(), mediaURL)
found, _ := failedMediaCache.Get(failKey)

if failed == "true" {
// any non nil value means we cached a failure, don't try again until our cache expires
if found != nil {
return "", logs, nil
}

Expand All @@ -650,6 +652,7 @@ func (h *handler) fetchMediaID(msg courier.Msg, mimeType, mediaURL string) (stri
log := courier.NewChannelLogFromRR("Fetching media", msg.Channel(), msg.ID(), rr).WithError("error fetching media", err)
logs = append(logs, log)
if err != nil {
failedMediaCache.Set(failKey, true, cache.DefaultExpiration)
return "", logs, nil
}

Expand All @@ -671,8 +674,7 @@ func (h *handler) fetchMediaID(msg courier.Msg, mimeType, mediaURL string) (stri
log = courier.NewChannelLogFromRR("Uploading media to WhatsApp", msg.Channel(), msg.ID(), rr).WithError("Error uploading media to WhatsApp", err)
logs = append(logs, log)
if err != nil {
// put in failure cache
rcache.Set(rc, failureCacheKey, mediaURL, "true")
failedMediaCache.Set(failKey, true, cache.DefaultExpiration)
return "", logs, errors.Wrapf(err, "error uploading media to whatsapp")
}

Expand Down

0 comments on commit 23a0f54

Please sign in to comment.