Skip to content

Commit

Permalink
Reduced read dependency on request object, removed copy operation
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode authored and buger committed Feb 10, 2018
1 parent 06d448f commit 9a841f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
20 changes: 20 additions & 0 deletions log_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ func getLogEntryForRequest(r *http.Request, key string, data map[string]interfac
}
return log.WithFields(fields)
}

func getExplicitLogEntryForRequest(path string, IP string, key string, data map[string]interface{}) *logrus.Entry {
// populate http request fields
fields := logrus.Fields{
"path": path,
"origin": IP,
}
// add key to log if configured to do so
if key != "" {
fields["key"] = key
if !config.Global.EnableKeyLogging {
fields["key"] = logHiddenValue
}
}
// add to log additional fields if any passed
for key, val := range data {
fields[key] = val
}
return log.WithFields(fields)
}
16 changes: 8 additions & 8 deletions mw_organisation_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, i
}
active, found := orgActiveMap.Load(k.Spec.OrgID)

requestCopy := copyRequest(r)
go k.AllowAccessNext(orgChan, requestCopy)
go k.AllowAccessNext(orgChan, r.URL.Path, requestIP(r), r)

if found && !active.(bool) {
log.Debug("Is not active")
Expand All @@ -127,7 +126,7 @@ func (k *OrganizationMonitor) ProcessRequestOffThread(r *http.Request) (error, i
return nil, 200
}

func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, r *http.Request) {
func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, path string, IP string, r *http.Request) {

session, found := k.OrgSession(k.Spec.OrgID)

Expand All @@ -138,7 +137,7 @@ func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, r *http.Request
}

// Is it active?
logEntry := getLogEntryForRequest(r, k.Spec.OrgID, nil)
logEntry := getExplicitLogEntryForRequest(path, IP, k.Spec.OrgID, nil)
if session.IsInactive {
logEntry.Warning("Organisation access is disabled.")

Expand All @@ -157,10 +156,11 @@ func (k *OrganizationMonitor) AllowAccessNext(orgChan chan bool, r *http.Request

// Fire a quota exceeded event
k.FireEvent(EventOrgQuotaExceeded, EventKeyFailureMeta{
EventMetaDefault: EventMetaDefault{Message: "Organisation quota has been exceeded", OriginatingRequest: EncodeRequestToEvent(r)},
Path: r.URL.Path,
Origin: requestIP(r),
Key: k.Spec.OrgID,
EventMetaDefault: EventMetaDefault{
Message: "Organisation quota has been exceeded"},
Path: path,
Origin: IP,
Key: k.Spec.OrgID,
})

//return errors.New("This organisation quota has been exceeded, please contact your API administrator"), 403
Expand Down

0 comments on commit 9a841f6

Please sign in to comment.