3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-29 07:29:31 +01:00

refactor channel join and part

This commit is contained in:
Shivaram Lingamneni 2018-04-24 03:11:11 -04:00
parent 22ec6139bf
commit b97ae00fe3
2 changed files with 96 additions and 90 deletions

View File

@ -25,7 +25,6 @@ type Channel struct {
key string key string
members MemberSet members MemberSet
membersCache []*Client // allow iteration over channel members without holding the lock membersCache []*Client // allow iteration over channel members without holding the lock
membersCacheMutex sync.Mutex // tier 2; see `regenerateMembersCache`
name string name string
nameCasefolded string nameCasefolded string
server *Server server *Server
@ -33,6 +32,7 @@ type Channel struct {
registeredFounder string registeredFounder string
registeredTime time.Time registeredTime time.Time
stateMutex sync.RWMutex // tier 1 stateMutex sync.RWMutex // tier 1
joinPartMutex sync.Mutex // tier 3
topic string topic string
topicSetBy string topicSetBy string
topicSetTime time.Time topicSetTime time.Time
@ -163,33 +163,19 @@ func (channel *Channel) IsRegistered() bool {
return channel.registeredFounder != "" return channel.registeredFounder != ""
} }
func (channel *Channel) regenerateMembersCache(noLocksNeeded bool) { func (channel *Channel) regenerateMembersCache() {
// this is eventually consistent even without holding stateMutex.Lock()
// throughout the update; all updates to `members` while holding Lock()
// have a serial order, so the call to `regenerateMembersCache` that
// happens-after the last one will see *all* the updates. then,
// `membersCacheMutex` ensures that this final read is correctly paired
// with the final write to `membersCache`.
if !noLocksNeeded {
channel.membersCacheMutex.Lock()
defer channel.membersCacheMutex.Unlock()
channel.stateMutex.RLock() channel.stateMutex.RLock()
}
result := make([]*Client, len(channel.members)) result := make([]*Client, len(channel.members))
i := 0 i := 0
for client := range channel.members { for client := range channel.members {
result[i] = client result[i] = client
i++ i++
} }
if !noLocksNeeded {
channel.stateMutex.RUnlock() channel.stateMutex.RUnlock()
channel.stateMutex.Lock() channel.stateMutex.Lock()
}
channel.membersCache = result channel.membersCache = result
if !noLocksNeeded {
channel.stateMutex.Unlock() channel.stateMutex.Unlock()
}
} }
// Names sends the list of users joined to the channel to the given client. // Names sends the list of users joined to the channel to the given client.
@ -381,110 +367,119 @@ func (channel *Channel) Join(client *Client, key string, rb *ResponseBuffer) {
return return
} }
chname := channel.Name()
if channel.IsFull() { if channel.IsFull() {
rb.Add(nil, client.server.name, ERR_CHANNELISFULL, channel.name, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "l")) rb.Add(nil, client.server.name, ERR_CHANNELISFULL, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "l"))
return return
} }
if !channel.CheckKey(key) { if !channel.CheckKey(key) {
rb.Add(nil, client.server.name, ERR_BADCHANNELKEY, channel.name, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "k")) rb.Add(nil, client.server.name, ERR_BADCHANNELKEY, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "k"))
return return
} }
isInvited := channel.lists[modes.InviteMask].Match(client.nickMaskCasefolded) isInvited := channel.lists[modes.InviteMask].Match(client.nickMaskCasefolded)
if channel.flags.HasMode(modes.InviteOnly) && !isInvited { if channel.flags.HasMode(modes.InviteOnly) && !isInvited {
rb.Add(nil, client.server.name, ERR_INVITEONLYCHAN, channel.name, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "i")) rb.Add(nil, client.server.name, ERR_INVITEONLYCHAN, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "i"))
return return
} }
if channel.lists[modes.BanMask].Match(client.nickMaskCasefolded) && if channel.lists[modes.BanMask].Match(client.nickMaskCasefolded) &&
!isInvited && !isInvited &&
!channel.lists[modes.ExceptMask].Match(client.nickMaskCasefolded) { !channel.lists[modes.ExceptMask].Match(client.nickMaskCasefolded) {
rb.Add(nil, client.server.name, ERR_BANNEDFROMCHAN, channel.name, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "b")) rb.Add(nil, client.server.name, ERR_BANNEDFROMCHAN, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "b"))
return return
} }
client.server.logger.Debug("join", fmt.Sprintf("%s joined channel %s", client.nick, channel.name)) client.server.logger.Debug("join", fmt.Sprintf("%s joined channel %s", client.nick, chname))
for _, member := range channel.Members() { newChannel, givenMode := func() (newChannel bool, givenMode modes.Mode) {
if member == client { channel.joinPartMutex.Lock()
if member.capabilities.Has(caps.ExtendedJoin) { defer channel.joinPartMutex.Unlock()
rb.Add(nil, client.nickMaskString, "JOIN", channel.name, client.AccountName(), client.realname)
} else {
rb.Add(nil, client.nickMaskString, "JOIN", channel.name)
}
} else {
if member.capabilities.Has(caps.ExtendedJoin) {
member.Send(nil, client.nickMaskString, "JOIN", channel.name, client.AccountName(), client.realname)
} else {
member.Send(nil, client.nickMaskString, "JOIN", channel.name)
}
}
}
func() {
account := client.Account()
channel.stateMutex.Lock() channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
channel.members.Add(client) channel.members.Add(client)
firstJoin := len(channel.members) == 1 firstJoin := len(channel.members) == 1
channel.stateMutex.Unlock() newChannel = firstJoin && channel.registeredFounder == ""
channel.regenerateMembersCache(false) if newChannel {
givenMode = modes.ChannelOperator
} else {
givenMode = channel.accountToUMode[account]
}
if givenMode != 0 {
channel.members[client].SetMode(givenMode, true)
}
}()
channel.regenerateMembersCache()
return
}()
client.addChannel(channel) client.addChannel(channel)
account := client.Account() nick := client.Nick()
nickmask := client.NickMaskString()
realname := client.Realname()
accountName := client.AccountName()
var modestr string
if givenMode != 0 {
modestr = fmt.Sprintf("+%v", givenMode)
}
// give channel mode if necessary for _, member := range channel.Members() {
channel.stateMutex.Lock() if member == client {
newChannel := firstJoin && channel.registeredFounder == "" continue
mode, persistentModeExists := channel.accountToUMode[account] }
var givenMode *modes.Mode if member.capabilities.Has(caps.ExtendedJoin) {
if persistentModeExists { member.Send(nil, nickmask, "JOIN", chname, accountName, realname)
givenMode = &mode } else {
} else if newChannel { member.Send(nil, nickmask, "JOIN", chname)
givenMode = &modes.ChannelOperator }
if givenMode != 0 {
member.Send(nil, client.server.name, "MODE", chname, modestr, nick)
} }
if givenMode != nil {
channel.members[client].SetMode(*givenMode, true)
} }
channel.stateMutex.Unlock()
if client.capabilities.Has(caps.ExtendedJoin) { if client.capabilities.Has(caps.ExtendedJoin) {
rb.Add(nil, client.nickMaskString, "JOIN", channel.name, client.AccountName(), client.realname) rb.Add(nil, nickmask, "JOIN", chname, accountName, realname)
} else { } else {
rb.Add(nil, client.nickMaskString, "JOIN", channel.name) rb.Add(nil, nickmask, "JOIN", chname)
} }
// don't send topic when it's an entirely new channel // don't send topic when it's an entirely new channel
if !newChannel { if !newChannel {
channel.SendTopic(client, rb) channel.SendTopic(client, rb)
} }
channel.Names(client, rb) channel.Names(client, rb)
if givenMode != nil {
for _, member := range channel.Members() { if givenMode != 0 {
if member == client { rb.Add(nil, client.server.name, "MODE", chname, modestr, nick)
rb.Add(nil, client.server.name, "MODE", channel.name, fmt.Sprintf("+%v", *givenMode), client.nick)
} else {
member.Send(nil, client.server.name, "MODE", channel.name, fmt.Sprintf("+%v", *givenMode), client.nick)
}
}
} }
} }
// Part parts the given client from this channel, with the given message. // Part parts the given client from this channel, with the given message.
func (channel *Channel) Part(client *Client, message string, rb *ResponseBuffer) { func (channel *Channel) Part(client *Client, message string, rb *ResponseBuffer) {
chname := channel.Name()
if !channel.hasClient(client) { if !channel.hasClient(client) {
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, channel.name, client.t("You're not on that channel")) rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, chname, client.t("You're not on that channel"))
return return
} }
for _, member := range channel.Members() {
if member == client {
rb.Add(nil, client.nickMaskString, "PART", channel.name, message)
} else {
member.Send(nil, client.nickMaskString, "PART", channel.name, message)
}
}
channel.Quit(client) channel.Quit(client)
client.server.logger.Debug("part", fmt.Sprintf("%s left channel %s", client.nick, channel.name)) nickmask := client.NickMaskString()
for _, member := range channel.Members() {
member.Send(nil, nickmask, "PART", chname, message)
}
rb.Add(nil, nickmask, "PART", chname, message)
client.server.logger.Debug("part", fmt.Sprintf("%s left channel %s", client.nick, chname))
} }
// SendTopic sends the channel topic to the given client. // SendTopic sends the channel topic to the given client.
@ -762,17 +757,22 @@ func (channel *Channel) applyModeMask(client *Client, mode modes.Mode, op modes.
// Quit removes the given client from the channel // Quit removes the given client from the channel
func (channel *Channel) Quit(client *Client) { func (channel *Channel) Quit(client *Client) {
channelEmpty := func() bool {
channel.joinPartMutex.Lock()
defer channel.joinPartMutex.Unlock()
channel.stateMutex.Lock() channel.stateMutex.Lock()
channel.members.Remove(client) channel.members.Remove(client)
empty := len(channel.members) == 0 channelEmpty := len(channel.members) == 0
channel.stateMutex.Unlock() channel.stateMutex.Unlock()
channel.regenerateMembersCache(false) channel.regenerateMembersCache()
return channelEmpty
}()
client.removeChannel(channel) if channelEmpty {
if empty {
client.server.channels.Cleanup(channel) client.server.channels.Cleanup(channel)
} }
client.removeChannel(channel)
} }
func (channel *Channel) Kick(client *Client, target *Client, comment string, rb *ResponseBuffer) { func (channel *Channel) Kick(client *Client, target *Client, comment string, rb *ResponseBuffer) {

View File

@ -409,16 +409,20 @@ func (client *Client) TryResume() {
client.nick = oldClient.nick client.nick = oldClient.nick
client.updateNickMaskNoMutex() client.updateNickMaskNoMutex()
for channel := range oldClient.channels { rejoinChannel := func(channel *Channel) {
channel.stateMutex.Lock() channel.joinPartMutex.Lock()
defer channel.joinPartMutex.Unlock()
channel.stateMutex.Lock()
client.channels[channel] = true client.channels[channel] = true
client.resumeDetails.SendFakeJoinsFor = append(client.resumeDetails.SendFakeJoinsFor, channel.name) client.resumeDetails.SendFakeJoinsFor = append(client.resumeDetails.SendFakeJoinsFor, channel.name)
oldModeSet := channel.members[oldClient] oldModeSet := channel.members[oldClient]
channel.members.Remove(oldClient) channel.members.Remove(oldClient)
channel.members[client] = oldModeSet channel.members[client] = oldModeSet
channel.regenerateMembersCache(true) channel.stateMutex.Unlock()
channel.regenerateMembersCache()
// construct fake modestring if necessary // construct fake modestring if necessary
oldModes := oldModeSet.String() oldModes := oldModeSet.String()
@ -447,8 +451,10 @@ func (client *Client) TryResume() {
member.Send(nil, server.name, "MODE", params...) member.Send(nil, server.name, "MODE", params...)
} }
} }
}
channel.stateMutex.Unlock() for channel := range oldClient.channels {
rejoinChannel(channel)
} }
server.clients.byNick[oldnick] = client server.clients.byNick[oldnick] = client