3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

do hostname lookups in the client read thread

This commit is contained in:
Jeremy Latt 2014-02-20 13:03:33 -08:00
parent 97881b555d
commit ad513da486
3 changed files with 86 additions and 52 deletions

View File

@ -12,6 +12,18 @@ func IsNickname(nick string) bool {
return NicknameExpr.MatchString(nick) return NicknameExpr.MatchString(nick)
} }
type HostnameLookup struct {
client *Client
hostname string
}
func NewHostnameLookup(client *Client, ipAddr string) *HostnameLookup {
return &HostnameLookup{
client: client,
hostname: LookupHostname(ipAddr),
}
}
type Client struct { type Client struct {
atime time.Time atime time.Time
awayMessage string awayMessage string
@ -23,6 +35,7 @@ type Client struct {
hostname string hostname string
idleTimer *time.Timer idleTimer *time.Timer
loginTimer *time.Timer loginTimer *time.Timer
lookups chan string
nick string nick string
phase Phase phase Phase
quitTimer *time.Timer quitTimer *time.Timer
@ -33,14 +46,14 @@ type Client struct {
username string username string
} }
func NewClient(server *Server, conn net.Conn, hostname string) *Client { func NewClient(server *Server, conn net.Conn) *Client {
now := time.Now() now := time.Now()
client := &Client{ client := &Client{
atime: now, atime: now,
channels: make(ChannelSet), channels: make(ChannelSet),
ctime: now, ctime: now,
flags: make(map[UserMode]bool), flags: make(map[UserMode]bool),
hostname: hostname, lookups: make(chan string),
phase: server.InitPhase(), phase: server.InitPhase(),
server: server, server: server,
socket: NewSocket(conn), socket: NewSocket(conn),
@ -48,6 +61,7 @@ func NewClient(server *Server, conn net.Conn, hostname string) *Client {
} }
client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionTimeout) client.loginTimer = time.AfterFunc(LOGIN_TIMEOUT, client.connectionTimeout)
go client.LookupHostname(IPString(conn.RemoteAddr()))
go client.readCommands() go client.readCommands()
go client.writeReplies() go client.writeReplies()
@ -59,9 +73,15 @@ func NewClient(server *Server, conn net.Conn, hostname string) *Client {
// //
func (client *Client) readCommands() { func (client *Client) readCommands() {
for { done := false
line, err := client.socket.Read() for !done {
if err != nil { select {
case ipAddr := <-client.lookups:
client.server.hostnames <- NewHostnameLookup(client, ipAddr)
case line := <-client.socket.Read():
if line == EOF {
done = true
break break
} }
msg, err := ParseCommand(line) msg, err := ParseCommand(line)
@ -77,10 +97,15 @@ func (client *Client) readCommands() {
msg.SetClient(client) msg.SetClient(client)
client.server.commands <- msg client.server.commands <- msg
} }
}
client.connectionClosed() client.connectionClosed()
} }
func (client *Client) LookupHostname(ipAddr string) {
client.lookups <- ipAddr
}
func (client *Client) connectionClosed() { func (client *Client) connectionClosed() {
msg := &QuitCommand{ msg := &QuitCommand{
message: "connection closed", message: "connection closed",
@ -95,11 +120,18 @@ func (client *Client) connectionClosed() {
func (client *Client) writeReplies() { func (client *Client) writeReplies() {
for reply := range client.replies { for reply := range client.replies {
if reply == EOF {
break
}
if client.socket.Write(reply) != nil { if client.socket.Write(reply) != nil {
break break
} }
} }
client.socket.Close() client.socket.Close()
for _ = range client.replies {
// discard
}
} }
// //
@ -161,8 +193,6 @@ func (client *Client) Register() {
func (client *Client) destroy() { func (client *Client) destroy() {
// clean up self // clean up self
close(client.replies)
client.loginTimer.Stop() client.loginTimer.Stop()
if client.idleTimer != nil { if client.idleTimer != nil {
@ -275,6 +305,7 @@ func (client *Client) Quit(message string) {
} }
client.replies <- RplError(client.server, "connection closed") client.replies <- RplError(client.server, "connection closed")
client.replies <- EOF
client.hasQuit = true client.hasQuit = true
friends := client.Friends() friends := client.Friends()

View File

@ -13,31 +13,16 @@ import (
"time" "time"
) )
type ConnData struct {
conn net.Conn
hostname string
}
func NewConnData(conn net.Conn) *ConnData {
return &ConnData{
conn: conn,
}
}
func (data *ConnData) LookupHostname(newConns chan<- *ConnData) {
data.hostname = AddrLookupHostname(data.conn.RemoteAddr())
newConns <- data
}
type Server struct { type Server struct {
channels ChannelNameMap channels ChannelNameMap
clients ClientNameMap clients ClientNameMap
commands chan Command commands chan Command
newConns chan *ConnData
ctime time.Time ctime time.Time
hostnames chan *HostnameLookup
idle chan *Client idle chan *Client
motdFile string motdFile string
name string name string
newConns chan net.Conn
operators map[string]string operators map[string]string
password string password string
} }
@ -46,12 +31,13 @@ func NewServer(config *Config) *Server {
server := &Server{ server := &Server{
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command), commands: make(chan Command, 16),
newConns: make(chan *ConnData),
ctime: time.Now(), ctime: time.Now(),
idle: make(chan *Client), hostnames: make(chan *HostnameLookup, 16),
idle: make(chan *Client, 16),
motdFile: config.MOTD, motdFile: config.MOTD,
name: config.Name, name: config.Name,
newConns: make(chan net.Conn, 16),
operators: make(map[string]string), operators: make(map[string]string),
password: config.Password, password: config.Password,
} }
@ -70,8 +56,15 @@ func NewServer(config *Config) *Server {
func (server *Server) ReceiveCommands() { func (server *Server) ReceiveCommands() {
for { for {
select { select {
case data := <-server.newConns: case conn := <-server.newConns:
NewClient(server, data.conn, data.hostname) NewClient(server, conn)
case lookup := <-server.hostnames:
if DEBUG_SERVER {
log.Printf("%s setting hostname of %s to %s",
server, lookup.client, lookup.hostname)
}
lookup.client.hostname = lookup.hostname
case client := <-server.idle: case client := <-server.idle:
client.Idle() client.Idle()
@ -168,7 +161,7 @@ func (s *Server) listen(config ListenerConfig) {
log.Printf("%s accept: %s", s, conn.RemoteAddr()) log.Printf("%s accept: %s", s, conn.RemoteAddr())
} }
go NewConnData(conn).LookupHostname(s.newConns) s.newConns <- conn
} }
} }
@ -270,8 +263,7 @@ func (s *Server) Nick() string {
// //
func (msg *ProxyCommand) HandleAuthServer(server *Server) { func (msg *ProxyCommand) HandleAuthServer(server *Server) {
client := msg.Client() go msg.Client().LookupHostname(msg.sourceIP)
client.hostname = LookupHostname(msg.sourceIP)
} }
func (msg *CapCommand) HandleAuthServer(server *Server) { func (msg *CapCommand) HandleAuthServer(server *Server) {

View File

@ -2,6 +2,7 @@ package irc
import ( import (
"bufio" "bufio"
"io"
"log" "log"
"net" "net"
"strings" "strings"
@ -10,11 +11,13 @@ import (
const ( const (
R = '→' R = '→'
W = '←' W = '←'
EOF = ""
) )
type Socket struct { type Socket struct {
closed bool closed bool
conn net.Conn conn net.Conn
read chan string
reader *bufio.Reader reader *bufio.Reader
writer *bufio.Writer writer *bufio.Writer
} }
@ -22,10 +25,13 @@ type Socket struct {
func NewSocket(conn net.Conn) *Socket { func NewSocket(conn net.Conn) *Socket {
socket := &Socket{ socket := &Socket{
conn: conn, conn: conn,
read: make(chan string),
reader: bufio.NewReader(conn), reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn), writer: bufio.NewWriter(conn),
} }
go socket.readLines()
return socket return socket
} }
@ -44,9 +50,9 @@ func (socket *Socket) Close() {
} }
} }
func (socket *Socket) Read() (line string, err error) { func (socket *Socket) readLines() {
for len(line) == 0 { for {
line, err = socket.reader.ReadString('\n') line, err := socket.reader.ReadString('\n')
if socket.isError(err, R) { if socket.isError(err, R) {
break break
} }
@ -58,8 +64,13 @@ func (socket *Socket) Read() (line string, err error) {
if DEBUG_NET { if DEBUG_NET {
log.Printf("%s → %s", socket, line) log.Printf("%s → %s", socket, line)
} }
socket.read <- line
} }
return close(socket.read)
}
func (socket *Socket) Read() <-chan string {
return socket.read
} }
func (socket *Socket) Write(lines ...string) (err error) { func (socket *Socket) Write(lines ...string) (err error) {
@ -93,7 +104,7 @@ func (socket *Socket) WriteLine(line string) (err error) {
func (socket *Socket) isError(err error, dir rune) bool { func (socket *Socket) isError(err error, dir rune) bool {
if err != nil { if err != nil {
if DEBUG_NET { if DEBUG_NET && (err != io.EOF) {
log.Printf("%s %c error: %s", socket, dir, err) log.Printf("%s %c error: %s", socket, dir, err)
} }
return true return true