Skip to content

Commit

Permalink
More issues to iron out, still testing hashing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Mar 20, 2015
1 parent c368dc5 commit cd52dd1
Show file tree
Hide file tree
Showing 5 changed files with 904 additions and 2 deletions.
3 changes: 3 additions & 0 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type RedisAnalyticsHandler struct {

// RecordHit will store an AnalyticsRecord in Redis
func (r RedisAnalyticsHandler) RecordHit(thisRecord AnalyticsRecord) error {
// If we are obfuscating API Keys, store the hashed representation (config check handled in hashing function)
thisRecord.APIKey = getHash(thisRecord.APIKey)

encoded, err := msgpack.Marshal(thisRecord)
u5, _ := uuid.NewV4()

Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Config struct {
} `json:"health_check"`
UseAsyncSessionWrite bool `json:"optimisations_use_async_session_write"`
AllowMasterKeys bool `json:"allow_master_keys"`
HashKeys bool `json:"hash_keys"`
}

// WriteDefaultConf will create a default configuration file and set the storage type to "memory"
Expand Down
4 changes: 2 additions & 2 deletions session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type SessionLimiter struct{}
// Key values to manage rate are Rate and Per, e.g. Rate of 10 messages Per 10 seconds
func (l SessionLimiter) ForwardMessage(currentSession *SessionState, key string, store StorageHandler) (bool, int) {

rateLimiterKey := RateLimitKeyPrefix + key
rateLimiterKey := RateLimitKeyPrefix + getHash(key)
ratePerPeriodNow := store.IncrememntWithExpire(rateLimiterKey, int64(currentSession.Per))

if ratePerPeriodNow > (int64(currentSession.Rate)) {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (l SessionLimiter) IsRedisQuotaExceeded(currentSession *SessionState, key s
}

// Create the key
rawKey := QuotaKeyPrefix + key
rawKey := QuotaKeyPrefix + getHash(key)

// INCR the key (If it equals 1 - set EXPIRE)
qInt := store.IncrememntWithExpire(rawKey, currentSession.QuotaRenewalRate)
Expand Down
4 changes: 4 additions & 0 deletions storage_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func (r *RedisStorageManager) Connect() bool {
}

func getHash(in string) string {
// if !config.HashKeys {
// Not hashing? Return the raw key
// return in
// }
var h128 murmur3.Hash128 = murmur3.New128()
h128.Write([]byte(in))
return hex.EncodeToString(h128.Sum(nil))
Expand Down
Loading

0 comments on commit cd52dd1

Please sign in to comment.