From 6598fcf36ea5ddf4a4e6173694759429a19d63c9 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 7 Aug 2020 15:31:07 +0200 Subject: [PATCH] Remove user from Server map when stopped --- server.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/server.go b/server.go index 99cec0f..bb93106 100644 --- a/server.go +++ b/server.go @@ -79,12 +79,8 @@ func (s *Server) Run() error { } s.lock.Lock() - for _, record := range users { - s.Logger.Printf("starting bouncer for user %q", record.Username) - u := newUser(s, &record) - s.users[u.Username] = u - - go u.run() + for i := range users { + s.addUserLocked(&users[i]) } s.lock.Unlock() @@ -104,11 +100,7 @@ func (s *Server) createUser(user *User) (*user, error) { return nil, fmt.Errorf("could not create user in db: %v", err) } - s.Logger.Printf("starting bouncer for new user %q", user.Username) - u := newUser(s, user) - s.users[u.Username] = u - go u.run() - return u, nil + return s.addUserLocked(user), nil } func (s *Server) getUser(name string) *user { @@ -118,6 +110,22 @@ func (s *Server) getUser(name string) *user { return u } +func (s *Server) addUserLocked(user *User) *user { + s.Logger.Printf("starting bouncer for user %q", user.Username) + u := newUser(s, user) + s.users[u.Username] = u + + go func() { + u.run() + + s.lock.Lock() + delete(s.users, u.Username) + s.lock.Unlock() + }() + + return u +} + var lastDownstreamID uint64 = 0 func (s *Server) handle(ic ircConn) {