pull/298/merge
Darien Raymond 2017-04-25 23:42:51 +02:00
parent 96d544e047
commit 2ad0e359ef
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
1 changed files with 22 additions and 19 deletions

View File

@ -35,12 +35,12 @@ func BeforeTime(t time.Time) ValidationStrategy {
} }
} }
func (v *timeoutValidStrategy) IsValid() bool { func (s *timeoutValidStrategy) IsValid() bool {
return v.until.After(time.Now()) return s.until.After(time.Now())
} }
func (v *timeoutValidStrategy) Invalidate() { func (s *timeoutValidStrategy) Invalidate() {
v.until = time.Time{} s.until = time.Time{}
} }
type ServerSpec struct { type ServerSpec struct {
@ -63,19 +63,19 @@ func NewServerSpecFromPB(spec ServerEndpoint) *ServerSpec {
return NewServerSpec(dest, AlwaysValid(), spec.User...) return NewServerSpec(dest, AlwaysValid(), spec.User...)
} }
func (v *ServerSpec) Destination() net.Destination { func (s *ServerSpec) Destination() net.Destination {
return v.dest return s.dest
} }
func (v *ServerSpec) HasUser(user *User) bool { func (s *ServerSpec) HasUser(user *User) bool {
v.RLock() s.RLock()
defer v.RUnlock() defer s.RUnlock()
accountA, err := user.GetTypedAccount() accountA, err := user.GetTypedAccount()
if err != nil { if err != nil {
return false return false
} }
for _, u := range v.users { for _, u := range s.users {
accountB, err := u.GetTypedAccount() accountB, err := u.GetTypedAccount()
if err == nil && accountA.Equals(accountB) { if err == nil && accountA.Equals(accountB) {
return true return true
@ -84,26 +84,29 @@ func (v *ServerSpec) HasUser(user *User) bool {
return false return false
} }
func (v *ServerSpec) AddUser(user *User) { func (s *ServerSpec) AddUser(user *User) {
if v.HasUser(user) { if s.HasUser(user) {
return return
} }
v.Lock() s.Lock()
defer v.Unlock() defer s.Unlock()
v.users = append(v.users, user) s.users = append(s.users, user)
} }
func (v *ServerSpec) PickUser() *User { func (s *ServerSpec) PickUser() *User {
userCount := len(v.users) s.RLock()
defer s.RUnlock()
userCount := len(s.users)
switch userCount { switch userCount {
case 0: case 0:
return nil return nil
case 1: case 1:
return v.users[0] return s.users[0]
default: default:
return v.users[dice.Roll(userCount)] return s.users[dice.Roll(userCount)]
} }
} }