Skip to content

Commit

Permalink
api: use a named constant for the special code 666
Browse files Browse the repository at this point in the history
Also remove handling of the special code 1666, which has never been
used.

Fixes #764.
  • Loading branch information
mvdan authored and buger committed Jun 8, 2017
1 parent 9252536 commit 20f86a8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (m *CoProcessMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Requ
}
w.WriteHeader(int(returnObject.Request.ReturnOverrides.ResponseCode))
w.Write([]byte(returnObject.Request.ReturnOverrides.ResponseError))
return nil, 666
return nil, mwStatusRespond
}

// Is this a CP authentication middleware?
Expand Down
14 changes: 3 additions & 11 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/paulbellamy/ratecounter"
)

const mwStatusRespond = 666

var GlobalRate = ratecounter.NewRateCounter(1 * time.Second)

type TykMiddlewareImplementation interface {
Expand Down Expand Up @@ -77,18 +79,8 @@ func CreateMiddleware(mw TykMiddlewareImplementation, tykMwSuper *TykMiddleware)
return
}

// Special code, stops execution
if errCode == 1666 {
// Stop
log.Info("[Middleware] Received stop code")
meta["stopped"] = "1"
job.TimingKv("exec_time", time.Since(startTime).Nanoseconds(), meta)
job.TimingKv(eventName+".exec_time", time.Since(startTime).Nanoseconds(), meta)
return
}

// Special code, bypasses all other execution
if errCode != 666 {
if errCode != mwStatusRespond {
// No error, carry on...
meta["bypass"] = "1"
h.ServeHTTP(w, r)
Expand Down
4 changes: 2 additions & 2 deletions middleware_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
go m.CacheStore.SetKey(key, toStore, cacheTTL)

}
return nil, 666
return nil, mwStatusRespond

}

Expand Down Expand Up @@ -272,5 +272,5 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
}

// Stop any further execution
return nil, 666
return nil, mwStatusRespond
}
4 changes: 2 additions & 2 deletions middleware_version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, co
// We handle redirects before ignores in case we aren't using a whitelist
if stat == StatusRedirectFlowByReply {
v.DoMockReply(w, meta)
return nil, 666
return nil, mwStatusRespond
}

if expTime, _ := meta.(*time.Time); expTime != nil {
Expand All @@ -70,7 +70,7 @@ func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, co

if stat == StatusOkAndIgnore {
v.sh.ServeHTTP(w, r)
return nil, 666
return nil, mwStatusRespond
}

return nil, 200
Expand Down
2 changes: 1 addition & 1 deletion middleware_virtual_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (d *VirtualEndpoint) ProcessRequest(w http.ResponseWriter, r *http.Request,
return nil, 200
}

return nil, 666
return nil, mwStatusRespond
}

func (d *VirtualEndpoint) HandleResponse(rw http.ResponseWriter, res *http.Response, ses *SessionState) error {
Expand Down

0 comments on commit 20f86a8

Please sign in to comment.