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

async hostname lookups

This commit is contained in:
Jeremy Latt 2014-02-20 11:51:45 -08:00
parent c0dc733351
commit 97881b555d
2 changed files with 23 additions and 7 deletions

View File

@ -33,14 +33,14 @@ type Client struct {
username string username string
} }
func NewClient(server *Server, conn net.Conn) *Client { func NewClient(server *Server, conn net.Conn, hostname string) *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: AddrLookupHostname(conn.RemoteAddr()), hostname: hostname,
phase: server.InitPhase(), phase: server.InitPhase(),
server: server, server: server,
socket: NewSocket(conn), socket: NewSocket(conn),

View File

@ -13,11 +13,27 @@ 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 net.Conn newConns chan *ConnData
ctime time.Time ctime time.Time
idle chan *Client idle chan *Client
motdFile string motdFile string
@ -31,7 +47,7 @@ func NewServer(config *Config) *Server {
channels: make(ChannelNameMap), channels: make(ChannelNameMap),
clients: make(ClientNameMap), clients: make(ClientNameMap),
commands: make(chan Command), commands: make(chan Command),
newConns: make(chan net.Conn), newConns: make(chan *ConnData),
ctime: time.Now(), ctime: time.Now(),
idle: make(chan *Client), idle: make(chan *Client),
motdFile: config.MOTD, motdFile: config.MOTD,
@ -54,8 +70,8 @@ func NewServer(config *Config) *Server {
func (server *Server) ReceiveCommands() { func (server *Server) ReceiveCommands() {
for { for {
select { select {
case conn := <-server.newConns: case data := <-server.newConns:
NewClient(server, conn) NewClient(server, data.conn, data.hostname)
case client := <-server.idle: case client := <-server.idle:
client.Idle() client.Idle()
@ -152,7 +168,7 @@ func (s *Server) listen(config ListenerConfig) {
log.Printf("%s accept: %s", s, conn.RemoteAddr()) log.Printf("%s accept: %s", s, conn.RemoteAddr())
} }
s.newConns <- conn go NewConnData(conn).LookupHostname(s.newConns)
} }
} }