3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +01:00

do idle in the server goroutine instead of the timeout goroutine

This commit is contained in:
Jeremy Latt 2014-02-18 11:22:56 -08:00
parent 4f106e7d3e
commit 2bc1b952a0
2 changed files with 11 additions and 13 deletions

View File

@ -79,26 +79,24 @@ func (client *Client) Touch() {
} }
if client.idleTimer == nil { if client.idleTimer == nil {
client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.Idle) client.idleTimer = time.AfterFunc(IDLE_TIMEOUT, client.connectionIdle)
} else { } else {
client.idleTimer.Reset(IDLE_TIMEOUT) client.idleTimer.Reset(IDLE_TIMEOUT)
} }
} }
type ClientIdle struct {
BaseCommand
}
func (client *Client) Idle() { func (client *Client) Idle() {
client.Reply(RplPing(client.server, client))
if client.quitTimer == nil { if client.quitTimer == nil {
client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout) client.quitTimer = time.AfterFunc(QUIT_TIMEOUT, client.connectionTimeout)
} else { } else {
client.quitTimer.Reset(QUIT_TIMEOUT) client.quitTimer.Reset(QUIT_TIMEOUT)
} }
}
cmd := &ClientIdle{} func (client *Client) connectionIdle() {
cmd.SetClient(client) client.server.idle <- client
client.server.commands <- cmd
} }
func (client *Client) connectionTimeout() { func (client *Client) connectionTimeout() {

View File

@ -19,6 +19,7 @@ type Server struct {
commands chan Command commands chan Command
conns chan net.Conn conns chan net.Conn
ctime time.Time ctime time.Time
idle chan *Client
motdFile string motdFile string
name string name string
operators map[string]string operators map[string]string
@ -33,6 +34,7 @@ func NewServer(config *Config) *Server {
commands: make(chan Command), commands: make(chan Command),
conns: make(chan net.Conn), conns: make(chan net.Conn),
ctime: time.Now(), ctime: time.Now(),
idle: make(chan *Client),
motdFile: config.MOTD, motdFile: config.MOTD,
name: config.Name, name: config.Name,
operators: make(map[string]string), operators: make(map[string]string),
@ -60,6 +62,9 @@ func (server *Server) ReceiveCommands() {
case client := <-server.toDestroy: case client := <-server.toDestroy:
client.Destroy() client.Destroy()
case client := <-server.idle:
client.Idle()
case cmd := <-server.commands: case cmd := <-server.commands:
client := cmd.Client() client := cmd.Client()
if DEBUG_SERVER { if DEBUG_SERVER {
@ -650,11 +655,6 @@ func (msg *ListCommand) HandleServer(server *Server) {
client.Reply(RplListEnd(server)) client.Reply(RplListEnd(server))
} }
func (msg *ClientIdle) HandleServer(server *Server) {
client := msg.Client()
client.Reply(RplPing(server, client))
}
func (msg *NamesCommand) HandleServer(server *Server) { func (msg *NamesCommand) HandleServer(server *Server) {
client := msg.Client() client := msg.Client()
if len(server.channels) == 0 { if len(server.channels) == 0 {