Browse code

libnetwork/datastore: un-embed mutex from cache

Signed-off-by: Cory Snider <csnider@mirantis.com>

Cory Snider authored on 2023/10/20 00:44:47
Showing 1 changed files
... ...
@@ -10,7 +10,7 @@ import (
10 10
 type kvMap map[string]KVObject
11 11
 
12 12
 type cache struct {
13
-	sync.Mutex
13
+	mu  sync.Mutex
14 14
 	kmm map[string]kvMap
15 15
 	ds  store.Store
16 16
 }
... ...
@@ -22,10 +22,10 @@ func newCache(ds store.Store) *cache {
22 22
 func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
23 23
 	var err error
24 24
 
25
-	c.Lock()
25
+	c.mu.Lock()
26 26
 	keyPrefix := Key(kvObject.KeyPrefix()...)
27 27
 	kmap, ok := c.kmm[keyPrefix]
28
-	c.Unlock()
28
+	c.mu.Unlock()
29 29
 
30 30
 	if ok {
31 31
 		return kmap, nil
... ...
@@ -67,15 +67,15 @@ out:
67 67
 	// There may multiple go routines racing to fill the
68 68
 	// cache. The one which places the kmap in c.kmm first
69 69
 	// wins. The others should just use what the first populated.
70
-	c.Lock()
70
+	c.mu.Lock()
71 71
 	kmapNew, ok := c.kmm[keyPrefix]
72 72
 	if ok {
73
-		c.Unlock()
73
+		c.mu.Unlock()
74 74
 		return kmapNew, nil
75 75
 	}
76 76
 
77 77
 	c.kmm[keyPrefix] = kmap
78
-	c.Unlock()
78
+	c.mu.Unlock()
79 79
 
80 80
 	return kmap, nil
81 81
 }
... ...
@@ -86,13 +86,13 @@ func (c *cache) add(kvObject KVObject, atomic bool) error {
86 86
 		return err
87 87
 	}
88 88
 
89
-	c.Lock()
89
+	c.mu.Lock()
90 90
 	// If atomic is true, cache needs to maintain its own index
91 91
 	// for atomicity and the add needs to be atomic.
92 92
 	if atomic {
93 93
 		if prev, ok := kmap[Key(kvObject.Key()...)]; ok {
94 94
 			if prev.Index() != kvObject.Index() {
95
-				c.Unlock()
95
+				c.mu.Unlock()
96 96
 				return ErrKeyModified
97 97
 			}
98 98
 		}
... ...
@@ -104,7 +104,7 @@ func (c *cache) add(kvObject KVObject, atomic bool) error {
104 104
 	}
105 105
 
106 106
 	kmap[Key(kvObject.Key()...)] = kvObject
107
-	c.Unlock()
107
+	c.mu.Unlock()
108 108
 	return nil
109 109
 }
110 110
 
... ...
@@ -114,20 +114,20 @@ func (c *cache) del(kvObject KVObject, atomic bool) error {
114 114
 		return err
115 115
 	}
116 116
 
117
-	c.Lock()
117
+	c.mu.Lock()
118 118
 	// If atomic is true, cache needs to maintain its own index
119 119
 	// for atomicity and del needs to be atomic.
120 120
 	if atomic {
121 121
 		if prev, ok := kmap[Key(kvObject.Key()...)]; ok {
122 122
 			if prev.Index() != kvObject.Index() {
123
-				c.Unlock()
123
+				c.mu.Unlock()
124 124
 				return ErrKeyModified
125 125
 			}
126 126
 		}
127 127
 	}
128 128
 
129 129
 	delete(kmap, Key(kvObject.Key()...))
130
-	c.Unlock()
130
+	c.mu.Unlock()
131 131
 	return nil
132 132
 }
133 133
 
... ...
@@ -137,8 +137,8 @@ func (c *cache) get(kvObject KVObject) error {
137 137
 		return err
138 138
 	}
139 139
 
140
-	c.Lock()
141
-	defer c.Unlock()
140
+	c.mu.Lock()
141
+	defer c.mu.Unlock()
142 142
 
143 143
 	o, ok := kmap[Key(kvObject.Key()...)]
144 144
 	if !ok {
... ...
@@ -154,8 +154,8 @@ func (c *cache) list(kvObject KVObject) ([]KVObject, error) {
154 154
 		return nil, err
155 155
 	}
156 156
 
157
-	c.Lock()
158
-	defer c.Unlock()
157
+	c.mu.Lock()
158
+	defer c.mu.Unlock()
159 159
 
160 160
 	var kvol []KVObject
161 161
 	for _, v := range kmap {