Skip to content

Commit 67fa1a7

Browse files
committed
use func provider for client info
1 parent c56378a commit 67fa1a7

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

tracker/clientcontext/injector.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ type ClientContextInjector struct {
2525
inboundRule *boundsRule
2626
outboundRule *boundsRule
2727
logger log.ContextLogger
28-
info ClientInfo
28+
getInfo GetClientInfoFn
2929
}
3030

3131
// NewClientContextInjector creates a tracker for injecting client info.
32-
func NewClientContextInjector(info ClientInfo, bounds MatchBounds, logger log.ContextLogger) *ClientContextInjector {
32+
func NewClientContextInjector(fn GetClientInfoFn, bounds MatchBounds, logger log.ContextLogger) *ClientContextInjector {
3333
return &ClientContextInjector{
3434
inboundRule: newBoundsRule(bounds.Inbound),
3535
outboundRule: newBoundsRule(bounds.Outbound),
3636
logger: logger,
37-
info: info,
37+
getInfo: fn,
3838
}
3939
}
4040

@@ -49,7 +49,8 @@ func (t *ClientContextInjector) RoutedConnection(
4949
if !t.inboundRule.match(metadata.Inbound) || !t.outboundRule.match(matchOutbound.Tag()) {
5050
return conn
5151
}
52-
return newWriteConn(conn, &t.info)
52+
info := t.getInfo()
53+
return newWriteConn(conn, &info)
5354
}
5455

5556
// RoutedPacketConnection wraps the packet connection for writing client info.
@@ -63,7 +64,8 @@ func (t *ClientContextInjector) RoutedPacketConnection(
6364
if !t.inboundRule.match(metadata.Inbound) || !t.outboundRule.match(matchOutbound.Tag()) {
6465
return conn
6566
}
66-
return newWritePacketConn(conn, metadata, &t.info)
67+
info := t.getInfo()
68+
return newWritePacketConn(conn, metadata, &info)
6769
}
6870

6971
func (t *ClientContextInjector) UpdateBounds(bounds MatchBounds) {

tracker/clientcontext/tracker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ package clientcontext
3535

3636
const packetPrefix = "CLIENTINFO "
3737

38+
type GetClientInfoFn func() ClientInfo
39+
3840
// ClientInfo holds information about the client user/device.
3941
type ClientInfo struct {
4042
DeviceID string

tracker/clientcontext/tracker_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ func TestIntegration(t *testing.T) {
6464
CountryCode: "US",
6565
Version: "9.0",
6666
}
67+
infoFn := func() ClientInfo { return cInfo }
6768
t.Run("with ClientContext tracker", func(t *testing.T) {
6869
mTracker.info = nil
69-
tracker := NewClientContextInjector(cInfo, MatchBounds{[]string{"any"}, []string{"any"}}, logger)
70+
tracker := NewClientContextInjector(infoFn, MatchBounds{[]string{"any"}, []string{"any"}}, logger)
7071
runTrackerTest(ctx, t, clientOpts, tracker, httpClient, addr)
7172
require.Equal(t, &cInfo, mTracker.info)
7273
})

0 commit comments

Comments
 (0)