Fix race condition with NICK and QUIT when multiple dispatch users are in the same channel

This commit is contained in:
Ken-Håvard Lieng 2017-04-11 03:49:52 +02:00
parent 3393b1b706
commit 18651c1a10
8 changed files with 145 additions and 138 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,14 +1,14 @@
export default function createCommandMiddleware(type, handlers) {
return store => next => action => {
return ({ dispatch, getState }) => next => action => {
if (action.type === type) {
const words = action.command.slice(1).split(' ');
const command = words[0];
const params = words.slice(1);
if (Object.prototype.hasOwnProperty.call(handlers, command)) {
if (command in handlers) {
handlers[command]({
dispatch: store.dispatch,
getState: store.getState,
dispatch,
getState,
server: action.server,
channel: action.channel
}, ...params);

View File

@ -104,15 +104,15 @@ export default createReducer(Map(), {
},
[actions.SOCKET_NICK](state, action) {
const { server, channels } = action;
const { server } = action;
return state.withMutations(s => {
channels.forEach(channel => {
s.updateIn([server, channel, 'users'], users => {
const i = users.findIndex(user => user.nick === action.old);
return users.update(i, user =>
updateRenderName(user.set('nick', action.new))
).sort(compareUsers);
});
s.get(server).forEach((v, channel) => {
s.updateIn([server, channel, 'users'], users =>
users.update(
users.findIndex(user => user.nick === action.old),
user => updateRenderName(user.set('nick', action.new))
).sort(compareUsers)
);
});
});
},

View File

@ -7,76 +7,101 @@ function withReason(message, reason) {
return message + (reason ? ` (${reason})` : '');
}
function findChannels(state, server, user) {
const channels = [];
state.channels.get(server).forEach((channel, channelName) => {
if (channel.get('users').find(u => u.nick === user)) {
channels.push(channelName);
}
});
return channels;
}
export default function handleSocket(socket, { dispatch, getState }) {
socket.onAny((event, data) => {
const type = `SOCKET_${event.toUpperCase()}`;
const handlers = {
message(message) {
dispatch(addMessage(message));
},
pm(message) {
dispatch(addMessage(message));
},
join(data) {
const state = getState();
const { server, channel } = state.tab.selected;
if (server && channel) {
const { nick } = state.servers.get(server);
const [joinedChannel] = data.channels;
if (server === data.server &&
nick === data.user &&
channel !== joinedChannel &&
normalizeChannel(channel) === normalizeChannel(joinedChannel)) {
dispatch(select(server, joinedChannel));
}
}
dispatch(inform(`${data.user} joined the channel`, data.server, data.channels[0]));
},
servers(data) {
if (!data) {
dispatch(routeActions.replace('/connect'));
}
},
part({ user, server, channel, reason }) {
dispatch(inform(withReason(`${user} left the channel`, reason), server, channel));
},
quit({ user, server, reason }) {
const channels = findChannels(getState(), server, user);
dispatch(broadcast(withReason(`${user} quit`, reason), server, channels));
},
nick(data) {
const channels = findChannels(getState(), data.server, data.old);
dispatch(broadcast(`${data.old} changed nick to ${data.new}`, data.server, channels));
},
motd({ content, server }) {
dispatch(addMessages(content.map(line => ({
server,
to: server,
message: line
}))));
},
whois(data) {
const tab = getState().tab.selected;
dispatch(inform([
`Nick: ${data.nick}`,
`Username: ${data.username}`,
`Realname: ${data.realname}`,
`Host: ${data.host}`,
`Server: ${data.server}`,
`Channels: ${data.channels}`
], tab.server, tab.channel));
},
print({ server, message }) {
dispatch(inform(message, server));
}
};
socket.onMessage((type, data) => {
if (type in handlers) {
handlers[type](data);
}
type = `SOCKET_${type.toUpperCase()}`;
if (Array.isArray(data)) {
dispatch({ type, data });
} else {
dispatch({ type, ...data });
}
});
socket.on('message', message => dispatch(addMessage(message)));
socket.on('pm', message => dispatch(addMessage(message)));
socket.on('join', data => {
const state = getState();
const { server, channel } = state.tab.selected;
if (server && channel) {
const { nick } = state.servers.get(server);
const [joinedChannel] = data.channels;
if (server === data.server &&
nick === data.user &&
channel !== joinedChannel &&
normalizeChannel(channel) === normalizeChannel(joinedChannel)) {
dispatch(select(server, joinedChannel));
}
}
});
socket.on('servers', data => {
if (!data) {
dispatch(routeActions.replace('/connect'));
}
});
socket.on('join', ({ user, server, channels }) =>
dispatch(inform(`${user} joined the channel`, server, channels[0]))
);
socket.on('part', ({ user, server, channel, reason }) =>
dispatch(inform(withReason(`${user} left the channel`, reason), server, channel))
);
socket.on('quit', ({ user, server, reason, channels }) =>
dispatch(broadcast(withReason(`${user} quit`, reason), server, channels))
);
socket.on('nick', data =>
dispatch(broadcast(`${data.old} changed nick to ${data.new}`, data.server, data.channels))
);
socket.on('motd', ({ content, server }) =>
dispatch(addMessages(content.map(line => ({
server,
to: server,
message: line
}))))
);
socket.on('whois', data => {
const tab = getState().tab.selected;
dispatch(inform([
`Nick: ${data.nick}`,
`Username: ${data.username}`,
`Realname: ${data.realname}`,
`Host: ${data.host}`,
`Server: ${data.server}`,
`Channels: ${data.channels}`
], tab.server, tab.channel));
});
socket.on('print', ({ server, message }) => dispatch(inform(message, server)));
}

View File

@ -1,10 +1,7 @@
import EventEmitter2 from 'eventemitter2';
import Backoff from 'backo';
export default class Socket extends EventEmitter2 {
export default class Socket {
constructor(host) {
super();
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws';
this.url = `${protocol}://${host}/ws`;
@ -15,6 +12,7 @@ export default class Socket extends EventEmitter2 {
max: 5000,
jitter: 0.25
});
this.handlers = [];
this.connect();
}
@ -30,7 +28,6 @@ export default class Socket extends EventEmitter2 {
this.ws.onopen = () => {
clearTimeout(this.timeoutConnect);
this.backoff.reset();
this.emit('connect');
this.setTimeoutPing();
};
@ -38,7 +35,6 @@ export default class Socket extends EventEmitter2 {
clearTimeout(this.timeoutConnect);
clearTimeout(this.timeoutPing);
if (!this.closing) {
this.emit('disconnect');
this.retry();
}
this.closing = false;
@ -76,10 +72,19 @@ export default class Socket extends EventEmitter2 {
setTimeoutPing() {
clearTimeout(this.timeoutPing);
this.timeoutPing = setTimeout(() => {
this.emit('disconnect');
this.closing = true;
this.ws.close();
this.connect();
}, this.pingTimeout);
}
onMessage(handler) {
this.handlers.push(handler);
}
emit(type, data) {
for (let i = 0; i < this.handlers.length; i++) {
this.handlers[i](type, data);
}
}
}

View File

@ -57,10 +57,9 @@ func (i *ircHandler) dispatchMessage(msg *irc.Message) {
func (i *ircHandler) nick(msg *irc.Message) {
i.session.sendJSON("nick", Nick{
Server: i.client.Host,
Old: msg.Nick,
New: msg.LastParam(),
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host),
Server: i.client.Host,
Old: msg.Nick,
New: msg.LastParam(),
})
channelStore.RenameUser(msg.Nick, msg.LastParam(), i.client.Host)
@ -138,10 +137,9 @@ func (i *ircHandler) message(msg *irc.Message) {
func (i *ircHandler) quit(msg *irc.Message) {
i.session.sendJSON("quit", Quit{
Server: i.client.Host,
User: msg.Nick,
Reason: msg.LastParam(),
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host),
Server: i.client.Host,
User: msg.Nick,
Reason: msg.LastParam(),
})
channelStore.RemoveUserAll(msg.Nick, i.client.Host)

View File

@ -27,10 +27,9 @@ type Connect struct {
}
type Nick struct {
Server string `json:"server"`
Old string `json:"old"`
New string `json:"new"`
Channels []string `json:"channels"`
Server string `json:"server"`
Old string `json:"old"`
New string `json:"new"`
}
type Join struct {
@ -56,10 +55,9 @@ type Mode struct {
}
type Quit struct {
Server string `json:"server"`
User string `json:"user"`
Reason string `json:"reason,omitempty"`
Channels []string `json:"channels"`
Server string `json:"server"`
User string `json:"user"`
Reason string `json:"reason,omitempty"`
}
type Chat struct {

View File

@ -101,25 +101,6 @@ func (c *ChannelStore) SetMode(server, channel, user, add, remove string) {
c.userLock.Unlock()
}
func (c *ChannelStore) FindUserChannels(user, server string) []string {
var channels []string
c.userLock.Lock()
for channel, users := range c.users[server] {
for _, nick := range users {
if strings.TrimLeft(nick, "@+") == user {
channels = append(channels, channel)
break
}
}
}
c.userLock.Unlock()
return channels
}
func (c *ChannelStore) GetTopic(server, channel string) string {
c.topicLock.Lock()
defer c.topicLock.Unlock()