Last active
August 29, 2015 14:06
-
-
Save gdm85/d3fca988e4314fc3bd3c to your computer and use it in GitHub Desktop.
Temptative patch to troubleshoot container id collision issue (updated at 15:15 UTC to add a mutex)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/utils/utils.go b/utils/utils.go | |
index da6854b..0eb283a 100644 | |
--- a/utils/utils.go | |
+++ b/utils/utils.go | |
@@ -2,12 +2,12 @@ package utils | |
import ( | |
"bytes" | |
- "crypto/rand" | |
"crypto/sha1" | |
"crypto/sha256" | |
"encoding/hex" | |
"fmt" | |
"io" | |
+ "bufio" | |
"io/ioutil" | |
"net/http" | |
"os" | |
@@ -244,7 +244,7 @@ func GetTotalUsedFds() int { | |
// TruncateID returns a shorthand version of a string identifier for convenience. | |
// A collision with other shorthands is very unlikely, but possible. | |
// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller | |
-// will need to use a langer prefix, or the full-length Id. | |
+// will need to use a larger prefix, or the full-length Id. | |
func TruncateID(id string) string { | |
shortLen := 12 | |
if len(id) < shortLen { | |
@@ -253,16 +253,37 @@ func TruncateID(id string) string { | |
return id[:shortLen] | |
} | |
+var urandomMutex sync.Mutex | |
+ | |
+func ReadRandomData(amount int) ([]byte, error) { | |
+ urandomMutex.Lock() | |
+ defer urandomMutex.Unlock() | |
+ file, err := os.Open("/dev/urandom") | |
+ if err != nil { | |
+ return nil, err | |
+ } | |
+ defer file.Close() | |
+ | |
+ bytes := make([]byte, amount) | |
+ bufr := bufio.NewReader(file) | |
+ _,err = bufr.Read(bytes) | |
+ if err != nil { | |
+ return nil, err | |
+ } | |
+ | |
+ return bytes, nil | |
+} | |
+ | |
// GenerateRandomID returns an unique id | |
func GenerateRandomID() string { | |
for { | |
- id := make([]byte, 32) | |
- if _, err := io.ReadFull(rand.Reader, id); err != nil { | |
+ id, err := ReadRandomData(32); | |
+ if err != nil { | |
panic(err) // This shouldn't happen | |
} | |
value := hex.EncodeToString(id) | |
// if we try to parse the truncated for as an int and we don't have | |
- // an error then the value is all numberic and causes issues when | |
+ // an error then the value is all numeric and causes issues when | |
// used as a hostname. ref #3869 | |
if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil { | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could this just be a multi-thread issue and you need to take a thread lock?