Browse code

Merge pull request #33097 from stevvooe/remove-random-pkg

pkg: remove random package

Brian Goff authored on 2017/06/21 05:43:38
Showing 5 changed files
... ...
@@ -2,8 +2,7 @@ package namesgenerator
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-
6
-	"github.com/docker/docker/pkg/random"
5
+	"math/rand"
7 6
 )
8 7
 
9 8
 var (
... ...
@@ -594,15 +593,14 @@ var (
594 594
 // formatted as "adjective_surname". For example 'focused_turing'. If retry is non-zero, a random
595 595
 // integer between 0 and 10 will be added to the end of the name, e.g `focused_turing3`
596 596
 func GetRandomName(retry int) string {
597
-	rnd := random.Rand
598 597
 begin:
599
-	name := fmt.Sprintf("%s_%s", left[rnd.Intn(len(left))], right[rnd.Intn(len(right))])
598
+	name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))])
600 599
 	if name == "boring_wozniak" /* Steve Wozniak is not boring */ {
601 600
 		goto begin
602 601
 	}
603 602
 
604 603
 	if retry > 0 {
605
-		name = fmt.Sprintf("%s%d", name, rnd.Intn(10))
604
+		name = fmt.Sprintf("%s%d", name, rand.Intn(10))
606 605
 	}
607 606
 	return name
608 607
 }
609 608
deleted file mode 100644
... ...
@@ -1,71 +0,0 @@
1
-package random
2
-
3
-import (
4
-	cryptorand "crypto/rand"
5
-	"io"
6
-	"math"
7
-	"math/big"
8
-	"math/rand"
9
-	"sync"
10
-	"time"
11
-)
12
-
13
-// Rand is a global *rand.Rand instance, which initialized with NewSource() source.
14
-var Rand = rand.New(NewSource())
15
-
16
-// Reader is a global, shared instance of a pseudorandom bytes generator.
17
-// It doesn't consume entropy.
18
-var Reader io.Reader = &reader{rnd: Rand}
19
-
20
-// copypaste from standard math/rand
21
-type lockedSource struct {
22
-	lk  sync.Mutex
23
-	src rand.Source
24
-}
25
-
26
-func (r *lockedSource) Int63() (n int64) {
27
-	r.lk.Lock()
28
-	n = r.src.Int63()
29
-	r.lk.Unlock()
30
-	return
31
-}
32
-
33
-func (r *lockedSource) Seed(seed int64) {
34
-	r.lk.Lock()
35
-	r.src.Seed(seed)
36
-	r.lk.Unlock()
37
-}
38
-
39
-// NewSource returns math/rand.Source safe for concurrent use and initialized
40
-// with current unix-nano timestamp
41
-func NewSource() rand.Source {
42
-	var seed int64
43
-	if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil {
44
-		// This should not happen, but worst-case fallback to time-based seed.
45
-		seed = time.Now().UnixNano()
46
-	} else {
47
-		seed = cryptoseed.Int64()
48
-	}
49
-	return &lockedSource{
50
-		src: rand.NewSource(seed),
51
-	}
52
-}
53
-
54
-type reader struct {
55
-	rnd *rand.Rand
56
-}
57
-
58
-func (r *reader) Read(b []byte) (int, error) {
59
-	i := 0
60
-	for {
61
-		val := r.rnd.Int63()
62
-		for val > 0 {
63
-			b[i] = byte(val)
64
-			i++
65
-			if i == len(b) {
66
-				return i, nil
67
-			}
68
-			val >>= 8
69
-		}
70
-	}
71
-}
72 1
deleted file mode 100644
... ...
@@ -1,22 +0,0 @@
1
-package random
2
-
3
-import (
4
-	"math/rand"
5
-	"sync"
6
-	"testing"
7
-)
8
-
9
-// for go test -v -race
10
-func TestConcurrency(t *testing.T) {
11
-	rnd := rand.New(NewSource())
12
-	var wg sync.WaitGroup
13
-
14
-	for i := 0; i < 10; i++ {
15
-		wg.Add(1)
16
-		go func() {
17
-			rnd.Int63()
18
-			wg.Done()
19
-		}()
20
-	}
21
-	wg.Wait()
22
-}
... ...
@@ -2,15 +2,17 @@
2 2
 package stringid
3 3
 
4 4
 import (
5
-	"crypto/rand"
5
+	cryptorand "crypto/rand"
6 6
 	"encoding/hex"
7 7
 	"fmt"
8 8
 	"io"
9
+	"math"
10
+	"math/big"
11
+	"math/rand"
9 12
 	"regexp"
10 13
 	"strconv"
11 14
 	"strings"
12
-
13
-	"github.com/docker/docker/pkg/random"
15
+	"time"
14 16
 )
15 17
 
16 18
 const shortLen = 12
... ...
@@ -39,12 +41,8 @@ func TruncateID(id string) string {
39 39
 	return id
40 40
 }
41 41
 
42
-func generateID(crypto bool) string {
42
+func generateID(r io.Reader) string {
43 43
 	b := make([]byte, 32)
44
-	r := random.Reader
45
-	if crypto {
46
-		r = rand.Reader
47
-	}
48 44
 	for {
49 45
 		if _, err := io.ReadFull(r, b); err != nil {
50 46
 			panic(err) // This shouldn't happen
... ...
@@ -62,14 +60,14 @@ func generateID(crypto bool) string {
62 62
 
63 63
 // GenerateRandomID returns a unique id.
64 64
 func GenerateRandomID() string {
65
-	return generateID(true)
65
+	return generateID(cryptorand.Reader)
66 66
 }
67 67
 
68 68
 // GenerateNonCryptoID generates unique id without using cryptographically
69 69
 // secure sources of random.
70 70
 // It helps you to save entropy.
71 71
 func GenerateNonCryptoID() string {
72
-	return generateID(false)
72
+	return generateID(readerFunc(rand.Read))
73 73
 }
74 74
 
75 75
 // ValidateID checks whether an ID string is a valid image ID.
... ...
@@ -79,3 +77,23 @@ func ValidateID(id string) error {
79 79
 	}
80 80
 	return nil
81 81
 }
82
+
83
+func init() {
84
+	// safely set the seed globally so we generate random ids. Tries to use a
85
+	// crypto seed before falling back to time.
86
+	var seed int64
87
+	if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil {
88
+		// This should not happen, but worst-case fallback to time-based seed.
89
+		seed = time.Now().UnixNano()
90
+	} else {
91
+		seed = cryptoseed.Int64()
92
+	}
93
+
94
+	rand.Seed(seed)
95
+}
96
+
97
+type readerFunc func(p []byte) (int, error)
98
+
99
+func (fn readerFunc) Read(p []byte) (int, error) {
100
+	return fn(p)
101
+}
... ...
@@ -5,8 +5,6 @@ import (
5 5
 	"bytes"
6 6
 	"math/rand"
7 7
 	"strings"
8
-
9
-	"github.com/docker/docker/pkg/random"
10 8
 )
11 9
 
12 10
 // GenerateRandomAlphaOnlyString generates an alphabetical random string with length n.
... ...
@@ -15,7 +13,7 @@ func GenerateRandomAlphaOnlyString(n int) string {
15 15
 	letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
16 16
 	b := make([]byte, n)
17 17
 	for i := range b {
18
-		b[i] = letters[random.Rand.Intn(len(letters))]
18
+		b[i] = letters[rand.Intn(len(letters))]
19 19
 	}
20 20
 	return string(b)
21 21
 }