simplificed monotonic time functions by using x/sys/unix

This commit is contained in:
hyung-hwan 2024-12-10 13:15:05 +09:00
parent 2d15f0d2a4
commit 6fdae92300
9 changed files with 48 additions and 63 deletions

View File

@ -14,8 +14,7 @@ SRCS=\
server-ctl.go \ server-ctl.go \
server-peer.go \ server-peer.go \
server-ws.go \ server-ws.go \
system-freebsd.go \ system.go
system-linux.go
CMD_DATA=\ CMD_DATA=\
cmd/tls.crt \ cmd/tls.crt \

View File

@ -65,6 +65,8 @@ type AppConfig struct {
type ServerConfig struct { type ServerConfig struct {
APP AppConfig `yaml:"app"` APP AppConfig `yaml:"app"`
// TODO: add some limits
// max number of clients, max nubmer of peers
CTL struct { CTL struct {
Service CTLServiceConfig `yaml:"service"` Service CTLServiceConfig `yaml:"service"`
TLS ServerTLSConfig `yaml:"tls"` TLS ServerTLSConfig `yaml:"tls"`
@ -79,6 +81,8 @@ type ServerConfig struct {
type ClientConfig struct { type ClientConfig struct {
APP AppConfig `yaml:"app"` APP AppConfig `yaml:"app"`
// TODO: add some limits
// max nubmer of peers
CTL struct { CTL struct {
Service CTLServiceConfig `yaml:"service"` Service CTLServiceConfig `yaml:"service"`
TLS ServerTLSConfig `yaml:"tls"` TLS ServerTLSConfig `yaml:"tls"`

7
go.mod
View File

@ -3,15 +3,14 @@ module hodu
go 1.22.0 go 1.22.0
require ( 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/grpc v1.67.1
google.golang.org/protobuf v1.34.2 google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
) )
require ( 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 golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )

3
go.sum
View File

@ -1,7 +1,5 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 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/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 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= 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= 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/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= 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/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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,8 +1,8 @@
package hodu package hodu
type ConnId uint64 type ConnId uint64
type RouteId uint32 type RouteId uint32 // keep this in sync with the type of RouteId in hodu.proto
type PeerId uint32 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 { func MakeRouteStartPacket(route_id RouteId, proto ROUTE_PROTO, addr string, svcnet string) *Packet {
return &Packet{ return &Packet{

View File

@ -345,6 +345,10 @@ func (cts *ServerConn) AddNewServerRoute(route_id RouteId, proto ROUTE_PROTO, pt
cts.route_mtx.Lock() cts.route_mtx.Lock()
if cts.route_map[route_id] != nil { 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() cts.route_mtx.Unlock()
return nil, fmt.Errorf("existent route id - %d", route_id) return nil, fmt.Errorf("existent route id - %d", route_id)
} }

View File

@ -1,26 +0,0 @@
//go:build freebsd
package hodu
import "syscall"
import "unsafe"
//#include <time.h>
//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())
}

View File

@ -1,26 +0,0 @@
//go:build linux
package hodu
import "syscall"
import "unsafe"
//#include <time.h>
//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())
}

32
system.go Normal file
View File

@ -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())
}