Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #47 from magneticio/feature/inmemorytokenstor-shou…
Browse files Browse the repository at this point in the history
…ld-be-conncurrent

Added rw mutex to the in imemory token store
  • Loading branch information
bgokden authored Jul 15, 2019
2 parents 7c0824e + eb232d9 commit aedcfc8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion client/tokenstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package client

import (
"io/ioutil"
"sync"
"time"

"github.com/magneticio/vampkubistcli/logging"
Expand Down Expand Up @@ -138,43 +139,61 @@ func (ts *FileBackedTokenStore) Tokens() map[string]int64 {

type InMemoryTokenStore struct {
tokenMap map[string]int64
rwlock sync.RWMutex
}

func (ts *InMemoryTokenStore) Store(token string, timeout int64) error {
ts.rwlock.Lock()
if ts.tokenMap == nil {
ts.tokenMap = make(map[string]int64)
}
ts.tokenMap[token] = timeout
ts.rwlock.Unlock()
return nil
}

func (ts *InMemoryTokenStore) Get(token string) (int64, bool) {
ts.rwlock.RLock()
if ts.tokenMap == nil {
ts.rwlock.RUnlock()
return 0, false
}
if timeout, ok := ts.tokenMap[token]; ok {
ts.rwlock.RUnlock()
return timeout, true
}
ts.rwlock.RUnlock()
return 0, false
}

func (ts *InMemoryTokenStore) Tokens() map[string]int64 {
return ts.tokenMap
ts.rwlock.RLock()
tokenMapCopy := make(map[string]int64)
for key, value := range ts.tokenMap {
tokenMapCopy[key] = value
}
ts.rwlock.RUnlock()
return tokenMapCopy
}

func (ts *InMemoryTokenStore) RemoveExpired() error {
ts.rwlock.Lock()
if ts.tokenMap == nil {
ts.rwlock.Unlock()
return nil
}
for token, timeout := range ts.tokenMap {
if time.Now().Unix() >= timeout {
delete(ts.tokenMap, token)
}
}
ts.rwlock.Unlock()
return nil
}

func (ts *InMemoryTokenStore) Clean() error {
ts.rwlock.Lock()
ts.tokenMap = make(map[string]int64)
ts.rwlock.Unlock()
return nil
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var kubeConfigPath string
var TokenStore client.TokenStore

// version should be in format d.d.d where d is a decimal number
const Version string = "v0.0.41"
const Version string = "v0.0.42"

var AppName string = InitAppName()

Expand Down

0 comments on commit aedcfc8

Please sign in to comment.