Add random nonce to test ipset name to prevent race
As ipset.New(...) can be called by two processes (weaver and weave-npc) at the same time, there is a possibility for a race when they both check the comment support. To prevent the race, we append a random nonce to the test ipset name.
This commit is contained in:
1 parent
b6115f0147
commit
2f8aa7a351
1 file changed
+10
-1
+10
-1
@@ -1,6 +1,8 @@
|
|||||||
package ipset
|
package ipset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -56,7 +58,14 @@ func New(logger *log.Logger) Interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for comment support
|
// Check for comment support
|
||||||
testIpsetName := Name("weave-test-comment")
|
|
||||||
|
// To prevent from a race when more than one process check for the support
|
||||||
|
// we append a random nonce to the test ipset name. The final name is
|
||||||
|
// shorter than 31 chars (max ipset name).
|
||||||
|
nonce := make([]byte, 4)
|
||||||
|
rand.Read(nonce)
|
||||||
|
testIpsetName := Name("weave-test-comment" + hex.EncodeToString(nonce))
|
||||||
|
|
||||||
// Clear it out if it already exists
|
// Clear it out if it already exists
|
||||||
_ = ips.Destroy(testIpsetName)
|
_ = ips.Destroy(testIpsetName)
|
||||||
// Test for comment support
|
// Test for comment support
|
||||||
|
|||||||
Reference in new issue
Block a user