From 50d2be3a8895bb92714ee0870222914c6f49a0f5 Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Sun, 24 Jul 2016 16:16:12 -0400 Subject: [PATCH] Fixed message buffer timeout --- chat/message/user.go | 5 +++-- cmd/ssh-chat/cmd.go | 1 + host.go | 3 +++ sshd/terminal.go | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/chat/message/user.go b/chat/message/user.go index a4f1adc..d4d1fcb 100644 --- a/chat/message/user.go +++ b/chat/message/user.go @@ -11,6 +11,7 @@ import ( ) const messageBuffer = 5 +const messageTimeout = 5 * time.Second const reHighlight = `\b(%s)\b` var ErrUserClosed = errors.New("user closed") @@ -152,8 +153,8 @@ func (u *User) Send(m Message) error { case u.msg <- m: case <-u.done: return ErrUserClosed - default: - logger.Printf("Msg buffer full, closing: %s", u.Name()) + case <-time.After(messageTimeout): + logger.Printf("Message buffer full, closing: %s", u.Name()) u.Close() return ErrUserClosed } diff --git a/cmd/ssh-chat/cmd.go b/cmd/ssh-chat/cmd.go index 42891e9..055f281 100644 --- a/cmd/ssh-chat/cmd.go +++ b/cmd/ssh-chat/cmd.go @@ -85,6 +85,7 @@ func main() { // Enable logging from submodules chat.SetLogger(os.Stderr) sshd.SetLogger(os.Stderr) + message.SetLogger(os.Stderr) } privateKeyPath := options.Identity diff --git a/host.go b/host.go index dfe096d..5017858 100644 --- a/host.go +++ b/host.go @@ -129,6 +129,8 @@ func (h *Host) Connect(term *sshd.Terminal) { } ratelimit := rateio.NewSimpleLimiter(3, time.Second*3) + logger.Debugf("Joined: %s", user.Name()) + for { line, err := term.ReadLine() if err == io.EOF { @@ -175,6 +177,7 @@ func (h *Host) Connect(term *sshd.Terminal) { logger.Errorf("Failed to leave: %s", err) return } + logger.Debugf("Leaving: %s", user.Name()) } // Serve our chat room onto the listener diff --git a/sshd/terminal.go b/sshd/terminal.go index 6a92bf9..e71749b 100644 --- a/sshd/terminal.go +++ b/sshd/terminal.go @@ -78,6 +78,7 @@ func NewTerminal(conn *ssh.ServerConn, ch ssh.NewChannel) (*Terminal, error) { go func() { for range time.Tick(keepaliveInterval) { + // TODO: Could break out earlier with a select if we want, rather than waiting for an error. _, err := channel.SendRequest(keepaliveRequest, true, nil) if err != nil { // Connection is gone