Skip to content

Commit 30aac11

Browse files
committed
docs: update inaccurate comments on Conn.Next/Peek
Also trim some long comments
1 parent 58b972b commit 30aac11

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

gnet.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,20 @@ type Reader interface {
231231
io.Reader
232232
io.WriterTo
233233

234-
// Next returns a slice containing the next n bytes from the buffer,
235-
// advancing the buffer as if the bytes had been returned by Read.
236-
// Calling this method has the same effect as calling Peek and Discard.
237-
// If the number of the available bytes is less than requested, a pair of (0, io.ErrShortBuffer)
238-
// is returned.
234+
// Next returns the next n bytes and advance the inbound buffer.
235+
// buf must not be used in a new goroutine. Otherwise, use Read instead.
239236
//
240-
// Note that the []byte buf returned by Next() is not allowed to be passed to a new goroutine,
241-
// as this []byte will be reused within event-loop.
242-
// If you have to use buf in a new goroutine, then you need to make a copy of buf and pass this copy
243-
// to that new goroutine.
237+
// If the number of the available bytes is less than requested,
238+
// a pair of (0, io.ErrShortBuffer) is returned.
244239
Next(n int) (buf []byte, err error)
245240

246-
// Peek returns the next n bytes without advancing the inbound buffer, the returned bytes
247-
// remain valid until a Discard is called. If the number of the available bytes is
248-
// less than requested, a pair of (0, io.ErrShortBuffer) is returned.
241+
// Peek returns the next n bytes without advancing the inbound buffer,
242+
// the returned bytes remain valid until a Discard is called.
243+
// buf must neither be used in a new goroutine nor anywhere after the call
244+
// to Discard, make a copy of buf manually or use Read otherwise.
249245
//
250-
// Note that the []byte buf returned by Peek() is not allowed to be passed to a new goroutine,
251-
// as this []byte will be reused within event-loop.
252-
// If you have to use buf in a new goroutine, then you need to make a copy of buf and pass this copy
253-
// to that new goroutine.
246+
// If the number of the available bytes is less than requested,
247+
// a pair of (0, io.ErrShortBuffer) is returned.
254248
Peek(n int) (buf []byte, err error)
255249

256250
// Discard advances the inbound buffer with next n bytes, returning the number of bytes discarded.
@@ -497,20 +491,17 @@ type (
497491

498492
// OnTraffic fires when a socket receives data from the remote.
499493
//
500-
// Note that the []byte returned from Conn.Peek(int)/Conn.Next(int) is not allowed to be passed to a new goroutine,
501-
// as this []byte will be reused within event-loop after OnTraffic() returns.
502-
// If you have to use this []byte in a new goroutine, you should either make a copy of it or call Conn.Read([]byte)
503-
// to read data into your own []byte, then pass the new []byte to the new goroutine.
494+
// Also check out the comments on Reader and Writer interfaces.
504495
OnTraffic(c Conn) (action Action)
505496

506497
// OnTick fires immediately after the engine starts and will fire again
507498
// following the duration specified by the delay return value.
508499
OnTick() (delay time.Duration, action Action)
509500
}
510501

511-
// BuiltinEventEngine is a built-in implementation of EventHandler which sets up each method with a default implementation,
512-
// you can compose it with your own implementation of EventHandler when you don't want to implement all methods
513-
// in EventHandler.
502+
// BuiltinEventEngine is a built-in implementation of EventHandler which sets up
503+
// each method with a default implementation, you can compose it with your own
504+
// implementation of EventHandler when you don't want to implement all methods in EventHandler.
514505
BuiltinEventEngine struct{}
515506
)
516507

0 commit comments

Comments
 (0)