aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2018-01-03 15:01:53 +0000
committerFelix Hanley <felix@userspace.com.au>2018-01-03 15:01:53 +0000
commitf1fcf73f15edefe25863f8dbf550a9fbfa8f03b6 (patch)
treed6a3b6b1c44f2740c8ff284e7b89871412d4b9a4
parented86d0f35046dc5e360d554d3a80a66d70b1a27f (diff)
downloaddhtsearch-f1fcf73f15edefe25863f8dbf550a9fbfa8f03b6.tar.gz
dhtsearch-f1fcf73f15edefe25863f8dbf550a9fbfa8f03b6.tar.bz2
Update config WIP
-rw-r--r--config.go141
-rw-r--r--config.toml.sample14
-rw-r--r--[-rwxr-xr-x]main.go39
-rw-r--r--stats.go32
4 files changed, 94 insertions, 132 deletions
diff --git a/config.go b/config.go
index 099ce7f..4ef538e 100644
--- a/config.go
+++ b/config.go
@@ -1,109 +1,42 @@
-package main
-
-import (
- "flag"
- "fmt"
- "github.com/BurntSushi/toml"
- "os"
- "path/filepath"
-)
-
-type config struct {
- BasePort int `toml:"base-port"`
- Debug bool
- Quiet bool
- NoHttp bool `toml:"no-http"`
- HttpAddress string `toml:"http-address"`
- Dsn string
- NumNodes int `toml:"num-nodes"`
- Tags map[string]string
- SkipTags []string `toml:"skip-tags"`
- Advanced advancedConfig
-}
-
-type advancedConfig struct {
- RoutingTableSize int `toml:"routing-table-size"`
- MaxBtWorkers int `toml:"max-bt-workers"`
- MaxDhtWorkers int `toml:"max-dht-workers"`
- PeerCacheSize int `toml:"peer-cache-size"`
- UdpTimeout int `toml:"udp-timeout"`
- TcpTimeout int `toml:"tcp-timeout"`
- ResultsPageSize int `toml:"results-page-size"`
-}
-
-// Global
-var Config config
-
-func loadConfig() {
- // Defaults
- Config = config{
- BasePort: 6881,
- NumNodes: 1,
- Debug: false,
- Quiet: false,
- Dsn: "postgres://dht:dht@localhost/dht?sslmode=disable",
- NoHttp: false,
- HttpAddress: "localhost:6880",
- Advanced: advancedConfig{
- RoutingTableSize: 4000,
- MaxBtWorkers: 256,
- MaxDhtWorkers: 256,
- PeerCacheSize: 200,
- UdpTimeout: 10,
- TcpTimeout: 10,
- ResultsPageSize: 50,
- },
- }
- /*
- ex, err := os.Executable()
- if err != nil {
- fmt.Printf("Failed to get executable: %q\n", err)
- os.Exit(1)
- }
- exPath := path.Dir(ex)
- */
-
- flag.IntVar(&Config.BasePort, "base-port", Config.BasePort, "listen port (and first of multiple ports)")
- flag.IntVar(&Config.NumNodes, "num-nodes", Config.NumNodes, "number of nodes to start")
- flag.BoolVar(&Config.Debug, "debug", Config.Debug, "provide debug output")
- flag.BoolVar(&Config.Quiet, "quiet", Config.Quiet, "log only errors")
- flag.StringVar(&Config.Dsn, "dsn", Config.Dsn, "Database DSN")
- flag.BoolVar(&Config.NoHttp, "no-http", Config.NoHttp, "no HTTP service")
- flag.StringVar(&Config.HttpAddress, "http-address", Config.HttpAddress, "HTTP listen address:port")
+package dhtsearch
+
+// Config -
+type Config struct {
+ BasePort int `toml:"base_port"`
+ Debug bool `toml:"debug"`
+ NoHTTP bool `toml:"no_http"`
+ HTTPAddress string `toml:"http_address"`
+ DSN string `toml:"dsn"`
+ NumNodes int `toml:"num_nodes"`
+ Tags map[string]string `toml:"tags"`
+ SkipTags []string `toml:"skip_tags"`
// Advanced
- flag.IntVar(&Config.Advanced.RoutingTableSize, "routing-table-size", Config.Advanced.RoutingTableSize, "number of remote nodes in routing table")
- flag.IntVar(&Config.Advanced.MaxBtWorkers, "max-bt-workers", Config.Advanced.MaxBtWorkers, "max number of BT workers")
- flag.IntVar(&Config.Advanced.MaxDhtWorkers, "max-dht-workers", Config.Advanced.MaxDhtWorkers, "max number of DHT workers")
- flag.IntVar(&Config.Advanced.PeerCacheSize, "peer-cache-size", Config.Advanced.PeerCacheSize, "memory cache of seen peers")
- flag.IntVar(&Config.Advanced.UdpTimeout, "udp-timeout", Config.Advanced.UdpTimeout, "UDP timeout in seconds")
- flag.IntVar(&Config.Advanced.TcpTimeout, "tcp-timeout", Config.Advanced.TcpTimeout, "TCP timeout in seconds")
- flag.IntVar(&Config.Advanced.ResultsPageSize, "results-page-size", Config.Advanced.ResultsPageSize, "number of items per page")
-
- cfgPath, _ := filepath.Abs("./config.toml")
- if _, err := os.Stat(cfgPath); !os.IsNotExist(err) {
- // fmt.Printf("Using configuration from %s\n", cfgPath)
- md, err := toml.DecodeFile(cfgPath, &Config)
- if err != nil {
- fmt.Printf("Failed to read configuration: %q\n", err)
- os.Exit(1)
- }
- if len(md.Undecoded()) > 0 {
- fmt.Printf("Extraneous configuration keys: %q\n", md.Undecoded())
- }
- }
-
- // Leave it to the user to get around this
- if len(Config.SkipTags) == 0 {
- fmt.Println("Using default skip tags")
- Config.SkipTags = []string{"adult"}
- }
-
- flag.Parse()
-
- initTagRegexps()
+ RoutingTableSize int `toml:"routing_table_size"`
+ MaxBTWorkers int `toml:"max_bt_workers"`
+ MaxDHTWorkers int `toml:"max_dht_workers"`
+ PeerCacheSize int `toml:"peer_cache_size"`
+ UDPTimeout int `toml:"udp_timeout"`
+ TCPTimeout int `toml:"tcp_timeout"`
+ ResultsPageSize int `toml:"results_page_size"`
+}
- if !Config.Quiet {
- fmt.Printf("Skipping tags: %q\n", Config.SkipTags)
+// DefaultConfig sets the defaults
+func DefaultConfig() Config {
+ return Config{
+ BasePort: 6881,
+ NumNodes: 1,
+ Debug: false,
+ Quiet: false,
+ DSN: "postgres://dht:dht@localhost/dht?sslmode=disable",
+ NoHTTP: false,
+ HTTPAddress: "localhost:6880",
+ RoutingTableSize: 4000,
+ MaxBTWorkers: 10,
+ MaxDHTWorkers: 256,
+ PeerCacheSize: 200,
+ UDPTimeout: 10,
+ TCPTimeout: 10,
+ ResultsPageSize: 50,
}
}
diff --git a/config.toml.sample b/config.toml.sample
new file mode 100644
index 0000000..dedd166
--- /dev/null
+++ b/config.toml.sample
@@ -0,0 +1,14 @@
+# Move to config.toml in the same directory as the binary.
+# All command line arguments are accepted plus some more.
+
+base_port = 6881
+num_nodes = 1
+debug = false
+quiet = false
+dsn = "postgres://dht:dht@localhost/dht?sslmode=disable"
+no_http = false
+http_address = "localhost:6880"
+skip_tags = ["adult"]
+
+[tags]
+test = "\\.asdfasdfsdfsfdsfs"
diff --git a/main.go b/main.go
index 6a7de7c..653df1f 100755..100644
--- a/main.go
+++ b/main.go
@@ -1,34 +1,13 @@
-package main
+package dhtsearch
import (
- "expvar"
"fmt"
- "time"
- //"github.com/pkg/profile"
"net"
"net/http"
"os"
-)
+ "time"
-// Exported vars
-var (
- dhtPacketsIn = expvar.NewInt("dht_packets_in")
- dhtPacketsOut = expvar.NewInt("dht_packets_out")
- dhtPacketsDropped = expvar.NewInt("dht_packets_dropped")
- dhtErrorPackets = expvar.NewInt("dht_error_packets")
- dhtCachedPeers = expvar.NewInt("dht_cached_peers")
- dhtBytesIn = expvar.NewInt("dht_bytes_in")
- dhtBytesOut = expvar.NewInt("dht_bytes_out")
- dhtWorkers = expvar.NewInt("dht_workers")
- btBytesIn = expvar.NewInt("bt_bytes_in")
- btBytesOut = expvar.NewInt("bt_bytes_out")
- btWorkers = expvar.NewInt("bt_workers")
- peersAnnounced = expvar.NewInt("peers_announced")
- peersSkipped = expvar.NewInt("peers_skipped")
- torrentsSkipped = expvar.NewInt("torrents_skipped")
- torrentsSaved = expvar.NewInt("torrents_saved")
- torrentsTotal = expvar.NewInt("torrents_total")
- start = time.Now()
+ "github.com/felix/logger"
)
func uptime() interface{} {
@@ -39,7 +18,10 @@ func main() {
//defer profile.Start(profile.CPUProfile).Stop()
expvar.Publish("uptime", expvar.Func(uptime))
- loadConfig()
+ log := logger.New(&logger.Options{
+ Name: "dht",
+ Level: logger.Info,
+ })
// Slice of channels for DHT node output
torrents := make(chan Torrent)
@@ -58,7 +40,7 @@ func main() {
defer DB.Close()
// Initialise tags
- for tag, _ := range tags {
+ for tag := range tags {
_, err := createTag(tag)
if err != nil {
fmt.Printf("Error creating tag %s: %q\n", tag, err)
@@ -83,8 +65,9 @@ func main() {
filteredPeers := make(chan peer)
// Create BT node
- btClient := newBTClient(filteredPeers, torrents)
- err = btClient.run(done)
+ bt := &btClient{}
+ bt.log = log.Named("bt")
+ err = btClient.run(torrents)
if err != nil {
os.Exit(1)
}
diff --git a/stats.go b/stats.go
new file mode 100644
index 0000000..0ff5e6f
--- /dev/null
+++ b/stats.go
@@ -0,0 +1,32 @@
+package dhtsearch
+
+type Stats struct {
+ DHTPacketsIn int `json:"dht_packets_in"`
+ DHTPacketsOut int `json:"dht_packets_out"`
+ DHTPacketsDropped int `json:"dht_packets_dropped"`
+ DHTErrors int `json:"dht_errors"`
+ DHTCachedPeers int `json:"dht_cached_peers"`
+ DHTBytesIn int `json:"dht_bytes_in"`
+ DHTBytesOut int `json:"dht_bytes_out"`
+ DHTWorkers int `json:"dht_workers"`
+ BTBytesInt int `json:"bt_bytes_int"`
+ BTBytesOut int `json:"bt_bytes_out"`
+ BTWorkers int `json:"bt_workers"`
+ PeersAnnounced int `json:"peers_announced"`
+ PeersSkipped int `json:"peers_skipped"`
+ TorrentsSkipped int `json:"torrents_skipped"`
+ TorrentsSaved int `json:"torrents_saved"`
+ TorrentsTotal int `json:"torrents_total"`
+}
+
+func (s *Stats) Sub(other *Stats) Stats {
+ if other == nil {
+ return *s
+ }
+ var diff Stats
+ diff.MessagesIn = s.MessagesIn - other.MessagesIn
+ diff.BytesIn = s.BytesIn - other.BytesIn
+ diff.MessagesOut = s.MessagesOut - other.MessagesOut
+ diff.BytesOut = s.BytesOut - other.BytesOut
+ return diff
+}