Files
x/observer/stats/wrapper/io.go
T
ginuerzh 7c95d40e05 fix(observer): add nil guards, fix resource leak, silent event drops, and missing doc comments
- WrapUDPConn, WrapPacketConn, WrapListener: add nil guards for pc/ln params
- NewGRPCPlugin: return no-op observer instead of nil on connection error
- gRPC plugin: fix resource leak by wiring Close() through observerWrapper
  and service.Close() lifecycle
- Both plugins: log unknown event types instead of silently dropping them
- httpPlugin: remove dead nil check for always-non-nil client field
- Stats.Reset: update core interface doc to match implementation behavior
- Add doc comments to all exported symbols across the observer package
2026-05-24 23:13:52 +08:00

41 lines
812 B
Go

package wrapper
import (
"io"
"github.com/go-gost/core/observer/stats"
)
// readWriter is an io.ReadWriter with Stats.
type readWriter struct {
io.ReadWriter
stats stats.Stats
}
// WrapReadWriter wraps an io.ReadWriter to track input and output bytes.
// If rw or stats is nil, the original ReadWriter is returned unchanged.
func WrapReadWriter(rw io.ReadWriter, stats stats.Stats) io.ReadWriter {
if rw == nil || stats == nil {
return rw
}
return &readWriter{
ReadWriter: rw,
stats: stats,
}
}
func (p *readWriter) Read(b []byte) (n int, err error) {
n, err = p.ReadWriter.Read(b)
p.stats.Add(stats.KindInputBytes, int64(n))
return
}
func (p *readWriter) Write(b []byte) (n int, err error) {
n, err = p.ReadWriter.Write(b)
p.stats.Add(stats.KindOutputBytes, int64(n))
return
}