Replace Mutex with RWMutex

This commit is contained in:
Carlos Quintana 2021-10-19 08:09:59 +02:00
parent 9f25490f61
commit c406b182bb
No known key found for this signature in database
GPG key ID: 68324E212E167F8E
2 changed files with 6 additions and 6 deletions

View file

@ -8,13 +8,13 @@ import "sync"
type GopenPGP struct { type GopenPGP struct {
latestServerTime int64 latestServerTime int64
generationOffset int64 generationOffset int64
lock *sync.Mutex lock *sync.RWMutex
} }
var pgp = GopenPGP{ var pgp = GopenPGP{
latestServerTime: 0, latestServerTime: 0,
generationOffset: 0, generationOffset: 0,
lock: &sync.Mutex{}, lock: &sync.RWMutex{},
} }
// clone returns a clone of the byte slice. Internal function used to make sure // clone returns a clone of the byte slice. Internal function used to make sure

View file

@ -36,8 +36,8 @@ func GetTime() time.Time {
// getNow returns the latest server time. // getNow returns the latest server time.
func getNow() time.Time { func getNow() time.Time {
pgp.lock.Lock() pgp.lock.RLock()
defer pgp.lock.Unlock() defer pgp.lock.RUnlock()
if pgp.latestServerTime == 0 { if pgp.latestServerTime == 0 {
return time.Now() return time.Now()
@ -53,8 +53,8 @@ func getTimeGenerator() func() time.Time {
// getNowKeyGenerationOffset returns the current time with the key generation offset. // getNowKeyGenerationOffset returns the current time with the key generation offset.
func getNowKeyGenerationOffset() time.Time { func getNowKeyGenerationOffset() time.Time {
pgp.lock.Lock() pgp.lock.RLock()
defer pgp.lock.Unlock() defer pgp.lock.RUnlock()
if pgp.latestServerTime == 0 { if pgp.latestServerTime == 0 {
return time.Unix(time.Now().Unix()+pgp.generationOffset, 0) return time.Unix(time.Now().Unix()+pgp.generationOffset, 0)