From f791660608619b14455325beb19396803804d893 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 4 Mar 2020 19:31:04 +0100 Subject: [PATCH] Fix bug with atomic for 32 bit architectures --- pkg/router/route_group.go | 6 +++--- pkg/router/route_group_test.go | 7 +++++++ pkg/util/alignment/alignment.go | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 pkg/util/alignment/alignment.go diff --git a/pkg/router/route_group.go b/pkg/router/route_group.go index 3a3cd96ea..121212537 100644 --- a/pkg/router/route_group.go +++ b/pkg/router/route_group.go @@ -67,6 +67,8 @@ type RouteGroup struct { desc routing.RouteDescriptor // describes the route group rt routing.Table + lastSent int64 + // 'tps' is transports used for writing/forward rules. // It should have the same number of elements as 'fwd' // where each element corresponds with the adjacent element in 'fwd'. @@ -79,15 +81,12 @@ type RouteGroup struct { fwd []routing.Rule // forward rules (for writing) rvs []routing.Rule // reverse rules (for reading) - lastSent int64 - // 'readCh' reads in incoming packets of this route group. // - Router should serve call '(*transport.Manager).ReadPacket' in a loop, // and push to the appropriate '(RouteGroup).readCh'. readCh chan []byte // push reads from Router readChMu sync.Mutex readBuf bytes.Buffer // for read overflow - once sync.Once readDeadline deadline.PipeDeadline writeDeadline deadline.PipeDeadline @@ -99,6 +98,7 @@ type RouteGroup struct { closed chan struct{} // used to wait for all the `Close` packets to run through the loop and come back closeDone sync.WaitGroup + once sync.Once } // NewRouteGroup creates a new RouteGroup. diff --git a/pkg/router/route_group_test.go b/pkg/router/route_group_test.go index bb5d07620..e61f747d5 100644 --- a/pkg/router/route_group_test.go +++ b/pkg/router/route_group_test.go @@ -28,6 +28,13 @@ func TestNewRouteGroup(t *testing.T) { require.Equal(t, DefaultRouteGroupConfig(), rg.cfg) } +// Uncomment for debugging +/* +func TestRouteGroupAlignment(t *testing.T) { + alignment.PrintStruct(RouteGroup{}) +} +*/ + func TestRouteGroup_Close(t *testing.T) { rg1, rg2, m1, m2, teardown := setupEnv(t) diff --git a/pkg/util/alignment/alignment.go b/pkg/util/alignment/alignment.go new file mode 100644 index 000000000..aad1a3b8d --- /dev/null +++ b/pkg/util/alignment/alignment.go @@ -0,0 +1,20 @@ +package alignment + +import ( + "fmt" + "reflect" +) + +// PrintStruct prints detailed information of struct fields alignment. +func PrintStruct(v interface{}) { + typ := reflect.TypeOf(v) + fmt.Printf("Struct is %d bytes long\n", typ.Size()) + // We can run through the fields in the structure in order + n := typ.NumField() + for i := 0; i < n; i++ { + field := typ.Field(i) + fmt.Printf("%s at offset %v, size=%d, align=%d\n", + field.Name, field.Offset, field.Type.Size(), + field.Type.Align()) + } +}