Skip to content

Commit

Permalink
Fix racy notificationManager (#3714)
Browse files Browse the repository at this point in the history
* using native http client for notification manager

* handling errors in NotificationsManager

* fixing errors

Co-authored-by: Jeffy Mathew <[email protected]>
  • Loading branch information
2 people authored and sredxny committed Jan 2, 2022
1 parent 6d1718c commit 78350ef
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions apidef/notifications.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package apidef

import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"time"

logger "github.com/TykTechnologies/tyk/log"

"github.com/franela/goreq"
)

var log = logger.Get()
var httpClient = initHttpNotificationClient()

// NotificationsManager handles sending notifications to OAuth endpoints to notify the provider of key changes.
// TODO: Make this more generic
Expand All @@ -17,6 +21,20 @@ type NotificationsManager struct {
OAuthKeyChangeURL string `bson:"oauth_on_keychange_url" json:"oauth_on_keychange_url"`
}

func initHttpNotificationClient() *http.Client {
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
return &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}

}

// SendRequest sends the requested package (as a POST) to the defined
func (n NotificationsManager) SendRequest(wait bool, count int, notification interface{}) {
if n.OAuthKeyChangeURL == "" {
Expand All @@ -32,23 +50,43 @@ func (n NotificationsManager) SendRequest(wait bool, count int, notification int
}
}

req := goreq.Request{
Method: "POST",
Uri: n.OAuthKeyChangeURL,
UserAgent: "Tyk-Gatewy-Notifications",
ContentType: "application/json",
Body: notification,
var req *http.Request
var resp *http.Response
var postBody []byte
var err error

postBody, err = json.Marshal(notification)
if err != nil {
log.Error("Error Marshaling the notification body:", err)
return
}
responseBody := bytes.NewBuffer(postBody)

req.AddHeader("X-Tyk-Shared-Secret", n.SharedSecret)
req, err = http.NewRequest("POST", n.OAuthKeyChangeURL, responseBody)
if err != nil {
log.Error("Cannot create a new notification request:", err)
return
}
req.Header.Set("User-Agent", "Tyk-Gatewy-Notifications")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Tyk-Shared-Secret", n.SharedSecret)

resp, err := req.Do()
resp, err = httpClient.Do(req)
if err != nil {
log.Error("Request failed, trying again in 10s. Error was: ", err)
count++
go n.SendRequest(true, count, notification)
return
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Error("Request failed, trying again in 10s. Error was: ", err)
count++
n.SendRequest(true, count, notification)
return
}

if resp.StatusCode != 200 {
log.Error("Request returned non-200 status, trying again in 10s.")
count++
Expand Down

0 comments on commit 78350ef

Please sign in to comment.