From 439fb3bf6a4f58b88961cf73b7b754ee5f9da5e3 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Tue, 12 Dec 2023 17:01:47 +0200 Subject: [PATCH] Make kannel channel paused when we get Queued message in response --- handlers/kannel/handler.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/handlers/kannel/handler.go b/handlers/kannel/handler.go index c249a6fef..639e9cc45 100644 --- a/handlers/kannel/handler.go +++ b/handlers/kannel/handler.go @@ -2,6 +2,7 @@ package kannel import ( "context" + "errors" "fmt" "net/http" "net/url" @@ -200,10 +201,22 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, clog *courier.Ch } var resp *http.Response + var respBody []byte if verifySSL { - resp, _, err = h.RequestHTTP(req, clog) + resp, respBody, err = h.RequestHTTP(req, clog) } else { - resp, _, err = h.RequestHTTPInsecure(req, clog) + resp, respBody, err = h.RequestHTTPInsecure(req, clog) + } + + if strings.Contains(string(respBody), "Queued") { + rc := h.Backend().RedisPool().Get() + defer rc.Close() + rateLimitKey := fmt.Sprintf("rate_limit:%s", msg.Channel().UUID()) + rc.Do("SET", rateLimitKey, "engaged") + + // We pause sending 30 seconds so the connection to the SMSC is reset + rc.Do("EXPIRE", rateLimitKey, 30) + return nil, errors.New("received Queued response from kannel, we'll pause sending to empty the queue") } status := h.Backend().NewStatusUpdate(msg.Channel(), msg.ID(), courier.MsgStatusErrored, clog)