forked from aykevl/plaincast
/
rid.go
38 lines (31 loc) · 750 Bytes
/
rid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package youtube
import (
"math/rand"
"sync"
)
// RandomID implements the 'rid' field for the Channel API.
type RandomID struct {
mutex sync.Mutex
number int
}
// NewRandomID returns a new initialized RID counter.
func NewRandomID() *RandomID {
rid := &RandomID{}
rid.Restart()
return rid
}
// Restart resets the counter to a new random value (as if a new RID object was
// created).
func (rid *RandomID) Restart() {
rid.mutex.Lock()
defer rid.mutex.Unlock()
// this appears to be a random number between 10000-99999
rid.number = rand.Intn(80000) + 10000
}
// Next returns the next RID, incrementing the humber by one.
func (rid *RandomID) Next() int {
rid.mutex.Lock()
defer rid.mutex.Unlock()
rid.number++
return rid.number
}