Commit 3e0dfe95 authored by Jacob Vosmaer's avatar Jacob Vosmaer Committed by Nick Thomas

Simplify s3 session management code

parent 5a2a990f
---
title: Simplify s3 session management code
merge_request: 616
author:
type: other
...@@ -35,9 +35,7 @@ func (s *s3Session) isExpired() bool { ...@@ -35,9 +35,7 @@ func (s *s3Session) isExpired() bool {
} }
func newS3SessionCache() *s3SessionCache { func newS3SessionCache() *s3SessionCache {
cache := &s3SessionCache{sessions: make(map[config.S3Config]*s3Session)} return &s3SessionCache{sessions: make(map[config.S3Config]*s3Session)}
return cache
} }
var ( var (
...@@ -57,13 +55,8 @@ func setupS3Session(s3Credentials config.S3Credentials, s3Config config.S3Config ...@@ -57,13 +55,8 @@ func setupS3Session(s3Credentials config.S3Credentials, s3Config config.S3Config
sessionCache.Lock() sessionCache.Lock()
defer sessionCache.Unlock() defer sessionCache.Unlock()
s, ok := sessionCache.sessions[s3Config] if s, ok := sessionCache.sessions[s3Config]; ok && !s.isExpired() {
return s.session, nil
if !ok {
s = &s3Session{}
sessionCache.sessions[s3Config] = s
} else if s.session != nil && !s.isExpired() {
return s.session.Copy(), nil
} }
cfg := &aws.Config{ cfg := &aws.Config{
...@@ -85,18 +78,17 @@ func setupS3Session(s3Credentials config.S3Credentials, s3Config config.S3Config ...@@ -85,18 +78,17 @@ func setupS3Session(s3Credentials config.S3Credentials, s3Config config.S3Config
return nil, err return nil, err
} }
s.expiry = time.Now().Add(sessionExpiration) sessionCache.sessions[s3Config] = &s3Session{
s.session = sess expiry: time.Now().Add(sessionExpiration),
session: sess,
}
return sess.Copy(), nil return sess, nil
} }
func ResetS3Session(s3Config config.S3Config) { func ResetS3Session(s3Config config.S3Config) {
sessionCache.Lock() sessionCache.Lock()
defer sessionCache.Unlock() defer sessionCache.Unlock()
s, ok := sessionCache.sessions[s3Config] delete(sessionCache.sessions, s3Config)
if ok {
s.session = nil
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment