Skip to content

Commit

Permalink
Merged master into develop, fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Sep 21, 2016
2 parents 5775469 + 2117e5c commit 72416b1
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ apps/test.json
logs
live_hybrid.conf
tyk.zeroconf.conf
tyk.zeroconf2.conf
tyk.zeroconf3.conf
lint_results.txt
build/
test_apps/
Expand Down
146 changes: 145 additions & 1 deletion api_definition_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
b64 "encoding/base64"
"encoding/json"
"errors"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tykcommon"
"github.com/gorilla/context"
"github.com/rubyist/circuitbreaker"
Expand Down Expand Up @@ -250,6 +251,147 @@ func (a *APIDefinitionLoader) readBody(response *http.Response) ([]byte, error)

}

func RegisterNodeWithDashboard(endpoint string, secret string) error {
// Get the definitions
log.Debug("Calling: ", endpoint)
newRequest, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
log.Error("Failed to create request: ", err)
}

newRequest.Header.Add("authorization", secret)

c := &http.Client{
Timeout: 5 * time.Second,
}
response, reqErr := c.Do(newRequest)

if reqErr != nil {
log.Error("Request failed: ", reqErr)
time.Sleep(time.Second * 5)
return RegisterNodeWithDashboard(endpoint, secret)
}

defer response.Body.Close()
retBody, err := ioutil.ReadAll(response.Body)

if response.StatusCode != 200 {
log.Error("Failed to register node, retrying in 5s")
log.Debug("Response was: ", string(retBody))
time.Sleep(time.Second * 5)
return RegisterNodeWithDashboard(endpoint, secret)
}

if err != nil {
return err
}

// Extract tagged APIs#
type NodeResponseOK struct {
Status string
Message map[string]string
Nonce string
}

thisVal := NodeResponseOK{}
decErr := json.Unmarshal(retBody, &thisVal)
if decErr != nil {
log.Error("Failed to decode body: ", decErr)
return decErr
}

// Set the NodeID
var found bool
NodeID, found = thisVal.Message["NodeID"]
if !found {
log.Error("Failed to register node, retrying in 5s")
time.Sleep(time.Second * 5)
return RegisterNodeWithDashboard(endpoint, secret)
}

log.WithFields(logrus.Fields{
"prefix": "dashboard",
"id": NodeID,
}).Info("Node registered")

// Set the nonce
ServiceNonceMutex.Lock()
defer ServiceNonceMutex.Unlock()
ServiceNonce = thisVal.Nonce
log.Debug("Registration Finished: Nonce Set: ", ServiceNonce)

return nil
}

func StartBeating(endpoint, secret string) {
for {
failure := SendHeartBeat(endpoint, secret)
if failure != nil {
log.Warning(failure)
}
time.Sleep(time.Second * 5)
}
}

func SendHeartBeat(endpoint string, secret string) error {
// Get the definitions
log.Debug("Calling: ", endpoint)
newRequest, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
log.Error("Failed to create request: ", err)
}

newRequest.Header.Add("authorization", secret)
newRequest.Header.Add("x-tyk-nodeid", NodeID)
log.Debug("Sending Heartbeat as: ", NodeID)

ServiceNonceMutex.Lock()
defer ServiceNonceMutex.Unlock()

newRequest.Header.Add("x-tyk-nonce", ServiceNonce)

c := &http.Client{
Timeout: 5 * time.Second,
}
response, reqErr := c.Do(newRequest)

if reqErr != nil {
return errors.New("Dashboard is down? Heartbeat is failing.")
}

if response.StatusCode != 200 {
return errors.New("Dashboard is down? Heartbeat is failing.")
}

defer response.Body.Close()
retBody, err := ioutil.ReadAll(response.Body)

if err != nil {
return err
}

// Extract tagged APIs#
type NodeResponseOK struct {
Status string
Message map[string]string
Nonce string
}

thisVal := NodeResponseOK{}
decErr := json.Unmarshal(retBody, &thisVal)
if decErr != nil {
log.Error("Failed to decode body: ", decErr)
return decErr
}

// Set the nonce

ServiceNonce = thisVal.Nonce
log.Debug("Hearbeat Finished: Nonce Set: ", ServiceNonce)

return nil
}

// LoadDefinitionsFromDashboardService will connect and download ApiDefintions from a Tyk Dashboard instance.
func (a *APIDefinitionLoader) LoadDefinitionsFromDashboardService(endpoint string, secret string) *[]*APISpec {
var APISpecs = []*APISpec{}
Expand All @@ -269,7 +411,9 @@ func (a *APIDefinitionLoader) LoadDefinitionsFromDashboardService(endpoint strin
defer ServiceNonceMutex.Unlock()
newRequest.Header.Add("x-tyk-nonce", ServiceNonce)

c := &http.Client{}
c := &http.Client{
Timeout: 5 * time.Second,
}
response, reqErr := c.Do(newRequest)

if reqErr != nil {
Expand Down
16 changes: 8 additions & 8 deletions dashboard_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type DashboardServiceSender interface {
}

type HTTPDashboardHandler struct {
RegistrationEndpoint string
RegistrationEndpoint string
DeRegistrationEndpoint string
HeartBeatEndpoint string
Secret string
HeartBeatEndpoint string
Secret string

heartBeatStopSentinel bool
}
Expand All @@ -47,9 +47,9 @@ func ReLogin() {
}

if err == nil {
go DashService.StartBeating()
go DashService.StartBeating()
}

}

func (h *HTTPDashboardHandler) Init() error {
Expand Down Expand Up @@ -77,7 +77,7 @@ func (h *HTTPDashboardHandler) Register() error {
newRequest.Header.Add("x-tyk-hostname", HostDetails.Hostname)

c := &http.Client{
Timeout: 5*time.Second,
Timeout: 5 * time.Second,
}
response, reqErr := c.Do(newRequest)

Expand Down Expand Up @@ -171,7 +171,7 @@ func (h *HTTPDashboardHandler) SendHeartBeat(endpoint string, secret string) err
newRequest.Header.Add("x-tyk-nonce", ServiceNonce)

c := &http.Client{
Timeout: 5*time.Second,
Timeout: 5 * time.Second,
}
response, reqErr := c.Do(newRequest)

Expand Down Expand Up @@ -226,7 +226,7 @@ func (h *HTTPDashboardHandler) DeRegister() error {
newRequest.Header.Add("x-tyk-nonce", ServiceNonce)

c := &http.Client{
Timeout: 5*time.Second,
Timeout: 5 * time.Second,
}
response, reqErr := c.Do(newRequest)

Expand Down
3 changes: 2 additions & 1 deletion handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

var SessionCache *cache.Cache = cache.New(10*time.Second, 5*time.Second)
var ExpiryCache *cache.Cache = cache.New(600*time.Second, 5*time.Second)
var ExpiryCache *cache.Cache = cache.New(600*time.Second, 10*time.Minute)

type ReturningHttpHandler interface {
ServeHTTP(http.ResponseWriter, *http.Request) *http.Response
Expand Down Expand Up @@ -85,6 +85,7 @@ func (t TykMiddleware) GetOrgSessionExpiry(orgid string) int64 {
log.Debug("Checking: ", orgid)
cachedVal, found := ExpiryCache.Get(orgid)
if !found {
go t.GetOrgSession(orgid)
log.Debug("no cached entry found, returning 7 days")
return 604800
}
Expand Down
5 changes: 5 additions & 0 deletions middleware_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
return nil, 200
}

if len(cachedData) == 0 {
m.CacheStore.DeleteKey(thisKey)
return nil, 200
}

retObj := bytes.NewReader([]byte(cachedData))
log.Debug("Cache got: ", cachedData)

Expand Down
7 changes: 3 additions & 4 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ func LoadPoliciesFromDashboard(endpoint string, secret string, allowExplicit boo
newRequest.Header.Add("x-tyk-nonce", ServiceNonce)

c := &http.Client{
Timeout: 5*time.Second,
Timeout: 5 * time.Second,
}

log.Debug("Calling: ", endpoint)

response, reqErr := c.Do(newRequest)

if reqErr != nil {
log.Error("Request failed: ", reqErr)
return policies
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

var VERSION string = "v2.2.0.18"
var VERSION string = "v2.2.0.23"

0 comments on commit 72416b1

Please sign in to comment.