forked from crowdsecurity/crowdsec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract revocationCache struct with mutex
- Loading branch information
Showing
2 changed files
with
65 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package v1 | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type cacheEntry struct { | ||
revoked bool | ||
timestamp time.Time | ||
} | ||
|
||
type RevocationCache struct { | ||
mu sync.RWMutex | ||
cache map[string]cacheEntry | ||
expiration time.Duration | ||
} | ||
|
||
func NewRevocationCache(expiration time.Duration) *RevocationCache { | ||
return &RevocationCache{ | ||
cache: make(map[string]cacheEntry), | ||
expiration: expiration, | ||
} | ||
} | ||
|
||
func (rc *RevocationCache) Get(sn string, logger *log.Entry) (bool, bool) { | ||
rc.mu.RLock() | ||
entry, exists := rc.cache[sn] | ||
rc.mu.RUnlock() | ||
|
||
if !exists { | ||
logger.Tracef("TLSAuth: no cached value for cert %s", sn) | ||
return false, false | ||
} | ||
|
||
rc.mu.Lock() | ||
if entry.timestamp.Add(rc.expiration).Before(time.Now()) { | ||
logger.Debugf("TLSAuth: cached value for %s expired, removing from cache", sn) | ||
delete(rc.cache, sn) | ||
rc.mu.Unlock() | ||
|
||
return false, false | ||
} | ||
rc.mu.Unlock() | ||
|
||
logger.Debugf("TLSAuth: using cached value for cert %s: %t", sn, entry.revoked) | ||
|
||
return entry.revoked, true | ||
} | ||
|
||
func (rc *RevocationCache) Set(sn string, revoked bool) { | ||
rc.mu.Lock() | ||
rc.cache[sn] = cacheEntry{ | ||
revoked: revoked, | ||
timestamp: time.Now(), | ||
} | ||
rc.mu.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters