Skip to content

Commit

Permalink
chore: update some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 21, 2021
1 parent bbdc1bc commit 65b8f8b
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 43 deletions.
6 changes: 3 additions & 3 deletions connection_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *conn) handleEvents(ev uint32) error {
// Re-ordering can easily introduce bugs and bad side-effects, as I found out painfully in the past.

// We should always check for the EPOLLOUT event first, as we must try to send the leftover data back to
// client when any error occurs on a connection.
// the peer when any error occurs on a connection.
//
// Either an EPOLLOUT or EPOLLERR event may be fired when a connection is refused.
// In either case loopWrite() should take care of it properly:
Expand All @@ -39,8 +39,8 @@ func (c *conn) handleEvents(ev uint32) error {
// If there is pending data in outbound buffer, then we should omit this readable event
// and prioritize the writable events to achieve a higher performance.
//
// Note that the client may send massive amounts of data to server by write() under blocking mode,
// resulting in that it won't receive any responses before the server reads all data from client,
// Note that the peer may send massive amounts of data to server by write() under blocking mode,
// resulting in that it won't receive any responses before the server reads all data from the peer,
// in which case if the server socket send buffer is full, we need to let it go and continue reading
// the data to prevent blocking forever.
if ev&netpoll.InEvents != 0 && (ev&netpoll.OutEvents == 0 || c.outboundBuffer.IsEmpty()) {
Expand Down
8 changes: 4 additions & 4 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type conn struct {
localAddr net.Addr // local addr
remoteAddr net.Addr // remote addr
byteBuffer *bytebuffer.ByteBuffer // bytes buffer for buffering current packet and data in ring-buffer
inboundBuffer *ringbuffer.RingBuffer // buffer for data from client
outboundBuffer *ringbuffer.RingBuffer // buffer for data that is ready to write to client
inboundBuffer *ringbuffer.RingBuffer // buffer for data from the peer
outboundBuffer *ringbuffer.RingBuffer // buffer for data that is ready to write to the peer
pollAttachment *netpoll.PollAttachment // connection attachment for poller
}

Expand Down Expand Up @@ -135,15 +135,15 @@ func (c *conn) write(buf []byte) (err error) {

var n int
if n, err = unix.Write(c.fd, packet); err != nil {
// A temporary error occurs, append the data to outbound buffer, writing it back to client in the next round.
// A temporary error occurs, append the data to outbound buffer, writing it back to the peer in the next round.
if err == unix.EAGAIN {
_, _ = c.outboundBuffer.Write(packet)
err = c.loop.poller.ModReadWrite(c.pollAttachment)
return
}
return c.loop.loopCloseConn(c, os.NewSyscallError("write", err))
}
// Fail to send all data back to client, buffer the leftover data for the next round.
// Fail to send all data back to the peer, buffer the leftover data for the next round.
if n < len(packet) {
_, _ = c.outboundBuffer.Write(packet[n:])
err = c.loop.poller.ModReadWrite(c.pollAttachment)
Expand Down
2 changes: 1 addition & 1 deletion connection_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type stdConn struct {
localAddr net.Addr // local server addr
remoteAddr net.Addr // remote peer addr
byteBuffer *bytebuffer.ByteBuffer // bytes buffer for buffering current packet and data in ring-buffer
inboundBuffer *ringbuffer.RingBuffer // buffer for data from client
inboundBuffer *ringbuffer.RingBuffer // buffer for data from the peer
}

func packTCPConn(c *stdConn, buf []byte) *tcpConn {
Expand Down
12 changes: 6 additions & 6 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func (el *eventloop) loopRead(c *conn) error {
for packet, _ := c.read(); packet != nil; packet, _ = c.read() {
out, action := el.eventHandler.React(packet, c)
if out != nil {
// Encode data and try to write it back to the client, this attempt is based on a fact:
// a client socket waits for the response data after sending request data to the server,
// which makes the client socket writable.
// Encode data and try to write it back to the peer, this attempt is based on a fact:
// the peer socket waits for the response data after sending request data to the server,
// which makes the peer socket writable.
if err = c.write(out); err != nil {
return err
}
Expand All @@ -133,8 +133,8 @@ func (el *eventloop) loopRead(c *conn) error {
return gerrors.ErrServerShutdown
}

// Check the status of connection every loop since it might be closed during writing data back to client due to
// some kind of system error.
// Check the status of connection every loop since it might be closed
// during writing data back to the peer due to some kind of system error.
if !c.opened {
return nil
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func (el *eventloop) loopCloseConn(c *conn, err error) (rerr error) {
return
}

// Send residual data in buffer back to client before actually closing the connection.
// Send residual data in buffer back to the peer before actually closing the connection.
if !c.outboundBuffer.IsEmpty() {
el.eventHandler.PreWrite(c)
head, tail := c.outboundBuffer.PeekAll()
Expand Down
22 changes: 11 additions & 11 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type Conn interface {
// SendTo writes data for UDP sockets, it allows you to send data back to UDP socket in individual goroutines.
SendTo(buf []byte) error

// AsyncWrite writes data to client/connection asynchronously, usually you would call it in individual goroutines
// AsyncWrite writes data to peer asynchronously, usually you would call it in individual goroutines
// instead of the event-loop goroutines.
AsyncWrite(buf []byte) error

Expand All @@ -153,27 +153,27 @@ type (

// OnOpened fires when a new connection has been opened.
// The parameter:c has information about the connection such as it's local and remote address.
// Parameter:out is the return value which is going to be sent back to the client.
// It is generally not recommended to send large amounts of data back to the client in OnOpened.
// Parameter:out is the return value which is going to be sent back to the peer.
// It is generally not recommended to send large amounts of data back to the peer in OnOpened.
//
// Note that the bytes returned by OnOpened will be sent back to client without being encoded.
// Note that the bytes returned by OnOpened will be sent back to the peer without being encoded.
OnOpened(c Conn) (out []byte, action Action)

// OnClosed fires when a connection has been closed.
// The parameter:err is the last known connection error.
OnClosed(c Conn, err error) (action Action)

// PreWrite fires just before a packet is written to the peer socket, this event function is usually where
// you put some code of logging/counting/reporting or any fore operations before writing data to client.
// you put some code of logging/counting/reporting or any fore operations before writing data to the peer.
PreWrite(c Conn)

// AfterWrite fires right after a packet is written to the peer socket, this event function is usually where
// you put the []byte's back to your memory pool.
AfterWrite(c Conn, b []byte)

// React fires when a connection sends the server data.
// Call c.Read() or c.ReadN(n) within the parameter:c to read incoming data from client.
// Parameter:out is the return value which is going to be sent back to the client.
// Call c.Read() or c.ReadN(n) within the parameter:c to read incoming data from the peer.
// Parameter:out is the return value which is going to be sent back to the peer.
React(packet []byte, c Conn) (out []byte, action Action)

// Tick fires immediately after the server starts and will fire again
Expand All @@ -200,7 +200,7 @@ func (es *EventServer) OnShutdown(svr Server) {

// OnOpened fires when a new connection has been opened.
// The parameter:c has information about the connection such as it's local and remote address.
// Parameter:out is the return value which is going to be sent back to the client.
// Parameter:out is the return value which is going to be sent back to the peer.
func (es *EventServer) OnOpened(c Conn) (out []byte, action Action) {
return
}
Expand All @@ -212,7 +212,7 @@ func (es *EventServer) OnClosed(c Conn, err error) (action Action) {
}

// PreWrite fires just before a packet is written to the peer socket, this event function is usually where
// you put some code of logging/counting/reporting or any fore operations before writing data to client.
// you put some code of logging/counting/reporting or any fore operations before writing data to the peer.
func (es *EventServer) PreWrite(c Conn) {
}

Expand All @@ -222,8 +222,8 @@ func (es *EventServer) AfterWrite(c Conn, b []byte) {
}

// React fires when a connection sends the server data.
// Call c.Read() or c.ReadN(n) within the parameter:c to read incoming data from client.
// Parameter:out is the return value which is going to be sent back to the client.
// Call c.Read() or c.ReadN(n) within the parameter:c to read incoming data from the peer.
// Parameter:out is the return value which is going to be sent back to the peer.
func (es *EventServer) React(packet []byte, c Conn) (out []byte, action Action) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/epoll_default_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p *Poller) UrgentTrigger(fn queue.TaskFunc, arg interface{}) (err error) {
}

// Trigger is like UrgentTrigger but it puts task into asyncTaskQueue,
// call this method when the task is not so urgent, for instance writing data back to client.
// call this method when the task is not so urgent, for instance writing data back to the peer.
//
// Note that asyncTaskQueue is a queue with low-priority whose size may grow large and tasks in it may backlog.
func (p *Poller) Trigger(fn queue.TaskFunc, arg interface{}) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/epoll_optimized_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (p *Poller) UrgentTrigger(fn queue.TaskFunc, arg interface{}) (err error) {
}

// Trigger is like UrgentTrigger but it puts task into asyncTaskQueue,
// call this method when the task is not so urgent, for instance writing data back to client.
// call this method when the task is not so urgent, for instance writing data back to the peer.
//
// Note that asyncTaskQueue is a queue with low-priority whose size may grow large and tasks in it may backlog.
func (p *Poller) Trigger(fn queue.TaskFunc, arg interface{}) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/kqueue_default_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (p *Poller) UrgentTrigger(fn queue.TaskFunc, arg interface{}) (err error) {
}

// Trigger is like UrgentTrigger but it puts task into asyncTaskQueue,
// call this method when the task is not so urgent, for instance writing data back to client.
// call this method when the task is not so urgent, for instance writing data back to the peer.
//
// Note that asyncTaskQueue is a queue with low-priority whose size may grow large and tasks in it may backlog.
func (p *Poller) Trigger(fn queue.TaskFunc, arg interface{}) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/netpoll/kqueue_optimized_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (p *Poller) UrgentTrigger(fn queue.TaskFunc, arg interface{}) (err error) {
}

// Trigger is like UrgentTrigger but it puts task into asyncTaskQueue,
// call this method when the task is not so urgent, for instance writing data back to client.
// call this method when the task is not so urgent, for instance writing data back to the peer.
//
// Note that asyncTaskQueue is a queue with low-priority whose size may grow large and tasks in it may backlog.
func (p *Poller) Trigger(fn queue.TaskFunc, arg interface{}) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func init() {
defaultLoggingLevel = zapcore.Level(loggingLevel)
}

// Initializes the inside default logger of client.
// Initializes the inside default logger of gnet.
fileName := os.Getenv("GNET_LOGGING_FILE")
if len(fileName) > 0 {
var err error
Expand Down
12 changes: 6 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ type Options struct {
// potential higher performance.
LockOSThread bool

// ReadBufferCap is the maximum number of bytes that can be read from the client when the readable event comes.
// The default value is 64KB, it can be reduced to avoid starving subsequent client connections.
// ReadBufferCap is the maximum number of bytes that can be read from the peer when the readable event comes.
// The default value is 64KB, it can be reduced to avoid starving the subsequent connections.
//
// Note that ReadBufferCap will be always converted to the least power of two integer value greater than
// or equal to its real amount.
Expand Down Expand Up @@ -95,14 +95,14 @@ type Options struct {
// ICodec encodes and decodes TCP stream.
Codec ICodec

// LogPath the local path where logs will be written, this is the easiest way to set up client logs,
// the client instantiates a default uber-go/zap logger with this given log path, you are also allowed to employ
// you own logger during the client lifetime by implementing the following log.Logger interface.
// LogPath the local path where logs will be written, this is the easiest way to set up logging,
// gnet instantiates a default uber-go/zap logger with this given log path, you are also allowed to employ
// you own logger during the lifetime by implementing the following log.Logger interface.
//
// Note that this option can be overridden by the option Logger.
LogPath string

// LogLevel indicates the logging level inside client, it should be used along with LogPath.
// LogLevel indicates the logging level, it should be used along with LogPath.
LogLevel zapcore.Level

// Logger is the customized logger for logging info, if it is not set,
Expand Down
14 changes: 7 additions & 7 deletions reactor_default_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (el *eventloop) activateSubReactor(lockOSThread bool) {
// Re-ordering can easily introduce bugs and bad side-effects, as I found out painfully in the past.

// We should always check for the EPOLLOUT event first, as we must try to send the leftover data back to
// client when any error occurs on a connection.
// the peer when any error occurs on a connection.
//
// Either an EPOLLOUT or EPOLLERR event may be fired when a connection is refused.
// In either case loopWrite() should take care of it properly:
Expand All @@ -72,8 +72,8 @@ func (el *eventloop) activateSubReactor(lockOSThread bool) {
// If there is pending data in outbound buffer, then we should omit this readable event
// and prioritize the writable events to achieve a higher performance.
//
// Note that the client may send massive amounts of data to server by write() under blocking mode,
// resulting in that it won't receive any responses before the server reads all data from client,
// Note that the peer may send massive amounts of data to server by write() under blocking mode,
// resulting in that it won't receive any responses before the server reads all data from the peer,
// in which case if the server socket send buffer is full, we need to let it go and continue reading
// the data to prevent blocking forever.
if ev&netpoll.InEvents != 0 && (ev&netpoll.OutEvents == 0 || c.outboundBuffer.IsEmpty()) {
Expand Down Expand Up @@ -101,14 +101,14 @@ func (el *eventloop) loopRun(lockOSThread bool) {
el.svr.signalShutdown()
}()

err := el.poller.Polling(func(fd int, ev uint32) (err error) {
err := el.poller.Polling(func(fd int, ev uint32) error {
if c, ok := el.connections[fd]; ok {
// Don't change the ordering of processing EPOLLOUT | EPOLLRDHUP / EPOLLIN unless you're 100%
// sure what you're doing!
// Re-ordering can easily introduce bugs and bad side-effects, as I found out painfully in the past.

// We should always check for the EPOLLOUT event first, as we must try to send the leftover data back to
// client when any error occurs on a connection.
// the peer when any error occurs on a connection.
//
// Either an EPOLLOUT or EPOLLERR event may be fired when a connection is refused.
// In either case loopWrite() should take care of it properly:
Expand All @@ -122,8 +122,8 @@ func (el *eventloop) loopRun(lockOSThread bool) {
// If there is pending data in outbound buffer, then we should omit this readable event
// and prioritize the writable events to achieve a higher performance.
//
// Note that the client may send massive amounts of data to server by write() under blocking mode,
// resulting in that it won't receive any responses before the server read all data from client,
// Note that the peer may send massive amounts of data to server by write() under blocking mode,
// resulting in that it won't receive any responses before the server reads all data from the peer,
// in which case if the socket send buffer is full, we need to let it go and continue reading the data
// to prevent blocking forever.
if ev&netpoll.InEvents != 0 && (ev&netpoll.OutEvents == 0 || c.outboundBuffer.IsEmpty()) {
Expand Down

0 comments on commit 65b8f8b

Please sign in to comment.