1
0
Fork 0
forked from External/ergo

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

@ -30,16 +30,19 @@ type Socket struct {
conn net.Conn
reader *bufio.Reader
MaxSendQBytes uint64
lineToSendExists chan bool
linesToSend []string
linesToSendMutex sync.Mutex
}
// NewSocket returns a new Socket.
func NewSocket(conn net.Conn) Socket {
func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket {
return Socket{
conn: conn,
reader: bufio.NewReader(conn),
MaxSendQBytes: maxSendQBytes,
lineToSendExists: make(chan bool),
}
}
@ -130,6 +133,19 @@ func (socket *Socket) RunSocketWriter() {
case <-socket.lineToSendExists:
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
data := socket.linesToSend[0]
if len(socket.linesToSend) > 1 {