Skip to content

Conversation

Copy link

Copilot AI commented Dec 20, 2025

In multi-replica Kubernetes deployments, each replica overwrites database byte counts instead of accumulating them, causing incorrect statistics.

Changes

Delta-based accumulation

  • Track last seen byte counts per peer in memory (peerStats map with mutex)
  • Calculate deltas between syncs and atomically accumulate in database
  • Added Storage.AddByteCounts(publicKey, rxDelta, txDelta) using SQL UPDATE with addition

Counter reset handling

  • Detect negative deltas (WireGuard interface restart)
  • Log warning and use current value as delta to continue accumulation

Memory management

  • Clean up tracking when peers disconnect from replica

Implementation

Before:

device.ReceiveBytes = peer.ReceiveBytes      // Overwrites
device.TransmitBytes = peer.TransmitBytes
d.SaveDevice(device)

After:

rxDelta := currentRx - lastStats.ReceiveBytes  // Calculate delta
txDelta := currentTx - lastStats.TransmitBytes
d.storage.AddByteCounts(publicKey, rxDelta, txDelta)  // Atomic: receive_bytes = receive_bytes + ?

Each replica independently tracks its connected peers and contributes deltas to the shared database, enabling correct cumulative totals across all replicas.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dns.ffmuc.net
    • Triggering command: `/tmp/go-build3546985116/b452/dnsproxy.test /tmp/go-build3546985116/b452/dnsproxy.test -test.testlogfile=/tmp/go-build3546985116/b452/testlog.txt -test.paniconexit0 -test.timeout=10m0s /tmp/go-build3546985116/b327/vet.cfg rnal/filetype/build.go 0920028/b188//cgo.o ux_amd64/compile --gdwarf-5 pkg/mod/github.ccommit -o ux_amd64/compileAdd comprehensive tests for AddByteCounts functionality

Co-authored-by: Goliath-ifaceassert -W g_.a /tmp/go-build1930920028/b188/ -data-downloader /home/REDACTED/go//opt/hostedtoolcache/go/1.24.11/x64/pkg/tool/linux_amd64/vet rt =0 -data-downloader-buildtags` (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Issue with Rx/Tx statistics</issue_title>
<issue_description>Hi!
I have installation of wg-access-server in kubernetes with postgres backend.

The statistics of received/transmitted bytes are not working correctly. It seems that when updating data in a multi-replicated installation, the data is not summarized, but overwritten.

Application version: wg-access-server:v0.6.0

Do you have any ideas how to fix this?</issue_description>

Comments on the Issue (you are @copilot in this section)

@DasSkelett Yeah, it just takes the value from WireGuard for each connected peer and shoves it into the database, overwriting what's already there from other instances or before restarts.

Is it important for you that the data is correctly summed?
It is possible to do that, but it requires some work. We'd have to track the stats for each peer internally, then calculate the increase and add it to the value already in the db.</comment_new>
<comment_new>@DasSkelett

By the way, it might be better to use the following algorithm:

* write current statistics to dictionary

* clear locally

* sum the values in the database with the values from the dictionary

To my knowledge WireGuard doesn't allow us to clear or edit the bytes counter, only read it</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@mergeable
Copy link

mergeable bot commented Dec 20, 2025

Thanks for creating a pull request! A maintainer will review your changes shortly. Please don't be discouraged if it takes a while.

Copilot AI and others added 6 commits December 20, 2025 11:07
Co-authored-by: GoliathLabs <8057646+GoliathLabs@users.noreply.github.com>
Co-authored-by: GoliathLabs <8057646+GoliathLabs@users.noreply.github.com>
…nction

Co-authored-by: GoliathLabs <8057646+GoliathLabs@users.noreply.github.com>
Co-authored-by: GoliathLabs <8057646+GoliathLabs@users.noreply.github.com>
…cation

Co-authored-by: GoliathLabs <8057646+GoliathLabs@users.noreply.github.com>
Co-authored-by: GoliathLabs <8057646+GoliathLabs@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Rx/Tx statistics in wg-access-server Fix Rx/Tx statistics overwrite in multi-replica deployments Dec 20, 2025
Copilot AI requested a review from GoliathLabs December 20, 2025 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with Rx/Tx statistics

2 participants