diff --git a/common/randomid.go b/common/randomid.go new file mode 100644 index 0000000..5b89cdf --- /dev/null +++ b/common/randomid.go @@ -0,0 +1,38 @@ +package common + +import ( + "crypto/rand" + "encoding/hex" + "io" + "strconv" +) + +// 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. +func TruncateID(id string) string { + shortLen := 12 + if len(id) < shortLen { + shortLen = len(id) + } + return id[:shortLen] +} + +// GenerateRandomID returns an unique id +func GenerateRandomID() string { + for { + id := make([]byte, 32) + if _, err := io.ReadFull(rand.Reader, id); 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 + // used as a hostname. ref #3869 + if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil { + continue + } + return value + } +} diff --git a/truncindex/truncindex_test.go b/truncindex/truncindex_test.go index 8ad1634..9286534 100644 --- a/truncindex/truncindex_test.go +++ b/truncindex/truncindex_test.go @@ -4,7 +4,7 @@ import ( "math/rand" "testing" - "github.com/docker/docker/utils" + "github.com/docker/docker/pkg/common" ) // Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix. @@ -111,7 +111,7 @@ func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult strin func BenchmarkTruncIndexAdd100(b *testing.B) { var testSet []string for i := 0; i < 100; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -127,7 +127,7 @@ func BenchmarkTruncIndexAdd100(b *testing.B) { func BenchmarkTruncIndexAdd250(b *testing.B) { var testSet []string for i := 0; i < 250; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -143,7 +143,7 @@ func BenchmarkTruncIndexAdd250(b *testing.B) { func BenchmarkTruncIndexAdd500(b *testing.B) { var testSet []string for i := 0; i < 500; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -160,7 +160,7 @@ func BenchmarkTruncIndexGet100(b *testing.B) { var testSet []string var testKeys []string for i := 0; i < 100; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } index := NewTruncIndex([]string{}) for _, id := range testSet { @@ -184,7 +184,7 @@ func BenchmarkTruncIndexGet250(b *testing.B) { var testSet []string var testKeys []string for i := 0; i < 250; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } index := NewTruncIndex([]string{}) for _, id := range testSet { @@ -208,7 +208,7 @@ func BenchmarkTruncIndexGet500(b *testing.B) { var testSet []string var testKeys []string for i := 0; i < 500; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } index := NewTruncIndex([]string{}) for _, id := range testSet { @@ -231,7 +231,7 @@ func BenchmarkTruncIndexGet500(b *testing.B) { func BenchmarkTruncIndexDelete100(b *testing.B) { var testSet []string for i := 0; i < 100; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -254,7 +254,7 @@ func BenchmarkTruncIndexDelete100(b *testing.B) { func BenchmarkTruncIndexDelete250(b *testing.B) { var testSet []string for i := 0; i < 250; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -277,7 +277,7 @@ func BenchmarkTruncIndexDelete250(b *testing.B) { func BenchmarkTruncIndexDelete500(b *testing.B) { var testSet []string for i := 0; i < 500; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -300,7 +300,7 @@ func BenchmarkTruncIndexDelete500(b *testing.B) { func BenchmarkTruncIndexNew100(b *testing.B) { var testSet []string for i := 0; i < 100; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -311,7 +311,7 @@ func BenchmarkTruncIndexNew100(b *testing.B) { func BenchmarkTruncIndexNew250(b *testing.B) { var testSet []string for i := 0; i < 250; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -322,7 +322,7 @@ func BenchmarkTruncIndexNew250(b *testing.B) { func BenchmarkTruncIndexNew500(b *testing.B) { var testSet []string for i := 0; i < 500; i++ { - testSet = append(testSet, utils.GenerateRandomID()) + testSet = append(testSet, common.GenerateRandomID()) } b.ResetTimer() for i := 0; i < b.N; i++ { @@ -334,7 +334,7 @@ func BenchmarkTruncIndexAddGet100(b *testing.B) { var testSet []string var testKeys []string for i := 0; i < 500; i++ { - id := utils.GenerateRandomID() + id := common.GenerateRandomID() testSet = append(testSet, id) l := rand.Intn(12) + 12 testKeys = append(testKeys, id[:l]) @@ -359,7 +359,7 @@ func BenchmarkTruncIndexAddGet250(b *testing.B) { var testSet []string var testKeys []string for i := 0; i < 500; i++ { - id := utils.GenerateRandomID() + id := common.GenerateRandomID() testSet = append(testSet, id) l := rand.Intn(12) + 12 testKeys = append(testKeys, id[:l]) @@ -384,7 +384,7 @@ func BenchmarkTruncIndexAddGet500(b *testing.B) { var testSet []string var testKeys []string for i := 0; i < 500; i++ { - id := utils.GenerateRandomID() + id := common.GenerateRandomID() testSet = append(testSet, id) l := rand.Intn(12) + 12 testKeys = append(testKeys, id[:l])