func (hdb *HostDB) threadedSaveLoop() {
	for {
		select {
		case <-hdb.tg.StopChan():
			return
		case <-time.After(saveFrequency):
			hdb.mu.Lock()
			err := hdb.saveSync()
			hdb.mu.Unlock()
			if err != nil {
				hdb.log.Println("Difficulties saving the hostdb:", err)
			}
		}
	}
}
threadedSaveLoop saves the hostdb to disk every 2 minutes, also saving when
given the shutdown signal.
package hostdb
import (
	"path/filepath"
	"time"
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/persist"
	"github.com/NebulousLabs/Sia/types"
)
var (
	persistFilename = "hostdb.json"
	persistMetadata = persist.Metadata{
		Header:  "HostDB Persistence",
		Version: "0.5",
	}
)
type hdbPersist struct {
	AllHosts    []modules.HostDBEntry
	BlockHeight types.BlockHeight
	LastChange  modules.ConsensusChangeID
}
func (hdb *HostDB) persistData() (data hdbPersist) {
	data.AllHosts = hdb.hostTree.All()
	data.BlockHeight = hdb.blockHeight
	data.LastChange = hdb.lastChange
	return data
}
func (hdb *HostDB) saveSync() error {
	return hdb.deps.SaveFileSync(persistMetadata, hdb.persistData(), filepath.Join(hdb.persistDir, persistFilename))
}
func (hdb *HostDB) load() error {
	var data hdbPersist
	err := hdb.deps.LoadFile(persistMetadata, &data, filepath.Join(hdb.persistDir, persistFilename))
	if err != nil {
		return err
	}
	hdb.blockHeight = data.BlockHeight
	hdb.lastChange = data.LastChange
	for _, host := range data.AllHosts {
		if hdb.blockHeight < host.FirstSeen {
			host.FirstSeen = hdb.blockHeight
		}
		err := hdb.hostTree.Insert(host)
		if err != nil {
			hdb.log.Debugln("ERROR: could not insert host while loading:", host.NetAddress)
		}
		if len(host.ScanHistory) < 2 {
			hdb.queueScan(host)
		}
	}
	return nil
}