summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>2018-07-25 21:33:26 +0200
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>2018-10-30 15:25:23 +0100
commit5c798f46a106d2c42dbc0d2d5ee10edcb2320a1d (patch)
tree7892dbd830bcbb77d40928cc6b40516e1e527c9d
parenta744e73f4fca743e4ee426604b6b6e34dfc6ede2 (diff)
downloadslav-5c798f46a106d2c42dbc0d2d5ee10edcb2320a1d.tar.gz
slav-5c798f46a106d2c42dbc0d2d5ee10edcb2320a1d.tar.bz2
slav-5c798f46a106d2c42dbc0d2d5ee10edcb2320a1d.zip
Use atomic operations for threshold protection
atomic.StoreUint32 and atomic.LoadUint32 are used for accessing threshold level in Logger. They are much faster than mutexes. Change-Id: Idd4db0ab2df1e5d783abc13c0b29c7560a1e0734 Signed-off-by: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
-rw-r--r--logger/logger.go13
-rw-r--r--logger/logger_test.go4
2 files changed, 5 insertions, 12 deletions
diff --git a/logger/logger.go b/logger/logger.go
index 7a2775c..c31bb1b 100644
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -21,6 +21,7 @@ import (
"fmt"
"os"
"sync"
+ "sync/atomic"
)
const (
@@ -59,24 +60,18 @@ func (l *Logger) SetThreshold(level Level) error {
return ErrInvalidLogLevel
}
- l.mutex.Lock()
- defer l.mutex.Unlock()
- l.threshold = level
+ atomic.StoreUint32((*uint32)(&l.threshold), uint32(level))
return nil
}
// Threshold returns current Logger's filter level.
func (l *Logger) Threshold() Level {
- l.mutex.Lock()
- defer l.mutex.Unlock()
- return l.threshold
+ return Level(atomic.LoadUint32((*uint32)(&l.threshold)))
}
// PassThreshold verifies if message with given level passes threshold and should be logged.
func (l *Logger) PassThreshold(level Level) bool {
- l.mutex.Lock()
- defer l.mutex.Unlock()
- return (level <= l.threshold)
+ return (level <= l.Threshold())
}
// AddBackend adds or replaces a backend with given name.
diff --git a/logger/logger_test.go b/logger/logger_test.go
index b3554f3..85efc65 100644
--- a/logger/logger_test.go
+++ b/logger/logger_test.go
@@ -93,9 +93,9 @@ var _ = Describe("Logger", func() {
It("should create a new default logger object", func() {
Expect(L).NotTo(BeNil())
Expect(L.mutex).NotTo(BeNil())
+ Expect(L.threshold).To(Equal(DefaultThreshold))
L.mutex.Lock()
defer L.mutex.Unlock()
- Expect(L.threshold).To(Equal(DefaultThreshold))
Expect(L.backends).To(BeEmpty())
})
})
@@ -105,8 +105,6 @@ var _ = Describe("Logger", func() {
func(level Level) {
err := L.SetThreshold(level)
Expect(err).NotTo(HaveOccurred())
- L.mutex.Lock()
- defer L.mutex.Unlock()
Expect(L.threshold).To(Equal(level))
},
T.Entry("EmergLevel", EmergLevel),