Skip to content

Commit e1e8a34

Browse files
committed
Introduce a postWrite hook.
If you are sending large amounts of data (multiple megabytes) and you don't want to allocate that large a byte slice, one option is to send the data and have a mechanism to Wake() the connection up after the write is done. This will require a postWrite(conn) hook that also provides a reference to the connection for which data was just written. This way the application can maintain state in the context and in the postWrite hook, call Wake() and send the next batch of data when the Data() hook gets called.
1 parent fe60817 commit e1e8a34

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

evio.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ type Events struct {
115115
Detached func(c Conn, rwc io.ReadWriteCloser) (action Action)
116116
// PreWrite fires just before any data is written to any client socket.
117117
PreWrite func()
118+
// PostWrite fires just after any data is written to any client socket.
119+
PostWrite func(c Conn)
118120
// Data fires when a connection sends the server data.
119121
// The in parameter is the incoming data.
120122
// Use the out return value to write data to the connection.

evio_std.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ func stdloopRead(s *stdserver, l *stdloop, c *stdconn, in []byte) error {
384384
s.events.PreWrite()
385385
}
386386
c.conn.Write(out)
387+
if s.events.PostWrite != nil {
388+
s.events.PostWrite(c)
389+
}
387390
}
388391
switch action {
389392
case Shutdown:
@@ -405,6 +408,9 @@ func stdloopReadUDP(s *stdserver, l *stdloop, c *stdudpconn) error {
405408
s.events.PreWrite()
406409
}
407410
s.lns[c.addrIndex].pconn.WriteTo(out, c.remoteAddr)
411+
if s.events.PostWrite != nil {
412+
s.events.PostWrite(c)
413+
}
408414
}
409415
switch action {
410416
case Shutdown:
@@ -439,6 +445,9 @@ func stdloopAccept(s *stdserver, l *stdloop, c *stdconn) error {
439445
s.events.PreWrite()
440446
}
441447
c.conn.Write(out)
448+
if s.events.PostWrite != nil {
449+
s.events.PostWrite(c)
450+
}
442451
}
443452
if opts.TCPKeepAlive > 0 {
444453
if c, ok := c.conn.(*net.TCPConn); ok {

evio_unix.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ func loopUDPRead(s *server, l *loop, lnidx, fd int) error {
333333
s.events.PreWrite()
334334
}
335335
syscall.Sendto(fd, out, 0, sa)
336+
if s.events.PostWrite != nil {
337+
s.events.PostWrite(c)
338+
}
336339
}
337340
switch action {
338341
case Shutdown:
@@ -391,6 +394,9 @@ func loopWrite(s *server, l *loop, c *conn) error {
391394
if len(c.out) == 0 && c.action == None {
392395
l.poll.ModRead(c.fd)
393396
}
397+
if len(c.out) == 0 && s.events.PostWrite != nil {
398+
s.events.PostWrite(c)
399+
}
394400
return nil
395401
}
396402

0 commit comments

Comments
 (0)