From 6fdae923001d4d195f9eef458a789140ebb307c6 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 10 Dec 2024 13:15:05 +0900 Subject: [PATCH] simplificed monotonic time functions by using x/sys/unix --- Makefile | 3 +-- cmd/config.go | 4 ++++ go.mod | 7 +++---- go.sum | 3 +-- packet.go | 6 +++--- server.go | 4 ++++ system-freebsd.go | 26 -------------------------- system-linux.go | 26 -------------------------- system.go | 32 ++++++++++++++++++++++++++++++++ 9 files changed, 48 insertions(+), 63 deletions(-) delete mode 100644 system-freebsd.go delete mode 100644 system-linux.go create mode 100644 system.go diff --git a/Makefile b/Makefile index 24b7d71..01794dc 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,7 @@ SRCS=\ server-ctl.go \ server-peer.go \ server-ws.go \ - system-freebsd.go \ - system-linux.go + system.go CMD_DATA=\ cmd/tls.crt \ diff --git a/cmd/config.go b/cmd/config.go index 153f523..c126ff4 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -65,6 +65,8 @@ type AppConfig struct { type ServerConfig struct { APP AppConfig `yaml:"app"` +// TODO: add some limits +// max number of clients, max nubmer of peers CTL struct { Service CTLServiceConfig `yaml:"service"` TLS ServerTLSConfig `yaml:"tls"` @@ -79,6 +81,8 @@ type ServerConfig struct { type ClientConfig struct { APP AppConfig `yaml:"app"` +// TODO: add some limits +// max nubmer of peers CTL struct { Service CTLServiceConfig `yaml:"service"` TLS ServerTLSConfig `yaml:"tls"` diff --git a/go.mod b/go.mod index fe7279d..a421e9a 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,14 @@ module hodu go 1.22.0 require ( - github.com/google/uuid v1.6.0 + golang.org/x/net v0.28.0 + golang.org/x/sys v0.24.0 google.golang.org/grpc v1.67.1 google.golang.org/protobuf v1.34.2 + gopkg.in/yaml.v3 v3.0.1 ) require ( - golang.org/x/net v0.28.0 // indirect - golang.org/x/sys v0.24.0 // indirect golang.org/x/text v0.17.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 983750e..344f62a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= @@ -14,6 +12,7 @@ google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/packet.go b/packet.go index 94dc260..1c04675 100644 --- a/packet.go +++ b/packet.go @@ -1,8 +1,8 @@ package hodu -type ConnId uint64 -type RouteId uint32 -type PeerId uint32 +type ConnId uint64 +type RouteId uint32 // keep this in sync with the type of RouteId in hodu.proto +type PeerId uint32 // keep this in sync with the type of RouteId in hodu.proto func MakeRouteStartPacket(route_id RouteId, proto ROUTE_PROTO, addr string, svcnet string) *Packet { return &Packet{ diff --git a/server.go b/server.go index b91699d..198954f 100644 --- a/server.go +++ b/server.go @@ -345,6 +345,10 @@ func (cts *ServerConn) AddNewServerRoute(route_id RouteId, proto ROUTE_PROTO, pt cts.route_mtx.Lock() if cts.route_map[route_id] != nil { + // If this happens, something must be wrong between the server and the client + // most likely, it must be a logic error. the state must not go out of sync + // as the route_id and the peer_id are supposed to be the same between the client + // and the server. cts.route_mtx.Unlock() return nil, fmt.Errorf("existent route id - %d", route_id) } diff --git a/system-freebsd.go b/system-freebsd.go deleted file mode 100644 index d2e79b5..0000000 --- a/system-freebsd.go +++ /dev/null @@ -1,26 +0,0 @@ -//go:build freebsd -package hodu - -import "syscall" -import "unsafe" - -//#include -//import "C" - -// C.CLOCK_MONOTONIC is more accurate when compiled with CGO. -// I want to avoid using it. so assume it is 1 on linux -//const FREEBSD_CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC -const CLOCK_MONOTONIC uintptr = 4 - -// utilize the builtin runtime.nanotime() -//go:noescape -//go:linkname nanotime runtime.nanotime -func nanotime() int64 - -func monotonic_time() uint64 { - var ts syscall.Timespec - var errno syscall.Errno - _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) - if errno != 0 { return uint64(nanotime()) } - return uint64(ts.Nano()) -} diff --git a/system-linux.go b/system-linux.go deleted file mode 100644 index 9c3cb13..0000000 --- a/system-linux.go +++ /dev/null @@ -1,26 +0,0 @@ -//go:build linux -package hodu - -import "syscall" -import "unsafe" - -//#include -//import "C" - -// C.CLOCK_MONOTONIC is more accurate when compiled with CGO. -// I want to avoid using it. so assume it is 1 on linux -//const CLOCK_MONOTONIC uintptr = C.CLOCK_MONOTONIC -const CLOCK_MONOTONIC uintptr = 1 - -// utilize the builtin runtime.nanotime() -//go:noescape -//go:linkname nanotime runtime.nanotime -func nanotime() int64 - -func monotonic_time() uint64 { - var ts syscall.Timespec - var errno syscall.Errno - _, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)), 0) - if errno != 0 { return uint64(nanotime()) } - return uint64(ts.Nano()) -} diff --git a/system.go b/system.go new file mode 100644 index 0000000..5f624fd --- /dev/null +++ b/system.go @@ -0,0 +1,32 @@ +package hodu + +import "syscall" +import "unsafe" +import "golang.org/x/sys/unix" + + +// utilize the builtin runtime.nanotime() +//go:noescape +//go:linkname nanotime runtime.nanotime +func nanotime() int64 + +func monotonic_time() uint64 { + var n int64 + var err error + var uts unix.Timespec + + n = nanotime() // hopefully it's faster than a system call. say, vdso is utilized. + if (n >= 0) { return uint64(n) } + + err = unix.ClockGettime(unix.CLOCK_MONOTONIC, &uts) + if err != nil { + //var errno syscall.Errno + var r uintptr + var sts syscall.Timespec + r, _, _/*errno*/ = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, unix.CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&sts)), 0) + if r == ^uintptr(0) { return uint64(n) } // may be negative cast to unsigned. no other fall-back + return uint64(sts.Nano()) + } + + return uint64(uts.Nano()) +}