-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
server_spec.go
81 lines (64 loc) · 1.34 KB
/
server_spec.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package protocol
import (
"sync"
"time"
"github.com/v2ray/v2ray-core/common/dice"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type ServerSpec struct {
sync.RWMutex
Destination v2net.Destination
users []*User
}
func NewServerSpec(dest v2net.Destination, users ...*User) *ServerSpec {
return &ServerSpec{
Destination: dest,
users: users,
}
}
func (this *ServerSpec) HasUser(user *User) bool {
this.RLock()
defer this.RUnlock()
account := user.Account
for _, u := range this.users {
if u.Account.Equals(account) {
return true
}
}
return false
}
func (this *ServerSpec) AddUser(user *User) {
if this.HasUser(user) {
return
}
this.Lock()
defer this.Unlock()
this.users = append(this.users, user)
}
func (this *ServerSpec) PickUser() *User {
userCount := len(this.users)
return this.users[dice.Roll(userCount)]
}
func (this *ServerSpec) IsValid() bool {
return true
}
func (this *ServerSpec) SetValid(b bool) {
}
type TimeoutServerSpec struct {
*ServerSpec
until time.Time
}
func NewTimeoutServerSpec(spec *ServerSpec, until time.Time) *TimeoutServerSpec {
return &TimeoutServerSpec{
ServerSpec: spec,
until: until,
}
}
func (this *TimeoutServerSpec) IsValid() bool {
return this.until.Before(time.Now())
}
func (this *TimeoutServerSpec) SetValid(b bool) {
if !b {
this.until = time.Time{}
}
}