* Placate `go vet`
* Reorder the `send` loop, clarify things a little
This commit is contained in:
Shivaram Lingamneni 2018-04-15 01:21:32 -04:00
parent b2f798cf03
commit 4778e7bcc7
2 changed files with 11 additions and 7 deletions

View File

@ -100,7 +100,7 @@ func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
ctime: now, ctime: now,
flags: make(map[modes.Mode]bool), flags: make(map[modes.Mode]bool),
server: server, server: server,
socket: &socket, socket: socket,
nick: "*", // * is used until actual nick is given nick: "*", // * is used until actual nick is given
nickCasefolded: "*", nickCasefolded: "*",
nickMaskString: "*", // * is used until actual nick is given nickMaskString: "*", // * is used until actual nick is given

View File

@ -42,7 +42,7 @@ type Socket struct {
} }
// NewSocket returns a new Socket. // NewSocket returns a new Socket.
func NewSocket(conn net.Conn, maxReadQBytes int, maxSendQBytes int) Socket { func NewSocket(conn net.Conn, maxReadQBytes int, maxSendQBytes int) *Socket {
result := Socket{ result := Socket{
conn: conn, conn: conn,
reader: bufio.NewReaderSize(conn, maxReadQBytes), reader: bufio.NewReaderSize(conn, maxReadQBytes),
@ -50,7 +50,7 @@ func NewSocket(conn net.Conn, maxReadQBytes int, maxSendQBytes int) Socket {
writerSlotOpen: make(chan bool, 1), writerSlotOpen: make(chan bool, 1),
} }
result.writerSlotOpen <- true result.writerSlotOpen <- true
return result return &result
} }
// Close stops a Socket from being able to send/receive any more data. // Close stops a Socket from being able to send/receive any more data.
@ -162,16 +162,20 @@ func (socket *Socket) readyToWrite() bool {
// send actually writes messages to socket.Conn; it may block // send actually writes messages to socket.Conn; it may block
func (socket *Socket) send() { func (socket *Socket) send() {
// one of these checks happens-after every call to Write(), so we can't miss writes for {
for socket.readyToWrite() {
select { select {
case <-socket.writerSlotOpen: case <-socket.writerSlotOpen:
// got the trylock: actually do the write // got the trylock: actually do the write
socket.performWrite() socket.performWrite()
// surrender the trylock:
socket.writerSlotOpen <- true socket.writerSlotOpen <- true
// check if more data came in while we held the trylock:
if !socket.readyToWrite() {
return
}
default: default:
// another goroutine is in progress; exit and wait for them to loop back around // someone else has the trylock; if there's more data to write,
// and observe readyToWrite() again // they'll see if after they release it
return return
} }
} }