Merge pull request #6025 from crosbymichael/concurrent-names

Improve name generation on concurrent requests
This commit is contained in:
unclejack 2014-05-27 23:18:19 +03:00
commit eb075733c5
2 changed files with 10 additions and 39 deletions

View file

@ -6,10 +6,6 @@ import (
"time" "time"
) )
type NameChecker interface {
Exists(name string) bool
}
var ( var (
left = [...]string{"happy", "jolly", "dreamy", "sad", "angry", "pensive", "focused", "sleepy", "grave", "distracted", "determined", "stoic", "stupefied", "sharp", "agitated", "cocky", "tender", "goofy", "furious", "desperate", "hopeful", "compassionate", "silly", "lonely", "condescending", "naughty", "kickass", "drunk", "boring", "nostalgic", "ecstatic", "insane", "cranky", "mad", "jovial", "sick", "hungry", "thirsty", "elegant", "backstabbing", "clever", "trusting", "loving", "suspicious", "berserk", "high", "romantic", "prickly", "evil"} left = [...]string{"happy", "jolly", "dreamy", "sad", "angry", "pensive", "focused", "sleepy", "grave", "distracted", "determined", "stoic", "stupefied", "sharp", "agitated", "cocky", "tender", "goofy", "furious", "desperate", "hopeful", "compassionate", "silly", "lonely", "condescending", "naughty", "kickass", "drunk", "boring", "nostalgic", "ecstatic", "insane", "cranky", "mad", "jovial", "sick", "hungry", "thirsty", "elegant", "backstabbing", "clever", "trusting", "loving", "suspicious", "berserk", "high", "romantic", "prickly", "evil"}
// Docker 0.7.x generates names from notable scientists and hackers. // Docker 0.7.x generates names from notable scientists and hackers.
@ -79,16 +75,17 @@ var (
right = [...]string{"lovelace", "franklin", "tesla", "einstein", "bohr", "davinci", "pasteur", "nobel", "curie", "darwin", "turing", "ritchie", "torvalds", "pike", "thompson", "wozniak", "galileo", "euclid", "newton", "fermat", "archimedes", "poincare", "heisenberg", "feynman", "hawking", "fermi", "pare", "mccarthy", "engelbart", "babbage", "albattani", "ptolemy", "bell", "wright", "lumiere", "morse", "mclean", "brown", "bardeen", "brattain", "shockley", "goldstine", "hoover", "hopper", "bartik", "sammet", "jones", "perlman", "wilson", "kowalevski", "hypatia", "goodall", "mayer", "elion", "blackwell", "lalande", "kirch", "ardinghelli", "colden", "almeida", "leakey", "meitner", "mestorf", "rosalind", "sinoussi", "carson", "mcclintock", "yonath"} right = [...]string{"lovelace", "franklin", "tesla", "einstein", "bohr", "davinci", "pasteur", "nobel", "curie", "darwin", "turing", "ritchie", "torvalds", "pike", "thompson", "wozniak", "galileo", "euclid", "newton", "fermat", "archimedes", "poincare", "heisenberg", "feynman", "hawking", "fermi", "pare", "mccarthy", "engelbart", "babbage", "albattani", "ptolemy", "bell", "wright", "lumiere", "morse", "mclean", "brown", "bardeen", "brattain", "shockley", "goldstine", "hoover", "hopper", "bartik", "sammet", "jones", "perlman", "wilson", "kowalevski", "hypatia", "goodall", "mayer", "elion", "blackwell", "lalande", "kirch", "ardinghelli", "colden", "almeida", "leakey", "meitner", "mestorf", "rosalind", "sinoussi", "carson", "mcclintock", "yonath"}
) )
func GenerateRandomName(checker NameChecker) (string, error) { func GetRandomName(retry int) string {
retry := 5
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
begin:
name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))]) name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))])
for checker != nil && checker.Exists(name) && retry > 0 || name == "boring_wozniak" /* Steve Wozniak is not boring */ { if name == "boring_wozniak" /* Steve Wozniak is not boring */ {
goto begin
}
if retry > 0 {
name = fmt.Sprintf("%s%d", name, rand.Intn(10)) name = fmt.Sprintf("%s%d", name, rand.Intn(10))
retry = retry - 1
} }
if retry == 0 { return name
return name, fmt.Errorf("Error generating random name")
}
return name, nil
} }

View file

@ -4,35 +4,9 @@ import (
"testing" "testing"
) )
type FalseChecker struct{}
func (n *FalseChecker) Exists(name string) bool {
return false
}
type TrueChecker struct{}
func (n *TrueChecker) Exists(name string) bool {
return true
}
func TestGenerateRandomName(t *testing.T) {
if _, err := GenerateRandomName(&FalseChecker{}); err != nil {
t.Error(err)
}
if _, err := GenerateRandomName(&TrueChecker{}); err == nil {
t.Error("An error was expected")
}
}
// Make sure the generated names are awesome // Make sure the generated names are awesome
func TestGenerateAwesomeNames(t *testing.T) { func TestGenerateAwesomeNames(t *testing.T) {
name, err := GenerateRandomName(&FalseChecker{}) name := GetRandomName(0)
if err != nil {
t.Error(err)
}
if !isAwesome(name) { if !isAwesome(name) {
t.Fatalf("Generated name '%s' is not awesome.", name) t.Fatalf("Generated name '%s' is not awesome.", name)
} }