Make storage.User synchronous, call session.setWS earlier

This commit is contained in:
Ken-Håvard Lieng 2015-06-07 06:16:16 +02:00
parent 7d17b4b30f
commit c6c740e24b
3 changed files with 28 additions and 19 deletions

View File

@ -92,7 +92,7 @@ func handleIRC(client *irc.Client, session *Session) {
} }
if msg.Params[0] != "*" { if msg.Params[0] != "*" {
session.user.LogMessage(client.Host, msg.Nick, msg.Params[0], msg.Trailing) go session.user.LogMessage(client.Host, msg.Nick, msg.Params[0], msg.Trailing)
} }
case irc.Quit: case irc.Quit:

View File

@ -48,6 +48,7 @@ func handleWS(conn *websocket.Conn) {
if storedSession, exists := sessions[UUID]; exists { if storedSession, exists := sessions[UUID]; exists {
sessionLock.Unlock() sessionLock.Unlock()
session = storedSession session = storedSession
session.setWS(addr, ws)
log.Println(addr, "attached to", session.numIRC(), "existing IRC connections") log.Println(addr, "attached to", session.numIRC(), "existing IRC connections")
@ -73,13 +74,12 @@ func handleWS(conn *websocket.Conn) {
sessions[UUID] = session sessions[UUID] = session
sessionLock.Unlock() sessionLock.Unlock()
session.setWS(addr, ws)
session.sendJSON("servers", nil) session.sendJSON("servers", nil)
go session.write() go session.write()
} }
session.setWS(addr, ws)
case "connect": case "connect":
var data Connect var data Connect

View File

@ -51,7 +51,7 @@ func NewUser(uuid string) *User {
UUID: uuid, UUID: uuid,
} }
go db.Update(func(tx *bolt.Tx) error { db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Users")) b := tx.Bucket([]byte("Users"))
data, _ := json.Marshal(user) data, _ := json.Marshal(user)
@ -60,7 +60,7 @@ func NewUser(uuid string) *User {
return nil return nil
}) })
go user.openMessageLog() user.openMessageLog()
return user return user
} }
@ -73,7 +73,7 @@ func LoadUsers() []*User {
b.ForEach(func(k, v []byte) error { b.ForEach(func(k, v []byte) error {
user := User{UUID: string(k)} user := User{UUID: string(k)}
go user.openMessageLog() user.openMessageLog()
users = append(users, &user) users = append(users, &user)
@ -126,7 +126,7 @@ func (u *User) GetChannels() []Channel {
} }
func (u *User) AddServer(server Server) { func (u *User) AddServer(server Server) {
go db.Update(func(tx *bolt.Tx) error { db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Servers")) b := tx.Bucket([]byte("Servers"))
data, _ := json.Marshal(server) data, _ := json.Marshal(server)
@ -137,7 +137,7 @@ func (u *User) AddServer(server Server) {
} }
func (u *User) AddChannel(channel Channel) { func (u *User) AddChannel(channel Channel) {
go db.Update(func(tx *bolt.Tx) error { db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Channels")) b := tx.Bucket([]byte("Channels"))
data, _ := json.Marshal(channel) data, _ := json.Marshal(channel)
@ -148,7 +148,7 @@ func (u *User) AddChannel(channel Channel) {
} }
func (u *User) SetNick(nick, address string) { func (u *User) SetNick(nick, address string) {
go db.Update(func(tx *bolt.Tx) error { db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Servers")) b := tx.Bucket([]byte("Servers"))
id := []byte(u.UUID + ":" + address) id := []byte(u.UUID + ":" + address)
var server Server var server Server
@ -164,7 +164,7 @@ func (u *User) SetNick(nick, address string) {
} }
func (u *User) RemoveServer(address string) { func (u *User) RemoveServer(address string) {
go db.Update(func(tx *bolt.Tx) error { db.Update(func(tx *bolt.Tx) error {
serverID := []byte(u.UUID + ":" + address) serverID := []byte(u.UUID + ":" + address)
tx.Bucket([]byte("Servers")).Delete(serverID) tx.Bucket([]byte("Servers")).Delete(serverID)
@ -181,7 +181,7 @@ func (u *User) RemoveServer(address string) {
} }
func (u *User) RemoveChannel(server, channel string) { func (u *User) RemoveChannel(server, channel string) {
go db.Update(func(tx *bolt.Tx) error { db.Update(func(tx *bolt.Tx) error {
tx.Bucket([]byte("Channels")).Delete([]byte(u.UUID + ":" + server + ":" + channel)) tx.Bucket([]byte("Channels")).Delete([]byte(u.UUID + ":" + server + ":" + channel))
return nil return nil
@ -189,13 +189,17 @@ func (u *User) RemoveChannel(server, channel string) {
} }
func (u *User) LogMessage(server, from, to, content string) { func (u *User) LogMessage(server, from, to, content string) {
go u.messageLog.Update(func(tx *bolt.Tx) error { bucketKey := server + ":" + to
bucketKey := server + ":" + to var id uint64
b, _ := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey)) var idStr string
id, _ := b.NextSequence() var message Message
idStr := strconv.FormatUint(id, 10)
message := Message{ u.messageLog.Update(func(tx *bolt.Tx) error {
b, _ := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey))
id, _ = b.NextSequence()
idStr = strconv.FormatUint(id, 10)
message = Message{
ID: id, ID: id,
Content: content, Content: content,
Server: server, Server: server,
@ -207,10 +211,10 @@ func (u *User) LogMessage(server, from, to, content string) {
data, _ := json.Marshal(message) data, _ := json.Marshal(message)
b.Put([]byte(idStr), data) b.Put([]byte(idStr), data)
go u.messageIndex.Index(bucketKey+":"+idStr, message)
return nil return nil
}) })
u.messageIndex.Index(bucketKey+":"+idStr, message)
} }
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) { func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) {
@ -301,6 +305,11 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error)
return messages, nil return messages, nil
} }
func (u *User) Close() {
u.messageLog.Close()
u.messageIndex.Close()
}
func (u *User) openMessageLog() { func (u *User) openMessageLog() {
var err error var err error