Skip to content

Commit 384a4a6

Browse files
[FAB-2649] Concurrent access to viper
Running gossip discovery tests in parallel rarelly results in concurrent access to viper and concurrent map read and map write error. This CS fix this by protecting access by lock Change-Id: I02db2cc1c5565008de316250850492e2f98af55e Signed-off-by: Gennady Laventman <[email protected]>
1 parent 1e9a087 commit 384a4a6

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

gossip/discovery/discovery_impl.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/hyperledger/fabric/gossip/util"
3232
proto "github.com/hyperledger/fabric/protos/gossip"
3333
"github.com/op/go-logging"
34-
"github.com/spf13/viper"
3534
)
3635

3736
const defaultHelloInterval = time.Duration(5) * time.Second
@@ -42,12 +41,12 @@ var maxConnectionAttempts = 120
4241

4342
// SetAliveTimeInterval sets the alive time interval
4443
func SetAliveTimeInterval(interval time.Duration) {
45-
viper.Set("peer.gossip.aliveTimeInterval", interval)
44+
util.SetDuration("peer.gossip.aliveTimeInterval", interval)
4645
}
4746

4847
// SetAliveExpirationTimeout sets the expiration timeout
4948
func SetAliveExpirationTimeout(timeout time.Duration) {
50-
viper.Set("peer.gossip.aliveExpirationTimeout", timeout)
49+
util.SetDuration("peer.gossip.aliveExpirationTimeout", timeout)
5150
aliveExpirationCheckInterval = time.Duration(timeout / 10)
5251
}
5352

@@ -58,7 +57,7 @@ func SetAliveExpirationCheckInterval(interval time.Duration) {
5857

5958
// SetReconnectInterval sets the reconnect interval
6059
func SetReconnectInterval(interval time.Duration) {
61-
viper.Set("peer.gossip.reconnectInterval", interval)
60+
util.SetDuration("peer.gossip.reconnectInterval", interval)
6261
}
6362

6463
// SetMaxConnAttempts sets the maximum number of connection

gossip/util/misc.go

+15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
// Equals returns whether a and b are the same
3434
type Equals func(a interface{}, b interface{}) bool
3535

36+
var viperLock sync.RWMutex
37+
3638
// IndexInSlice returns the index of given object o in array
3739
func IndexInSlice(array interface{}, o interface{}, equals Equals) int {
3840
arr := reflect.ValueOf(array)
@@ -145,6 +147,9 @@ func PrintStackTrace() {
145147

146148
// GetIntOrDefault returns the int value from config if present otherwise default value
147149
func GetIntOrDefault(key string, defVal int) int {
150+
viperLock.RLock()
151+
defer viperLock.RUnlock()
152+
148153
if val := viper.GetInt(key); val != 0 {
149154
return val
150155
}
@@ -154,13 +159,23 @@ func GetIntOrDefault(key string, defVal int) int {
154159

155160
// GetDurationOrDefault returns the Duration value from config if present otherwise default value
156161
func GetDurationOrDefault(key string, defVal time.Duration) time.Duration {
162+
viperLock.RLock()
163+
defer viperLock.RUnlock()
164+
157165
if val := viper.GetDuration(key); val != 0 {
158166
return val
159167
}
160168

161169
return defVal
162170
}
163171

172+
// SetDuration stores duration key value to viper
173+
func SetDuration(key string, val time.Duration) {
174+
viperLock.Lock()
175+
defer viperLock.Unlock()
176+
viper.Set(key, val)
177+
}
178+
164179
// RandomInt returns, as an int, a non-negative pseudo-random integer in [0,n)
165180
// It panics if n <= 0
166181
func RandomInt(n int) int {

0 commit comments

Comments
 (0)