From f0541cd3c6d4fe9541707950ee80cb2baf8a3b6a Mon Sep 17 00:00:00 2001 From: Liam Stanley Date: Sun, 13 Nov 2016 05:46:44 -0500 Subject: [PATCH] additional lock/unlocking issues --- callback.go | 3 +++ main.go | 6 +----- state.go | 25 +++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/callback.go b/callback.go index c9a7425..e6de2a7 100644 --- a/callback.go +++ b/callback.go @@ -2,6 +2,9 @@ package girc // handleEvent runs the necessary callbacks for the incoming event func (c *Client) handleEvent(event *Event) { + // log the event + c.log.Print(event.String()) + // wildcard callbacks first if callbacks, ok := c.callbacks[ALLEVENTS]; ok { for i := 0; i < len(callbacks); i++ { diff --git a/main.go b/main.go index 57b3c31..e6738a7 100644 --- a/main.go +++ b/main.go @@ -215,11 +215,7 @@ func (c *Client) Wait() { for { select { case event := <-c.Events: - // log the event - c.log.Print(event.String()) - - // run in the background - go c.handleEvent(event) + c.handleEvent(event) case <-c.quitChan: return } diff --git a/state.go b/state.go index 90ace85..b5105e1 100644 --- a/state.go +++ b/state.go @@ -44,43 +44,43 @@ func NewState() *State { // createChanIfNotExists creates the channel in state, if not already done func (s *State) createChanIfNotExists(channel string) { - s.m.Lock() - defer s.m.Unlock() + channel = strings.ToLower(channel) // not a valid channel if !IsValidChannel(channel) { return } + s.m.Lock() if _, ok := s.channels[channel]; !ok { s.channels[channel] = &Channel{ - Name: strings.ToLower(channel), + Name: channel, users: make(map[string]*User), Joined: time.Now(), } } + s.m.Unlock() } // deleteChannel removes the channel from state, if not already done func (s *State) deleteChannel(channel string) { - s.m.Lock() - defer s.m.Unlock() - + channel = strings.ToLower(channel) s.createChanIfNotExists(channel) + s.m.Lock() if _, ok := s.channels[channel]; ok { delete(s.channels, channel) } + s.m.Unlock() } // createUserIfNotExists creates the channel and user in state, // if not already done func (s *State) createUserIfNotExists(channel, nick, ident, host string) { - s.m.Lock() - defer s.m.Unlock() - + channel = strings.ToLower(channel) s.createChanIfNotExists(channel) + s.m.Lock() if _, ok := s.channels[channel].users[nick]; !ok { s.channels[channel].users[nick] = &User{ Nick: nick, @@ -89,18 +89,19 @@ func (s *State) createUserIfNotExists(channel, nick, ident, host string) { FirstSeen: time.Now(), } } + s.m.Unlock() } // deleteUser removes the user from channel state func (s *State) deleteUser(channel, nick string) { - s.m.Lock() - defer s.m.Unlock() - + channel = strings.ToLower(channel) s.createChanIfNotExists(channel) + s.m.Lock() if _, ok := s.channels[channel].users[nick]; ok { delete(s.channels[channel].users, nick) } + s.m.Unlock() } // renameUser renames the user in state, in all locations where