forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.go
57 lines (44 loc) · 821 Bytes
/
room.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package room
import (
"math/rand"
"sync"
"time"
"github.com/yearnfar/nanoserver/db"
)
const (
roomNoLen = 6
)
type Number string
type numberManager struct {
lock sync.Mutex
}
var rn *numberManager
var numbers = [...]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
func init() {
rn = &numberManager{}
rand.Seed(time.Now().Unix())
}
func (rn *numberManager) next() Number {
no := make([]byte, roomNoLen)
rn.lock.Lock()
defer rn.lock.Unlock()
for {
for i := 0; i < roomNoLen; i++ {
no[i] = numbers[rand.Intn(10)]
}
temp := Number(no)
dn := string(no)
if !db.DeskNumberExists(dn) {
return temp
}
}
}
func (rn *numberManager) remove(no Number) {
//rn.noPool.Remove(string(no))
}
func Next() Number {
return rn.next()
}
func (n Number) String() string {
return string(n)
}