Skip to content

Commit

Permalink
fix: Ensure necessary fields are 64-bit aligned (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg committed Dec 2, 2020
1 parent e30c62e commit 4575c8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions proxy/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ type CertSource interface {
// Client is a type to handle connecting to a Server. All fields are required
// unless otherwise specified.
type Client struct {
// ConnectionsCounter is used to enforce the optional maxConnections limit
ConnectionsCounter uint64

// MaxConnections is the maximum number of connections to establish
// before refusing new connections. 0 means no limit.
MaxConnections uint64

// Port designates which remote port should be used when connecting to
// instances. This value is defined by the server-side code, but for now it
// should always be 3307.
Expand Down Expand Up @@ -95,13 +102,6 @@ type Client struct {
// RefreshCertBuffer is the amount of time before the configuration expires to
// attempt to refresh it. If not set, it defaults to 5 minutes.
RefreshCfgBuffer time.Duration

// MaxConnections is the maximum number of connections to establish
// before refusing new connections. 0 means no limit.
MaxConnections uint64

// ConnectionsCounter is used to enforce the optional maxConnections limit
ConnectionsCounter uint64
}

type cacheEntry struct {
Expand Down
9 changes: 9 additions & 0 deletions proxy/proxy/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync/atomic"
"testing"
"time"
"unsafe"
)

const instance = "instance-name"
Expand Down Expand Up @@ -285,3 +286,11 @@ func TestRefreshTimer(t *testing.T) {
t.Error("expected cert to be refreshed.")
}
}

func TestSyncAtomicAlignment(t *testing.T) {
// The sync/atomic pkg has a bug that requires the developer to guarantee 64-bit alignment when using 64-bit functions on 32-bit systems.
c := &Client{}
if a := unsafe.Offsetof(c.ConnectionsCounter); a%64 != 0 {
t.Errorf("Client.ConnectionsCounter is not aligned: want %v, got %v", 0, a)
}
}

0 comments on commit 4575c8f

Please sign in to comment.