<commit_before>package dht

import (
	"context"
	"encoding/json"
	"strings"
	"sync"

	"github.com/google/uuid"
	"github.com/sirupsen/logrus"

	messagebus "github.com/nimona/go-nimona-messagebus"
	net "github.com/nimona/go-nimona-net"
)

const (
	protocolID = "dht-kad/v1"
)

// DHTNode is the struct that implements the dht protocol
type DHTNode struct {
	localPeer  net.Peer
	store      *Store
	net        net.Network
	messageBus messagebus.MessageBus
	queries    map[string]*query
	mt         sync.RWMutex
}

func NewDHTNode(bps []net.Peer, lp net.Peer, nn net.Network) (*DHTNode, error) {
	// create new routing table
	st, _ := newStore()

	// create messagebud
	mb, err := messagebus.New(protocolID, nn)
	if err != nil {
		return nil, err
	}

	// Create DHT node
	nd := &DHTNode{
		localPeer:  lp,
		store:      st,
		net:        nn,
		messageBus: mb,
		queries:    map[string]*query{},
	}

	// Register message bus, message handler
	if err := mb.HandleMessage(nd.messageHandler); err != nil {
		return nil, err
	}

	// Add bootstrap nodes
	for _, peer := range bps {
		if err := nd.storePeer(peer, true); err != nil {
			logrus.WithField("error", err).Error("new could not store peer")
		}
		if err := nd.putPeer(peer); err != nil {
			logrus.WithField("error", err).Error("new could not put peer")
		}
	}

	// start refresh worker
	// ticker := time.NewTicker(15 * time.Second)
	// quit := make(chan struct{})
	go func() {
		// refresh for the first time
		nd.refresh()
		// and then just wait
		// for {
		// 	select {
		// 	case <-ticker.C:
		// 		nd.refresh()
		// 	case <-quit:
		// 		ticker.Stop()
		// 		return
		// 	}
		// }
	}()

	return nd, nil
}

func (nd *DHTNode) refresh() {
	logrus.Infof("Refreshing")
	cps, err := nd.store.FindKeysNearestTo(KeyPrefixPeer, nd.GetLocalPeer().ID, numPeersNear*10)
	if err != nil {<commit_msg>Add refreshing<commit_after>package dht

import (
	"context"
	"encoding/json"
	"strings"
	"sync"
	"time"

	"github.com/google/uuid"
	"github.com/sirupsen/logrus"

	messagebus "github.com/nimona/go-nimona-messagebus"
	net "github.com/nimona/go-nimona-net"
)

const (
	protocolID = "dht-kad/v1"
)

// DHTNode is the struct that implements the dht protocol
type DHTNode struct {
	localPeer  net.Peer
	store      *Store
	net        net.Network
	messageBus messagebus.MessageBus
	queries    map[string]*query
	mt         sync.RWMutex
}

func NewDHTNode(bps []net.Peer, lp net.Peer, nn net.Network) (*DHTNode, error) {
	// create new routing table
	st, _ := newStore()

	// create messagebud
	mb, err := messagebus.New(protocolID, nn)
	if err != nil {
		return nil, err
	}

	// Create DHT node
	nd := &DHTNode{
		localPeer:  lp,
		store:      st,
		net:        nn,
		messageBus: mb,
		queries:    map[string]*query{},
	}

	// Register message bus, message handler
	if err := mb.HandleMessage(nd.messageHandler); err != nil {
		return nil, err
	}

	// Add bootstrap nodes
	for _, peer := range bps {
		if err := nd.storePeer(peer, true); err != nil {
			logrus.WithField("error", err).Error("new could not store peer")
		}
		if err := nd.putPeer(peer); err != nil {
			logrus.WithField("error", err).Error("new could not put peer")
		}
	}

	// start refresh worker
	ticker := time.NewTicker(15 * time.Second)
	quit := make(chan struct{})
	go func() {
		// refresh for the first time
		nd.refresh()
		// and then just wait
		for {
			select {
			case <-ticker.C:
				nd.refresh()
			case <-quit:
				ticker.Stop()
				return
			}
		}
	}()

	return nd, nil
}

func (nd *DHTNode) refresh() {
	logrus.Infof("Refreshing")
	cps, err := nd.store.FindKeysNearestTo(KeyPrefixPeer, nd.GetLocalPeer().ID, numPeersNear*10)
	if err != nil {
{'fraction_non_alphanumeric': 0.14519650655021835, 'fraction_numerical': 0.0027292576419213972, 'mean_word_length': 2.95361380798274, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7341951', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1367, 'n_words': 344}