gnunet-go

GNUnet Bindings for Go
Log | Files | Refs | README | LICENSE

commit 170ce0e4edadb079ad9ab98da23e4fda65c21851
parent 9cae276c86826c5ccdea54e351289ea9cb86a632
Author: Bernd Fix <brf@hoi-polloi.org>
Date:   Fri, 20 Sep 2019 10:06:39 +0200

Fixed issues from 'gnunet-developers' feedback:

* correctly handle expiration time ("never")
* removed busy loop from service implementation ("CPU load")

Diffstat:
Msrc/gnunet/service/gns/gns.go | 5++---
Msrc/gnunet/service/service.go | 6+++++-
Msrc/gnunet/util/format.go | 4++++
Msrc/gnunet/util/time.go | 9+++++++++
4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/gnunet/service/gns/gns.go b/src/gnunet/service/gns/gns.go @@ -3,7 +3,6 @@ package gns import ( "encoding/hex" "io" - "time" "github.com/bfix/gospel/crypto/ed25519" "github.com/bfix/gospel/data" @@ -179,7 +178,7 @@ func (s *GNSService) LookupNamecache(query *crypto.HashCode, zoneKey *ed25519.Pu break } // check if record has expired - if m.Expire > 0 && int64(m.Expire) < time.Now().Unix() { + if util.Expired(m.Expire) { logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) break } @@ -246,7 +245,7 @@ func (s *GNSService) LookupDHT(query *crypto.HashCode, zoneKey *ed25519.PublicKe break } // check if record has expired - if m.Expire > 0 && int64(m.Expire) < time.Now().Unix() { + if util.Expired(m.Expire) { logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) break } diff --git a/src/gnunet/service/service.go b/src/gnunet/service/service.go @@ -21,6 +21,7 @@ type Service interface { type ServiceImpl struct { impl Service hdlr chan transport.Channel + ctrl chan bool srvc transport.ChannelServer name string running bool @@ -31,6 +32,7 @@ func NewServiceImpl(name string, srv Service) *ServiceImpl { return &ServiceImpl{ impl: srv, hdlr: make(chan transport.Channel), + ctrl: make(chan bool), srvc: nil, name: name, running: false, @@ -67,7 +69,8 @@ func (si *ServiceImpl) Start(spec string) (err error) { logger.Printf(logger.DBG, "[%s] Client connected.\n", si.name) go si.impl.ServeClient(transport.NewMsgChannel(ch)) } - default: + case <-si.ctrl: + break loop } } logger.Printf(logger.DBG, "[%s] Service closing.\n", si.name) @@ -85,6 +88,7 @@ func (si *ServiceImpl) Stop() error { return fmt.Errorf("service not running") } si.running = false + si.ctrl <- true logger.Printf(logger.DBG, "[%s] Service terminating.\n", si.name) return si.impl.Stop() diff --git a/src/gnunet/util/format.go b/src/gnunet/util/format.go @@ -3,6 +3,7 @@ package util import ( "encoding/hex" "fmt" + "math" "net" "time" ) @@ -17,6 +18,9 @@ func AddressString(transport string, addr []byte) string { } func Timestamp(ts uint64) string { + if ts == math.MaxUint64 { + return "Never" + } t := time.Unix(int64(ts/(1000*1000)), int64((ts%1000)*1000)) return t.Format(time.RFC3339Nano) } diff --git a/src/gnunet/util/time.go b/src/gnunet/util/time.go @@ -1,6 +1,7 @@ package util import ( + "math" "time" ) @@ -12,6 +13,14 @@ func GetAbsoluteTimeOffset(t time.Duration) uint64 { return getTimestamp(time.Now().Add(t)) } +func Expired(ts uint64) bool { + // check for "never" + if ts == math.MaxUint64 { + return false + } + return ts < uint64(time.Now().Unix()) +} + func getTimestamp(t time.Time) uint64 { secs := t.Unix() usecs := t.Nanosecond() / 1000