tImplement polling routine for deleting expired nodes. - tordam - A library for peer discovery inside the Tor network HTML git clone https://git.parazyd.org/tordam DIR Log DIR Files DIR Refs DIR README DIR LICENSE --- DIR commit 475f06264c63ca48384bbc8c0f1492fd7d4512c8 DIR parent bf06d4818e02fde075fbb2748afc06b37c6ba376 HTML Author: parazyd <parazyd@dyne.org> Date: Sat, 27 Oct 2018 12:49:19 +0200 Implement polling routine for deleting expired nodes. This adds a -ttl flag that will make a polling goroutine active and make it poll all the nodes it finds in Redis. If their lastseen is more than tthe TTL interval, they will be deleted. This does not disallow them to reappear once they annouce again. Diffstat: M cmd/dam-dir/main.go | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+), 0 deletions(-) --- DIR diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go t@@ -27,6 +27,7 @@ import ( "net/http" "os" "os/exec" + "strconv" "sync" "time" t@@ -209,6 +210,30 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { } } +func pollNodeTTL(interval int64) { + for { + log.Println("Polling redis for expired nodes") + nodes, err := lib.RedisCli.Keys("*.onion").Result() + lib.CheckError(err) + now := time.Time.Unix(time.Now()) + + for _, i := range nodes { + res, err := lib.RedisCli.HGet(i, "lastseen").Result() + lib.CheckError(err) + lastseen, err := strconv.Atoi(res) + lib.CheckError(err) + + diff := int64((now - int64(lastseen)) / 60) + if diff > interval { + log.Printf("Deleting %s from redis because of expiration\n", i) + // TODO: Redis pubsub + lib.RedisCli.Del(i) + } + } + time.Sleep(time.Duration(interval) * time.Minute) + } +} + // handleElse is a noop for anything that isn't /announce. We don't care about // other requests (yet). func handleElse(rw http.ResponseWriter, request *http.Request) {} t@@ -216,8 +241,10 @@ func handleElse(rw http.ResponseWriter, request *http.Request) {} func main() { var wg sync.WaitGroup var t bool + var ttl int64 flag.BoolVar(&t, "t", false, "Mark all new nodes valid initially") + flag.Int64Var(&ttl, "ttl", 0, "Set expiry time in minutes (TTL) for nodes") flag.Parse() if t { t@@ -249,6 +276,12 @@ func main() { wg.Add(1) go srv.ListenAndServe() log.Println("Listening on", ListenAddress) + + if ttl > 0 { + log.Println("Enabling TTL polling.") + go pollNodeTTL(ttl) + } + wg.Wait() os.Exit(1) }