aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBernd Fix <brf@hoi-polloi.org>2019-09-20 10:06:39 +0200
committerBernd Fix <brf@hoi-polloi.org>2019-09-20 10:06:39 +0200
commit170ce0e4edadb079ad9ab98da23e4fda65c21851 (patch)
tree267c7d14da381b775e1db14052f229eb36c18c99 /src
parent9cae276c86826c5ccdea54e351289ea9cb86a632 (diff)
downloadgnunet-go-170ce0e4edadb079ad9ab98da23e4fda65c21851.tar.gz
gnunet-go-170ce0e4edadb079ad9ab98da23e4fda65c21851.zip
Fixed issues from 'gnunet-developers' feedback:
* correctly handle expiration time ("never") * removed busy loop from service implementation ("CPU load")
Diffstat (limited to 'src')
-rw-r--r--src/gnunet/service/gns/gns.go5
-rw-r--r--src/gnunet/service/service.go6
-rw-r--r--src/gnunet/util/format.go4
-rw-r--r--src/gnunet/util/time.go9
4 files changed, 20 insertions, 4 deletions
diff --git a/src/gnunet/service/gns/gns.go b/src/gnunet/service/gns/gns.go
index 9ac8b37..fb93c4b 100644
--- a/src/gnunet/service/gns/gns.go
+++ b/src/gnunet/service/gns/gns.go
@@ -3,7 +3,6 @@ package gns
3import ( 3import (
4 "encoding/hex" 4 "encoding/hex"
5 "io" 5 "io"
6 "time"
7 6
8 "github.com/bfix/gospel/crypto/ed25519" 7 "github.com/bfix/gospel/crypto/ed25519"
9 "github.com/bfix/gospel/data" 8 "github.com/bfix/gospel/data"
@@ -179,7 +178,7 @@ func (s *GNSService) LookupNamecache(query *crypto.HashCode, zoneKey *ed25519.Pu
179 break 178 break
180 } 179 }
181 // check if record has expired 180 // check if record has expired
182 if m.Expire > 0 && int64(m.Expire) < time.Now().Unix() { 181 if util.Expired(m.Expire) {
183 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) 182 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire))
184 break 183 break
185 } 184 }
@@ -246,7 +245,7 @@ func (s *GNSService) LookupDHT(query *crypto.HashCode, zoneKey *ed25519.PublicKe
246 break 245 break
247 } 246 }
248 // check if record has expired 247 // check if record has expired
249 if m.Expire > 0 && int64(m.Expire) < time.Now().Unix() { 248 if util.Expired(m.Expire) {
250 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire)) 249 logger.Printf(logger.ERROR, "[gns] block expired at %s\n", util.Timestamp(m.Expire))
251 break 250 break
252 } 251 }
diff --git a/src/gnunet/service/service.go b/src/gnunet/service/service.go
index f43c385..5b44d47 100644
--- a/src/gnunet/service/service.go
+++ b/src/gnunet/service/service.go
@@ -21,6 +21,7 @@ type Service interface {
21type ServiceImpl struct { 21type ServiceImpl struct {
22 impl Service 22 impl Service
23 hdlr chan transport.Channel 23 hdlr chan transport.Channel
24 ctrl chan bool
24 srvc transport.ChannelServer 25 srvc transport.ChannelServer
25 name string 26 name string
26 running bool 27 running bool
@@ -31,6 +32,7 @@ func NewServiceImpl(name string, srv Service) *ServiceImpl {
31 return &ServiceImpl{ 32 return &ServiceImpl{
32 impl: srv, 33 impl: srv,
33 hdlr: make(chan transport.Channel), 34 hdlr: make(chan transport.Channel),
35 ctrl: make(chan bool),
34 srvc: nil, 36 srvc: nil,
35 name: name, 37 name: name,
36 running: false, 38 running: false,
@@ -67,7 +69,8 @@ func (si *ServiceImpl) Start(spec string) (err error) {
67 logger.Printf(logger.DBG, "[%s] Client connected.\n", si.name) 69 logger.Printf(logger.DBG, "[%s] Client connected.\n", si.name)
68 go si.impl.ServeClient(transport.NewMsgChannel(ch)) 70 go si.impl.ServeClient(transport.NewMsgChannel(ch))
69 } 71 }
70 default: 72 case <-si.ctrl:
73 break loop
71 } 74 }
72 } 75 }
73 logger.Printf(logger.DBG, "[%s] Service closing.\n", si.name) 76 logger.Printf(logger.DBG, "[%s] Service closing.\n", si.name)
@@ -85,6 +88,7 @@ func (si *ServiceImpl) Stop() error {
85 return fmt.Errorf("service not running") 88 return fmt.Errorf("service not running")
86 } 89 }
87 si.running = false 90 si.running = false
91 si.ctrl <- true
88 logger.Printf(logger.DBG, "[%s] Service terminating.\n", si.name) 92 logger.Printf(logger.DBG, "[%s] Service terminating.\n", si.name)
89 93
90 return si.impl.Stop() 94 return si.impl.Stop()
diff --git a/src/gnunet/util/format.go b/src/gnunet/util/format.go
index b52418f..4d42520 100644
--- a/src/gnunet/util/format.go
+++ b/src/gnunet/util/format.go
@@ -3,6 +3,7 @@ package util
3import ( 3import (
4 "encoding/hex" 4 "encoding/hex"
5 "fmt" 5 "fmt"
6 "math"
6 "net" 7 "net"
7 "time" 8 "time"
8) 9)
@@ -17,6 +18,9 @@ func AddressString(transport string, addr []byte) string {
17} 18}
18 19
19func Timestamp(ts uint64) string { 20func Timestamp(ts uint64) string {
21 if ts == math.MaxUint64 {
22 return "Never"
23 }
20 t := time.Unix(int64(ts/(1000*1000)), int64((ts%1000)*1000)) 24 t := time.Unix(int64(ts/(1000*1000)), int64((ts%1000)*1000))
21 return t.Format(time.RFC3339Nano) 25 return t.Format(time.RFC3339Nano)
22} 26}
diff --git a/src/gnunet/util/time.go b/src/gnunet/util/time.go
index 89109a7..78b0722 100644
--- a/src/gnunet/util/time.go
+++ b/src/gnunet/util/time.go
@@ -1,6 +1,7 @@
1package util 1package util
2 2
3import ( 3import (
4 "math"
4 "time" 5 "time"
5) 6)
6 7
@@ -12,6 +13,14 @@ func GetAbsoluteTimeOffset(t time.Duration) uint64 {
12 return getTimestamp(time.Now().Add(t)) 13 return getTimestamp(time.Now().Add(t))
13} 14}
14 15
16func Expired(ts uint64) bool {
17 // check for "never"
18 if ts == math.MaxUint64 {
19 return false
20 }
21 return ts < uint64(time.Now().Unix())
22}
23
15func getTimestamp(t time.Time) uint64 { 24func getTimestamp(t time.Time) uint64 {
16 secs := t.Unix() 25 secs := t.Unix()
17 usecs := t.Nanosecond() / 1000 26 usecs := t.Nanosecond() / 1000