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,20 +7,29 @@ function withReason(message, reason) {
return message + (reason ? ` (${reason})` : '');
}
export default function handleSocket(socket, { dispatch, getState }) {
socket.onAny((event, data) => {
const type = `SOCKET_${event.toUpperCase()}`;
if (Array.isArray(data)) {
dispatch({ type, data });
} else {
dispatch({ type, ...data });
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);
}
});
socket.on('message', message => dispatch(addMessage(message)));
socket.on('pm', message => dispatch(addMessage(message)));
return channels;
}
socket.on('join', data => {
export default function handleSocket(socket, { dispatch, getState }) {
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) {
@ -33,39 +42,39 @@ export default function handleSocket(socket, { dispatch, getState }) {
dispatch(select(server, joinedChannel));
}
}
});
socket.on('servers', data => {
dispatch(inform(`${data.user} joined the channel`, data.server, data.channels[0]));
},
servers(data) {
if (!data) {
dispatch(routeActions.replace('/connect'));
}
});
},
socket.on('join', ({ user, server, channels }) =>
dispatch(inform(`${user} joined the channel`, server, channels[0]))
);
part({ user, server, channel, reason }) {
dispatch(inform(withReason(`${user} left the channel`, reason), server, channel));
},
socket.on('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));
},
socket.on('quit', ({ user, server, reason, channels }) =>
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));
},
socket.on('nick', data =>
dispatch(broadcast(`${data.old} changed nick to ${data.new}`, data.server, data.channels))
);
socket.on('motd', ({ content, server }) =>
motd({ content, server }) {
dispatch(addMessages(content.map(line => ({
server,
to: server,
message: line
}))))
);
}))));
},
socket.on('whois', data => {
whois(data) {
const tab = getState().tab.selected;
dispatch(inform([
@ -76,7 +85,23 @@ export default function handleSocket(socket, { dispatch, getState }) {
`Server: ${data.server}`,
`Channels: ${data.channels}`
], tab.server, tab.channel));
});
},
socket.on('print', ({ server, message }) => dispatch(inform(message, server)));
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 });
}
});
}

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

@ -60,7 +60,6 @@ func (i *ircHandler) nick(msg *irc.Message) {
Server: i.client.Host,
Old: msg.Nick,
New: msg.LastParam(),
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host),
})
channelStore.RenameUser(msg.Nick, msg.LastParam(), i.client.Host)
@ -141,7 +140,6 @@ func (i *ircHandler) quit(msg *irc.Message) {
Server: i.client.Host,
User: msg.Nick,
Reason: msg.LastParam(),
Channels: channelStore.FindUserChannels(msg.Nick, i.client.Host),
})
channelStore.RemoveUserAll(msg.Nick, i.client.Host)

View File

@ -30,7 +30,6 @@ type Nick struct {
Server string `json:"server"`
Old string `json:"old"`
New string `json:"new"`
Channels []string `json:"channels"`
}
type Join struct {
@ -59,7 +58,6 @@ type Quit struct {
Server string `json:"server"`
User string `json:"user"`
Reason string `json:"reason,omitempty"`
Channels []string `json:"channels"`
}
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()