mirror of
https://github.com/ergochat/ergo.git
synced 2025-02-19 15:10:42 +01:00
Merge pull request #218 from slingamn/socketwriter.1
refactor irc.Socket
This commit is contained in:
commit
7cfa75a59e
@ -87,7 +87,9 @@ type Client struct {
|
|||||||
// NewClient returns a client with all the appropriate info setup.
|
// NewClient returns a client with all the appropriate info setup.
|
||||||
func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
|
func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
socket := NewSocket(conn, server.MaxSendQBytes)
|
limits := server.Limits()
|
||||||
|
fullLineLenLimit := limits.LineLen.Tags + limits.LineLen.Rest
|
||||||
|
socket := NewSocket(conn, fullLineLenLimit*2, server.MaxSendQBytes())
|
||||||
go socket.RunSocketWriter()
|
go socket.RunSocketWriter()
|
||||||
client := &Client{
|
client := &Client{
|
||||||
atime: now,
|
atime: now,
|
||||||
@ -253,7 +255,11 @@ func (client *Client) run() {
|
|||||||
|
|
||||||
line, err = client.socket.Read()
|
line, err = client.socket.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.Quit("connection closed")
|
quitMessage := "connection closed"
|
||||||
|
if err == errReadQ {
|
||||||
|
quitMessage = "readQ exceeded"
|
||||||
|
}
|
||||||
|
client.Quit(quitMessage)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ type Config struct {
|
|||||||
ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
|
ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
|
||||||
WebIRC []webircConfig `yaml:"webirc"`
|
WebIRC []webircConfig `yaml:"webirc"`
|
||||||
MaxSendQString string `yaml:"max-sendq"`
|
MaxSendQString string `yaml:"max-sendq"`
|
||||||
MaxSendQBytes uint64
|
MaxSendQBytes int
|
||||||
ConnectionLimiter connection_limits.LimiterConfig `yaml:"connection-limits"`
|
ConnectionLimiter connection_limits.LimiterConfig `yaml:"connection-limits"`
|
||||||
ConnectionThrottler connection_limits.ThrottlerConfig `yaml:"connection-throttling"`
|
ConnectionThrottler connection_limits.ThrottlerConfig `yaml:"connection-throttling"`
|
||||||
}
|
}
|
||||||
@ -530,10 +530,11 @@ func LoadConfig(filename string) (config *Config, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Server.MaxSendQBytes, err = bytefmt.ToBytes(config.Server.MaxSendQString)
|
maxSendQBytes, err := bytefmt.ToBytes(config.Server.MaxSendQString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Could not parse maximum SendQ size (make sure it only contains whole numbers): %s", err.Error())
|
return nil, fmt.Errorf("Could not parse maximum SendQ size (make sure it only contains whole numbers): %s", err.Error())
|
||||||
}
|
}
|
||||||
|
config.Server.MaxSendQBytes = int(maxSendQBytes)
|
||||||
|
|
||||||
// get language files
|
// get language files
|
||||||
config.Languages.Data = make(map[string]languages.LangData)
|
config.Languages.Data = make(map[string]languages.LangData)
|
||||||
|
@ -40,6 +40,7 @@ var (
|
|||||||
var (
|
var (
|
||||||
errNoPeerCerts = errors.New("Client did not provide a certificate")
|
errNoPeerCerts = errors.New("Client did not provide a certificate")
|
||||||
errNotTLS = errors.New("Not a TLS connection")
|
errNotTLS = errors.New("Not a TLS connection")
|
||||||
|
errReadQ = errors.New("ReadQ Exceeded")
|
||||||
)
|
)
|
||||||
|
|
||||||
// String Errors
|
// String Errors
|
||||||
|
@ -6,8 +6,17 @@ package irc
|
|||||||
import (
|
import (
|
||||||
"github.com/oragono/oragono/irc/isupport"
|
"github.com/oragono/oragono/irc/isupport"
|
||||||
"github.com/oragono/oragono/irc/modes"
|
"github.com/oragono/oragono/irc/modes"
|
||||||
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (server *Server) MaxSendQBytes() int {
|
||||||
|
return int(atomic.LoadUint32(&server.maxSendQBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *Server) SetMaxSendQBytes(m int) {
|
||||||
|
atomic.StoreUint32(&server.maxSendQBytes, uint32(m))
|
||||||
|
}
|
||||||
|
|
||||||
func (server *Server) ISupport() *isupport.List {
|
func (server *Server) ISupport() *isupport.List {
|
||||||
server.configurableStateMutex.RLock()
|
server.configurableStateMutex.RLock()
|
||||||
defer server.configurableStateMutex.RUnlock()
|
defer server.configurableStateMutex.RUnlock()
|
||||||
|
@ -109,7 +109,7 @@ type Server struct {
|
|||||||
limits Limits
|
limits Limits
|
||||||
listeners map[string]*ListenerWrapper
|
listeners map[string]*ListenerWrapper
|
||||||
logger *logger.Manager
|
logger *logger.Manager
|
||||||
MaxSendQBytes uint64
|
maxSendQBytes uint32
|
||||||
monitorManager *MonitorManager
|
monitorManager *MonitorManager
|
||||||
motdLines []string
|
motdLines []string
|
||||||
name string
|
name string
|
||||||
@ -928,16 +928,7 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
|
|||||||
server.configurableStateMutex.Unlock()
|
server.configurableStateMutex.Unlock()
|
||||||
|
|
||||||
// set new sendqueue size
|
// set new sendqueue size
|
||||||
if config.Server.MaxSendQBytes != server.MaxSendQBytes {
|
server.SetMaxSendQBytes(config.Server.MaxSendQBytes)
|
||||||
server.configurableStateMutex.Lock()
|
|
||||||
server.MaxSendQBytes = config.Server.MaxSendQBytes
|
|
||||||
server.configurableStateMutex.Unlock()
|
|
||||||
|
|
||||||
// update on all clients
|
|
||||||
for _, sClient := range server.clients.AllClients() {
|
|
||||||
sClient.socket.MaxSendQBytes = config.Server.MaxSendQBytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
server.loadMOTD(config.Server.MOTD, config.Server.MOTDFormatting)
|
server.loadMOTD(config.Server.MOTD, config.Server.MOTDFormatting)
|
||||||
|
|
||||||
|
175
irc/socket.go
175
irc/socket.go
@ -9,6 +9,7 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@ -18,47 +19,44 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
handshakeTimeout, _ = time.ParseDuration("5s")
|
handshakeTimeout, _ = time.ParseDuration("5s")
|
||||||
|
errSendQExceeded = errors.New("SendQ exceeded")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Socket represents an IRC socket.
|
// Socket represents an IRC socket.
|
||||||
type Socket struct {
|
type Socket struct {
|
||||||
|
sync.Mutex
|
||||||
|
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
reader *bufio.Reader
|
reader *bufio.Reader
|
||||||
|
|
||||||
MaxSendQBytes uint64
|
maxSendQBytes int
|
||||||
|
|
||||||
|
// coordination system for asynchronous writes
|
||||||
|
buffer []byte
|
||||||
|
lineToSendExists chan bool
|
||||||
|
|
||||||
closed bool
|
closed bool
|
||||||
closedMutex sync.Mutex
|
sendQExceeded bool
|
||||||
|
|
||||||
finalData string // what to send when we die
|
finalData string // what to send when we die
|
||||||
finalDataMutex sync.Mutex
|
|
||||||
|
|
||||||
lineToSendExists chan bool
|
|
||||||
linesToSend []string
|
|
||||||
linesToSendMutex sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSocket returns a new Socket.
|
// NewSocket returns a new Socket.
|
||||||
func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket {
|
func NewSocket(conn net.Conn, maxReadQBytes int, maxSendQBytes int) Socket {
|
||||||
return Socket{
|
return Socket{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
reader: bufio.NewReader(conn),
|
reader: bufio.NewReaderSize(conn, maxReadQBytes),
|
||||||
MaxSendQBytes: maxSendQBytes,
|
maxSendQBytes: maxSendQBytes,
|
||||||
lineToSendExists: make(chan bool),
|
lineToSendExists: make(chan bool, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
func (socket *Socket) Close() {
|
func (socket *Socket) Close() {
|
||||||
socket.closedMutex.Lock()
|
socket.Lock()
|
||||||
defer socket.closedMutex.Unlock()
|
|
||||||
if socket.closed {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
socket.closed = true
|
socket.closed = true
|
||||||
|
socket.Unlock()
|
||||||
|
|
||||||
// force close loop to happen if it hasn't already
|
socket.wakeWriter()
|
||||||
go socket.timedFillLineToSendExists(200 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CertFP returns the fingerprint of the certificate provided by the client.
|
// CertFP returns the fingerprint of the certificate provided by the client.
|
||||||
@ -94,10 +92,13 @@ func (socket *Socket) Read() (string, error) {
|
|||||||
return "", io.EOF
|
return "", io.EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
lineBytes, err := socket.reader.ReadBytes('\n')
|
lineBytes, isPrefix, err := socket.reader.ReadLine()
|
||||||
|
if isPrefix {
|
||||||
|
return "", errReadQ
|
||||||
|
}
|
||||||
|
|
||||||
// convert bytes to string
|
// convert bytes to string
|
||||||
line := string(lineBytes[:])
|
line := string(lineBytes)
|
||||||
|
|
||||||
// read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes
|
// read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
@ -110,128 +111,84 @@ func (socket *Socket) Read() (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.TrimRight(line, "\r\n"), nil
|
return line, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write sends the given string out of Socket.
|
// Write sends the given string out of Socket.
|
||||||
func (socket *Socket) Write(data string) error {
|
func (socket *Socket) Write(data string) (err error) {
|
||||||
if socket.IsClosed() {
|
socket.Lock()
|
||||||
return io.EOF
|
if socket.closed {
|
||||||
|
err = io.EOF
|
||||||
|
} else if len(data)+len(socket.buffer) > socket.maxSendQBytes {
|
||||||
|
socket.sendQExceeded = true
|
||||||
|
err = errSendQExceeded
|
||||||
|
} else {
|
||||||
|
socket.buffer = append(socket.buffer, data...)
|
||||||
}
|
}
|
||||||
|
socket.Unlock()
|
||||||
|
|
||||||
socket.linesToSendMutex.Lock()
|
socket.wakeWriter()
|
||||||
socket.linesToSend = append(socket.linesToSend, data)
|
return
|
||||||
socket.linesToSendMutex.Unlock()
|
|
||||||
|
|
||||||
go socket.timedFillLineToSendExists(15 * time.Second)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// timedFillLineToSendExists either sends the note or times out.
|
// wakeWriter wakes up the goroutine that actually performs the write, without blocking
|
||||||
func (socket *Socket) timedFillLineToSendExists(duration time.Duration) {
|
func (socket *Socket) wakeWriter() {
|
||||||
lineToSendTimeout := time.NewTimer(duration)
|
// nonblocking send to the channel, no-op if it's full
|
||||||
defer lineToSendTimeout.Stop()
|
|
||||||
select {
|
select {
|
||||||
case socket.lineToSendExists <- true:
|
case socket.lineToSendExists <- true:
|
||||||
// passed data successfully
|
default:
|
||||||
case <-lineToSendTimeout.C:
|
|
||||||
// timed out send
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFinalData sets the final data to send when the SocketWriter closes.
|
// SetFinalData sets the final data to send when the SocketWriter closes.
|
||||||
func (socket *Socket) SetFinalData(data string) {
|
func (socket *Socket) SetFinalData(data string) {
|
||||||
socket.finalDataMutex.Lock()
|
socket.Lock()
|
||||||
|
defer socket.Unlock()
|
||||||
socket.finalData = data
|
socket.finalData = data
|
||||||
socket.finalDataMutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsClosed returns whether the socket is closed.
|
// IsClosed returns whether the socket is closed.
|
||||||
func (socket *Socket) IsClosed() bool {
|
func (socket *Socket) IsClosed() bool {
|
||||||
socket.closedMutex.Lock()
|
socket.Lock()
|
||||||
defer socket.closedMutex.Unlock()
|
defer socket.Unlock()
|
||||||
return socket.closed
|
return socket.closed
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunSocketWriter starts writing messages to the outgoing socket.
|
// RunSocketWriter starts writing messages to the outgoing socket.
|
||||||
func (socket *Socket) RunSocketWriter() {
|
func (socket *Socket) RunSocketWriter() {
|
||||||
for {
|
localBuffer := make([]byte, 0)
|
||||||
|
shouldStop := false
|
||||||
|
for !shouldStop {
|
||||||
// wait for new lines
|
// wait for new lines
|
||||||
select {
|
select {
|
||||||
case <-socket.lineToSendExists:
|
case <-socket.lineToSendExists:
|
||||||
socket.linesToSendMutex.Lock()
|
// retrieve the buffered data, clear the buffer
|
||||||
|
socket.Lock()
|
||||||
|
localBuffer = append(localBuffer, socket.buffer...)
|
||||||
|
socket.buffer = socket.buffer[:0]
|
||||||
|
socket.Unlock()
|
||||||
|
|
||||||
// check if we're closed
|
_, err := socket.conn.Write(localBuffer)
|
||||||
if socket.IsClosed() {
|
localBuffer = localBuffer[:0]
|
||||||
socket.linesToSendMutex.Unlock()
|
|
||||||
break
|
socket.Lock()
|
||||||
|
shouldStop = (err != nil) || socket.closed || socket.sendQExceeded
|
||||||
|
socket.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check whether new lines actually exist or not
|
// mark the socket closed (if someone hasn't already), then write error lines
|
||||||
if len(socket.linesToSend) < 1 {
|
socket.Lock()
|
||||||
socket.linesToSendMutex.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// check sendq
|
|
||||||
var sendQBytes uint64
|
|
||||||
for _, line := range socket.linesToSend {
|
|
||||||
sendQBytes += uint64(len(line))
|
|
||||||
if socket.MaxSendQBytes < sendQBytes {
|
|
||||||
// don't unlock mutex because this break is just to escape this for loop
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if socket.MaxSendQBytes < sendQBytes {
|
|
||||||
socket.SetFinalData("\r\nERROR :SendQ Exceeded\r\n")
|
|
||||||
socket.linesToSendMutex.Unlock()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// get all existing data
|
|
||||||
data := strings.Join(socket.linesToSend, "")
|
|
||||||
socket.linesToSend = []string{}
|
|
||||||
|
|
||||||
socket.linesToSendMutex.Unlock()
|
|
||||||
|
|
||||||
// write data
|
|
||||||
if 0 < len(data) {
|
|
||||||
_, err := socket.conn.Write([]byte(data))
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if socket.IsClosed() {
|
|
||||||
// error out or we've been closed
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// force closure of socket
|
|
||||||
socket.closedMutex.Lock()
|
|
||||||
if !socket.closed {
|
|
||||||
socket.closed = true
|
socket.closed = true
|
||||||
|
finalData := socket.finalData
|
||||||
|
if socket.sendQExceeded {
|
||||||
|
finalData = "\r\nERROR :SendQ Exceeded\r\n"
|
||||||
}
|
}
|
||||||
socket.closedMutex.Unlock()
|
socket.Unlock()
|
||||||
|
if finalData != "" {
|
||||||
// write error lines
|
socket.conn.Write([]byte(finalData))
|
||||||
socket.finalDataMutex.Lock()
|
|
||||||
if 0 < len(socket.finalData) {
|
|
||||||
socket.conn.Write([]byte(socket.finalData))
|
|
||||||
}
|
}
|
||||||
socket.finalDataMutex.Unlock()
|
|
||||||
|
|
||||||
// close the connection
|
// close the connection
|
||||||
socket.conn.Close()
|
socket.conn.Close()
|
||||||
|
|
||||||
// empty the lineToSendExists channel
|
|
||||||
for 0 < len(socket.lineToSendExists) {
|
|
||||||
<-socket.lineToSendExists
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteLine writes the given line out of Socket.
|
|
||||||
func (socket *Socket) WriteLine(line string) error {
|
|
||||||
return socket.Write(line + "\r\n")
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user