mirror of
https://github.com/jeremyd/ergo.git
synced 2026-07-03 23:33:36 -07:00
socket: Very initial SendQ limit
This commit is contained in:
+17
-1
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user