Skip to content

Commit

Permalink
fix: align properly fields which are updated atomically
Browse files Browse the repository at this point in the history
This fixes alignment of the fields on 32-bit arches.

Fixes #30

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Feb 10, 2021
1 parent 63c1c8b commit 36cb201
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on: [push, pull_request]
name: Lint
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.28
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.14.x, 1.15.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test -v ./...
- name: Test Race
run: go test -v -race ./...
- name: Bench
run: go test -v -bench . -benchmem -run nothing ./...
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

8 changes: 5 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type Client struct {
}

type transport struct {
// these fields are updated with atomic operations,
// so they should be at the top for proper alignment
lostPacketsPeriod int64
lostPacketsOverall int64

maxPacketSize int
tagFormat *TagFormat

Expand All @@ -53,9 +58,6 @@ type transport struct {
shutdown chan struct{}
shutdownOnce sync.Once
shutdownWg sync.WaitGroup

lostPacketsPeriod int64
lostPacketsOverall int64
}

// NewClient creates new statsd client and starts background processing
Expand Down
15 changes: 11 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func setupListener(t *testing.T) (*net.UDPConn, chan []byte) {
t.Error(err)
}

received := make(chan []byte)
received := make(chan []byte, 1024)

go func() {
for {
Expand Down Expand Up @@ -305,8 +305,8 @@ func TestConcurrent(t *testing.T) {
wg1.Done()
}()

workers := 32
count := 4096
workers := 16
count := 1024

for i := 0; i < workers; i++ {
wg2.Add(1)
Expand Down Expand Up @@ -334,7 +334,14 @@ func TestConcurrent(t *testing.T) {

_ = client.Close()

time.Sleep(time.Second)
// wait for 10 seconds for all the packets to be received
for i := 0; i < 10; i++ {
if atomic.LoadInt64(&totalSent) == atomic.LoadInt64(&totalReceived) {
break
}

time.Sleep(time.Second)
}

_ = inSocket.Close()
close(received)
Expand Down

0 comments on commit 36cb201

Please sign in to comment.