Skip to content

Commit

Permalink
Add a bunch of locks around things to fix some races
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Irvine committed Feb 25, 2016
1 parent 2cc2143 commit 4502914
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
15 changes: 11 additions & 4 deletions caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"errors"
"fmt"
"reflect"
"sync"
)

type caller struct {
Func reflect.Value
Args []reflect.Type
sync.RWMutex
Func reflect.Value
Args []reflect.Type
}

func newCaller(f interface{}) (*caller, error) {
Expand All @@ -28,12 +30,15 @@ func newCaller(f interface{}) (*caller, error) {
}

return &caller{
Func: fv,
Args: args,
Func: fv,
Args: args,
}, nil
}

func (c *caller) GetArgs() []interface{} {
c.RLock()
defer c.RUnlock()

ret := make([]interface{}, len(c.Args))
for i, argT := range c.Args {
if argT.Kind() == reflect.Ptr {
Expand All @@ -46,6 +51,8 @@ func (c *caller) GetArgs() []interface{} {
}

func (c *caller) Call(args []interface{}) []reflect.Value {
c.RLock()
defer c.RUnlock()
var a []reflect.Value
diff := 0

Expand Down
22 changes: 14 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package socketio_client

import (
"net/url"
"reflect"
"path"
"reflect"
"strings"
"sync"
)

type Options struct {
Expand All @@ -17,10 +18,11 @@ type Client struct {

conn *clientConn

events map[string]*caller
acks map[int]*caller
id int
namespace string
eventsLock sync.RWMutex
events map[string]*caller
acks map[int]*caller
id int
namespace string
}

func NewClient(uri string, opts *Options) (client *Client, err error) {
Expand All @@ -29,10 +31,10 @@ func NewClient(uri string, opts *Options) (client *Client, err error) {
if err != nil {
return
}
url.Path = path.Join("/socket.io",url.Path)
url.Path = path.Join("/socket.io", url.Path)
url.Path = url.EscapedPath()
if strings.HasSuffix(url.Path,"socket.io"){
url.Path+="/"
if strings.HasSuffix(url.Path, "socket.io") {
url.Path += "/"
}
q := url.Query()
for k, v := range opts.Query {
Expand Down Expand Up @@ -63,7 +65,9 @@ func (client *Client) On(message string, f interface{}) (err error) {
if err != nil {
return
}
client.eventsLock.Lock()
client.events[message] = c
client.eventsLock.Unlock()
return
}

Expand Down Expand Up @@ -147,7 +151,9 @@ func (client *Client) onPacket(decoder *decoder, packet *packet) ([]interface{},
default:
message = decoder.Message()
}
client.eventsLock.RLock()
c, ok := client.events[message]
client.eventsLock.RUnlock()
if !ok {
// If the message is not recognized by the server, the decoder.currentCloser
// needs to be closed otherwise the server will be stuck until the e
Expand Down
2 changes: 2 additions & 0 deletions client_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (c *clientConn) OnPacket(r *parser.PacketDecoder) {
t := c.getCurrent()
u := c.getUpgrade()
newWriter := t.NextWriter
c.writerLocker.Lock()
if u != nil {
if w, _ := t.NextWriter(message.MessageText, parser.NOOP); w != nil {
w.Close()
Expand All @@ -191,6 +192,7 @@ func (c *clientConn) OnPacket(r *parser.PacketDecoder) {
io.Copy(w, r)
w.Close()
}
c.writerLocker.Unlock()
fallthrough
case parser.PONG:
c.pingChan <- true
Expand Down

0 comments on commit 4502914

Please sign in to comment.