-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
conntrack: add connection tracker wrapper based on connfu
Use builder pattern to return implementation with only required features. Use connfu to handle CloseWrite() and ReadFrom() propagation.
- Loading branch information
Showing
4 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package conntrack | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/mmatczuk/connfu" | ||
) | ||
|
||
// Observer allows to observe the number of bytes read and written from a connection. | ||
type Observer struct { | ||
rx atomic.Uint64 | ||
tx atomic.Uint64 | ||
} | ||
|
||
// Rx returns the number of bytes read from the connection. | ||
// It requires TrackTraffic to be set to true, otherwise it returns 0. | ||
func (o *Observer) Rx() uint64 { | ||
return o.rx.Load() | ||
} | ||
|
||
// Tx returns the number of bytes written to the connection. | ||
// It requires TrackTraffic to be set to true, otherwise it returns 0. | ||
func (o *Observer) Tx() uint64 { | ||
return o.tx.Load() | ||
} | ||
|
||
func (o *Observer) addRx(n uint64) { | ||
o.rx.Add(n) | ||
} | ||
|
||
func (o *Observer) addTx(n uint64) { | ||
o.tx.Add(n) | ||
} | ||
|
||
type closeConn struct { | ||
net.Conn | ||
l closeListener // this is a field to avoid ambiguous selector error on Close method | ||
} | ||
|
||
func (c *closeConn) Close() error { | ||
return c.l.Close() | ||
} | ||
|
||
type closeListener struct { | ||
close func() error | ||
once sync.Once | ||
onClose func() | ||
} | ||
|
||
func (c *closeListener) Close() error { | ||
err := c.close() | ||
c.once.Do(c.onClose) | ||
return err | ||
} | ||
|
||
// conn is a net.Conn that tracks the number of bytes read and written. | ||
// It needs to be configured before first use by setting TrackTraffic and onClose if needed. | ||
type conn struct { | ||
net.Conn | ||
o Observer | ||
} | ||
|
||
func (c *conn) Read(p []byte) (n int, err error) { | ||
n, err = c.Conn.Read(p) | ||
c.o.addRx(uint64(n)) | ||
return | ||
} | ||
|
||
func (c *conn) Write(p []byte) (n int, err error) { | ||
n, err = c.Conn.Write(p) | ||
c.o.addTx(uint64(n)) | ||
return | ||
} | ||
|
||
func (c *conn) ReadFrom(r io.Reader) (n int64, err error) { | ||
n, err = c.Conn.(io.ReaderFrom).ReadFrom(r) | ||
c.o.addTx(uint64(n)) | ||
return | ||
} | ||
|
||
type Builder struct { | ||
// TrackTraffic enables counting of bytes read and written by the connection. | ||
// Use Rx and Tx to get the number of bytes read and written. | ||
TrackTraffic bool | ||
|
||
// OnClose is called after the underlying connection is closed and before the Close method returns. | ||
// OnClose is called at most once. | ||
OnClose func() | ||
} | ||
|
||
func (b Builder) Build(c net.Conn) net.Conn { | ||
wc, _ := b.BuildWithObserver(c) | ||
return wc | ||
} | ||
|
||
func (b Builder) BuildWithObserver(c net.Conn) (net.Conn, *Observer) { | ||
var ( | ||
wc net.Conn | ||
co *Observer | ||
) | ||
|
||
if b.TrackTraffic { | ||
if b.OnClose != nil { | ||
cc := &struct { | ||
conn | ||
closeListener | ||
}{ | ||
conn: conn{Conn: c}, | ||
closeListener: closeListener{ | ||
close: c.Close, | ||
onClose: b.OnClose, | ||
}, | ||
} | ||
wc = cc | ||
co = &cc.conn.o | ||
} else { | ||
cc := &conn{ | ||
Conn: c, | ||
} | ||
wc = cc | ||
co = &cc.o | ||
} | ||
} else { | ||
if b.OnClose == nil { | ||
wc = c | ||
} else { | ||
wc = &closeConn{ | ||
Conn: c, | ||
l: closeListener{ | ||
close: c.Close, | ||
onClose: b.OnClose, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
return connfu.Combine(wc, c), co | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package conntrack | ||
|
||
import ( | ||
"crypto/tls" | ||
"io" | ||
"net" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
type closeWriter interface { | ||
CloseWrite() error | ||
} | ||
|
||
func TestBuildTCP(t *testing.T) { | ||
wc, co := Builder{TrackTraffic: true}.Build(new(net.TCPConn)) | ||
if co == nil { | ||
t.Error("Expected a connection observer") | ||
} | ||
if _, ok := wc.(io.ReaderFrom); ok != (runtime.GOOS == "linux") { | ||
t.Error("ReaderFrom missmatch") | ||
} | ||
if _, ok := wc.(io.WriterTo); ok { | ||
t.Error("Unexpected WriterTo") | ||
} | ||
if _, ok := wc.(closeWriter); !ok { | ||
t.Error("Missing CloseWrite") | ||
} | ||
} | ||
|
||
func TestBuildTLS(t *testing.T) { | ||
wc, co := Builder{TrackTraffic: true}.Build(new(tls.Conn)) | ||
if co == nil { | ||
t.Error("Expected a connection observer") | ||
} | ||
if _, ok := wc.(io.ReaderFrom); ok { | ||
t.Error("Unexpected ReaderFrom") | ||
} | ||
if _, ok := wc.(io.WriterTo); ok { | ||
t.Error("Unexpected WriterTo") | ||
} | ||
if _, ok := wc.(closeWriter); !ok { | ||
t.Error("Missing CloseWrite") | ||
} | ||
} | ||
|
||
func TestBuildOnClose(t *testing.T) { | ||
var closed bool | ||
wc, co := Builder{OnClose: func() { closed = true }}.Build(new(net.TCPConn)) | ||
if co != nil { | ||
t.Error("Unexpected connection observer") | ||
} | ||
if _, ok := wc.(io.ReaderFrom); ok != (runtime.GOOS == "linux") { | ||
t.Error("ReaderFrom missmatch") | ||
} | ||
if _, ok := wc.(io.WriterTo); ok { | ||
t.Error("Unexpected WriterTo") | ||
} | ||
if _, ok := wc.(closeWriter); !ok { | ||
t.Error("Missing CloseWrite") | ||
} | ||
wc.Close() | ||
if !closed { | ||
t.Error("OnClose not called") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters