3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 11:59:40 +01:00
This commit is contained in:
Shivaram Lingamneni 2018-03-17 21:32:12 -04:00
parent 8fd1446627
commit d1f5c59eef
6 changed files with 34 additions and 23 deletions

View File

@ -86,7 +86,9 @@ type Client struct {
// NewClient returns a client with all the appropriate info setup.
func NewClient(server *Server, conn net.Conn, isTLS bool) *Client {
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()
client := &Client{
atime: now,
@ -230,7 +232,11 @@ func (client *Client) run() {
line, err = client.socket.Read()
if err != nil {
client.Quit("connection closed")
quitMessage := "connection closed"
if err == errReadQ {
quitMessage = "readQ exceeded"
}
client.Quit(quitMessage)
break
}

View File

@ -208,7 +208,7 @@ type Config struct {
ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
WebIRC []webircConfig `yaml:"webirc"`
MaxSendQString string `yaml:"max-sendq"`
MaxSendQBytes uint64
MaxSendQBytes int
ConnectionLimiter connection_limits.LimiterConfig `yaml:"connection-limits"`
ConnectionThrottler connection_limits.ThrottlerConfig `yaml:"connection-throttling"`
}
@ -520,10 +520,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 {
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
config.Languages.Data = make(map[string]languages.LangData)

View File

@ -40,6 +40,7 @@ var (
var (
errNoPeerCerts = errors.New("Client did not provide a certificate")
errNotTLS = errors.New("Not a TLS connection")
errReadQ = errors.New("ReadQ Exceeded")
)
// String Errors

View File

@ -6,8 +6,17 @@ package irc
import (
"github.com/oragono/oragono/irc/isupport"
"github.com/oragono/oragono/irc/modes"
"sync/atomic"
)
func (server *Server) MaxSendQBytes() int {
return int(atomic.LoadUint64(&server.maxSendQBytes))
}
func (server *Server) SetMaxSendQBytes(m int) {
atomic.StoreUint64(&server.maxSendQBytes, uint64(m))
}
func (server *Server) ISupport() *isupport.List {
server.configurableStateMutex.RLock()
defer server.configurableStateMutex.RUnlock()

View File

@ -109,7 +109,7 @@ type Server struct {
limits Limits
listeners map[string]*ListenerWrapper
logger *logger.Manager
MaxSendQBytes uint64
maxSendQBytes uint64
monitorManager *MonitorManager
motdLines []string
name string
@ -932,16 +932,7 @@ func (server *Server) applyConfig(config *Config, initial bool) error {
server.configurableStateMutex.Unlock()
// set new sendqueue size
if config.Server.MaxSendQBytes != 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.SetMaxSendQBytes(config.Server.MaxSendQBytes)
// set RPL_ISUPPORT
var newISupportReplies [][]string

View File

@ -29,7 +29,7 @@ type Socket struct {
conn net.Conn
reader *bufio.Reader
MaxSendQBytes uint64
maxSendQBytes int
// coordination system for asynchronous writes
buffer []byte
@ -41,11 +41,11 @@ type Socket struct {
}
// 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{
conn: conn,
reader: bufio.NewReader(conn),
MaxSendQBytes: maxSendQBytes,
reader: bufio.NewReaderSize(conn, maxReadQBytes),
maxSendQBytes: maxSendQBytes,
lineToSendExists: make(chan bool, 1),
}
}
@ -92,10 +92,13 @@ func (socket *Socket) Read() (string, error) {
return "", io.EOF
}
lineBytes, err := socket.reader.ReadBytes('\n')
lineBytes, isPrefix, err := socket.reader.ReadLine()
if isPrefix {
return "", errReadQ
}
// convert bytes to string
line := string(lineBytes[:])
line := string(lineBytes)
// read last message properly (such as ERROR/QUIT/etc), just fail next reads/writes
if err == io.EOF {
@ -108,7 +111,7 @@ func (socket *Socket) Read() (string, error) {
return "", err
}
return strings.TrimRight(line, "\r\n"), nil
return line, nil
}
// Write sends the given string out of Socket.
@ -116,7 +119,7 @@ func (socket *Socket) Write(data string) (err error) {
socket.Lock()
if socket.closed {
err = io.EOF
} else if uint64(len(data)+len(socket.buffer)) > socket.MaxSendQBytes {
} else if len(data)+len(socket.buffer) > socket.maxSendQBytes {
socket.sendQExceeded = true
err = errSendQExceeded
} else {