diff --git a/changelogs/unreleased/jv-simplify-s3-sessions.yml b/changelogs/unreleased/jv-simplify-s3-sessions.yml new file mode 100644 index 0000000000000000000000000000000000000000..8984cb448fd035a3c3c70aadf583dd282128d7d7 --- /dev/null +++ b/changelogs/unreleased/jv-simplify-s3-sessions.yml @@ -0,0 +1,5 @@ +--- +title: Simplify s3 session management code +merge_request: 616 +author: +type: other diff --git a/internal/objectstore/s3_session.go b/internal/objectstore/s3_session.go index c9b4e8c61db4c02e3230806e0e2c6143591099ff..ebc8daf534c043f5bd816f5da0214e5303b620a3 100644 --- a/internal/objectstore/s3_session.go +++ b/internal/objectstore/s3_session.go @@ -35,9 +35,7 @@ func (s *s3Session) isExpired() bool { } func newS3SessionCache() *s3SessionCache { - cache := &s3SessionCache{sessions: make(map[config.S3Config]*s3Session)} - - return cache + return &s3SessionCache{sessions: make(map[config.S3Config]*s3Session)} } var ( @@ -57,13 +55,8 @@ func setupS3Session(s3Credentials config.S3Credentials, s3Config config.S3Config sessionCache.Lock() defer sessionCache.Unlock() - s, ok := sessionCache.sessions[s3Config] - - if !ok { - s = &s3Session{} - sessionCache.sessions[s3Config] = s - } else if s.session != nil && !s.isExpired() { - return s.session.Copy(), nil + if s, ok := sessionCache.sessions[s3Config]; ok && !s.isExpired() { + return s.session, nil } cfg := &aws.Config{ @@ -85,18 +78,17 @@ func setupS3Session(s3Credentials config.S3Credentials, s3Config config.S3Config return nil, err } - s.expiry = time.Now().Add(sessionExpiration) - s.session = sess + sessionCache.sessions[s3Config] = &s3Session{ + expiry: time.Now().Add(sessionExpiration), + session: sess, + } - return sess.Copy(), nil + return sess, nil } func ResetS3Session(s3Config config.S3Config) { sessionCache.Lock() defer sessionCache.Unlock() - s, ok := sessionCache.sessions[s3Config] - if ok { - s.session = nil - } + delete(sessionCache.sessions, s3Config) }