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

socket: Very initial SendQ limit

This commit is contained in:
Daniel Oaks 2017-03-14 08:12:39 +10:00
parent de4db1c6ef
commit f29a5f0e70
5 changed files with 44 additions and 2 deletions

View File

@ -74,7 +74,7 @@ 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) socket := NewSocket(conn, server.MaxSendQBytes)
go socket.RunSocketWriter() go socket.RunSocketWriter()
client := &Client{ client := &Client{
atime: now, atime: now,

View File

@ -14,6 +14,8 @@ import (
"strings" "strings"
"time" "time"
"code.cloudfoundry.org/bytefmt"
"github.com/DanielOaks/oragono/irc/custime" "github.com/DanielOaks/oragono/irc/custime"
"github.com/DanielOaks/oragono/irc/logger" "github.com/DanielOaks/oragono/irc/logger"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -171,6 +173,8 @@ type Config struct {
RestAPI RestAPIConfig `yaml:"rest-api"` RestAPI RestAPIConfig `yaml:"rest-api"`
CheckIdent bool `yaml:"check-ident"` CheckIdent bool `yaml:"check-ident"`
MOTD string MOTD string
MaxSendQString string `yaml:"max-sendq"`
MaxSendQBytes uint64
ConnectionLimits ConnectionLimitsConfig `yaml:"connection-limits"` ConnectionLimits ConnectionLimitsConfig `yaml:"connection-limits"`
ConnectionThrottle ConnectionThrottleConfig `yaml:"connection-throttling"` ConnectionThrottle ConnectionThrottleConfig `yaml:"connection-throttling"`
} }
@ -433,5 +437,10 @@ func LoadConfig(filename string) (config *Config, err error) {
} }
config.Logging = newLogConfigs config.Logging = newLogConfigs
config.Server.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())
}
return config, nil return config, nil
} }

View File

@ -104,6 +104,7 @@ type Server struct {
listeners map[string]ListenerInterface listeners map[string]ListenerInterface
listenerUpdateMutex sync.Mutex listenerUpdateMutex sync.Mutex
logger *logger.Manager logger *logger.Manager
MaxSendQBytes uint64
monitoring map[string][]Client monitoring map[string][]Client
motdLines []string motdLines []string
name string name string
@ -211,6 +212,7 @@ func NewServer(configFilename string, config *Config, logger *logger.Manager) (*
}, },
listeners: make(map[string]ListenerInterface), listeners: make(map[string]ListenerInterface),
logger: logger, logger: logger,
MaxSendQBytes: config.Server.MaxSendQBytes,
monitoring: make(map[string][]Client), monitoring: make(map[string][]Client),
name: config.Server.Name, name: config.Server.Name,
nameCasefolded: casefoldedName, nameCasefolded: casefoldedName,
@ -1413,6 +1415,18 @@ func (server *Server) rehash() error {
accountReg := NewAccountRegistration(config.Accounts.Registration) accountReg := NewAccountRegistration(config.Accounts.Registration)
server.accountRegistration = &accountReg server.accountRegistration = &accountReg
// set new sendqueue size
if config.Server.MaxSendQBytes != server.MaxSendQBytes {
server.MaxSendQBytes = config.Server.MaxSendQBytes
// update on all clients
server.clients.ByNickMutex.RLock()
for _, sClient := range server.clients.ByNick {
sClient.socket.MaxSendQBytes = config.Server.MaxSendQBytes
}
server.clients.ByNickMutex.RUnlock()
}
// set RPL_ISUPPORT // set RPL_ISUPPORT
oldISupportList := server.isupport oldISupportList := server.isupport
server.setISupport() server.setISupport()

View File

@ -30,16 +30,19 @@ type Socket struct {
conn net.Conn conn net.Conn
reader *bufio.Reader reader *bufio.Reader
MaxSendQBytes uint64
lineToSendExists chan bool lineToSendExists chan bool
linesToSend []string linesToSend []string
linesToSendMutex sync.Mutex linesToSendMutex sync.Mutex
} }
// NewSocket returns a new Socket. // NewSocket returns a new Socket.
func NewSocket(conn net.Conn) Socket { func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket {
return Socket{ return Socket{
conn: conn, conn: conn,
reader: bufio.NewReader(conn), reader: bufio.NewReader(conn),
MaxSendQBytes: maxSendQBytes,
lineToSendExists: make(chan bool), lineToSendExists: make(chan bool),
} }
} }
@ -130,6 +133,19 @@ func (socket *Socket) RunSocketWriter() {
case <-socket.lineToSendExists: case <-socket.lineToSendExists:
socket.linesToSendMutex.Lock() socket.linesToSendMutex.Lock()
// check sendq
var sendQBytes uint64
for _, line := range socket.linesToSend {
sendQBytes += uint64(len(line))
if socket.MaxSendQBytes < sendQBytes {
break
}
}
if socket.MaxSendQBytes < sendQBytes {
socket.conn.Write([]byte("\r\nERROR :SendQ Exceeded\r\n"))
break
}
// get data // get data
data := socket.linesToSend[0] data := socket.linesToSend[0]
if len(socket.linesToSend) > 1 { if len(socket.linesToSend) > 1 {

View File

@ -65,6 +65,9 @@ server:
# if you change the motd, you should move it to ircd.motd # if you change the motd, you should move it to ircd.motd
motd: oragono.motd motd: oragono.motd
# maximum length of clients' sendQ in bytes
max-sendq: 16k
# maximum number of connections per subnet # maximum number of connections per subnet
connection-limits: connection-limits:
# whether to throttle limits or not # whether to throttle limits or not