Exemple #1
0
func (self *Server) SignUp(user string) error {
	if len(user) > trib.MaxUsernameLen {
		return fmt.Errorf("username %q too long", user)
	}

	if !trib.IsValidUsername(user) {
		return fmt.Errorf("invalid username %q", user)
	}

	self.lock.Lock()
	defer self.lock.Unlock()

	_, found := self.users[user]
	if found {
		return fmt.Errorf("user %q already exists", user)
	}

	self.users[user] = newUser()
	return nil
}
Exemple #2
0
func (self *FrontServer) SignUp(user string) error {
	if len(user) > trib.MaxUsernameLen {
		return fmt.Errorf("username %q too long", user)
	}
	if !trib.IsValidUsername(user) {
		return fmt.Errorf("invalid username %q", user)
	}
	self.lock.Lock()
	defer self.lock.Unlock()

	client := self.storageClient.Bin(user)
	// make sure the global buffer is not a user
	anotherClient := self.storageClient.Bin("GlobalUsersCache")
	value := ""
	succ := false
	if e := anotherClient.Get("Existence", &value); e != nil {
		return e
	}
	if value != "True" {
		succ = false
		if e := anotherClient.Set(&trib.KeyValue{"Existence", "True"}, &succ); e != nil {
			return e
		}
	}

	// check existence:
	value = ""
	if e := client.Get("Existence", &value); e != nil {
		return e
	}
	if value == "True" {
		return fmt.Errorf("user %q already exists", user)
	}

	// simulate a lock using key-value store
	succ = false
	if e := self.Lock("SignupLock", client, &succ); e != nil {
		return e
	}

	// try signup
	succ = false
	if e := client.Set(&trib.KeyValue{"Existence", "True"}, &succ); e != nil {
		return e
	}

	// try unlock
	unlock, e := self.Unlock("SignupLock", client)
	if e != nil {
		return e
	}
	if unlock == true {
		// add to global cache
		if len(self.usersCache) < trib.MinListUser {
			self.usersCache = append(self.usersCache, user)
			succ = false
			if e := anotherClient.ListAppend(&trib.KeyValue{"Usernames", user}, &succ); e != nil {
				return e
			}
		}
		return nil
	} else {
		return fmt.Errorf("user %q already signed up by others..", user)
	}
}