Skip to content

Commit

Permalink
Remove unneeded config complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 15, 2024
1 parent c1f2edb commit 99fd575
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 73 deletions.
8 changes: 0 additions & 8 deletions config/actor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"context"
"fmt"
"os"

Expand Down Expand Up @@ -201,10 +200,3 @@ func initActorKeyset() {
}

}

func docPublishOptions() *doc.PublishOptions {
return &doc.PublishOptions{
Ctx: context.Background(),
Force: forceFlag(),
}
}
10 changes: 4 additions & 6 deletions config/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const (
dbMaxConnections = 1 // Required for serialized access to the database
defaultDBFilename = "ma.db"
defaultDbTimeout = 10000
DB_TIMEOUT = 10000
)

var (
Expand All @@ -26,11 +26,9 @@ var (

func init() {

pflag.String("db-file", defaultDbFile, "File to *write* node peers and entities to. If the file does not exist, it will be created.")
pflag.Int("db-timeout", defaultDbTimeout, "Timeout for serialized access to the database in milliseconds.")
pflag.String("db", defaultDbFile, "File for sqlite database.")

viper.BindPFlag("db.file", pflag.Lookup("db-file"))
viper.BindPFlag("db.timeout", pflag.Lookup("db-timeout"))
viper.BindPFlag("db.file", pflag.Lookup("db"))

}

Expand Down Expand Up @@ -93,7 +91,7 @@ func Get() (*sql.DB, error) {
return Init()
}

// Returns expanded path to the db-file file
// Returns expanded path to the dbfile file
// If the expansion fails it returns an empty string
func dbfile() (string, error) {

Expand Down
21 changes: 12 additions & 9 deletions config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ func generateActorConfigFile(identity string, node string) {
"grace-period": P2PConnMgrGracePeriod(),
},
"discovery": map[string]interface{}{
"advertise-ttl": P2PDiscoveryAdvertiseTTL(),
"advertise-limit": P2PDiscoveryAdvertiseLimit(),
"allow-all": DEFAULT_ALLOW_ALL,
"advertise-ttl": P2PDiscoveryAdvertiseTTL(),
"advertise-limit": P2PDiscoveryAdvertiseLimit(),
"advertise-interval": P2PDiscoveryAdvertiseInterval(),
"allow-all": ALLOW_ALL_PEERS,
},
},
}
Expand Down Expand Up @@ -135,9 +136,10 @@ func generatePongConfigFile(identity string, node string) {
"grace-period": P2PConnMgrGracePeriod(),
},
"discovery": map[string]interface{}{
"advertise-ttl": P2PDiscoveryAdvertiseTTL(),
"advertise-limit": P2PDiscoveryAdvertiseLimit(),
"allow-all": DEFAULT_ALLOW_ALL,
"advertise-ttl": P2PDiscoveryAdvertiseTTL(),
"advertise-limit": P2PDiscoveryAdvertiseLimit(),
"advertise-interval": P2PDiscoveryAdvertiseInterval(),
"allow-all": ALLOW_ALL_PEERS,
},
},
"mode": map[string]interface{}{
Expand Down Expand Up @@ -185,9 +187,10 @@ func generateRelayConfigFile(node string) {
"grace-period": P2PConnMgrGracePeriod(),
},
"discovery": map[string]interface{}{
"advertise-ttl": P2PDiscoveryAdvertiseTTL(),
"advertise-limit": P2PDiscoveryAdvertiseLimit(),
"allow-all": DEFAULT_ALLOW_ALL,
"advertise-ttl": P2PDiscoveryAdvertiseTTL(),
"advertise-interval": P2PDiscoveryAdvertiseInterval(),
"advertise-limit": P2PDiscoveryAdvertiseLimit(),
"allow-all": ALLOW_ALL_PEERS,
},
},
}
Expand Down
8 changes: 4 additions & 4 deletions config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var defaultLogfile string = NormalisePath(dataHome + defaultNick + ".log")

func init() {

pflag.String("log-level", defaultLogLevel, "Loglevel to use for application.")
pflag.String("log-file", defaultLogfile, "Logfile to use for application. Accepts 'STDERR' and 'STDOUT' as such.")
pflag.String("loglevel", defaultLogLevel, "Loglevel to use for application.")
pflag.String("logfile", defaultLogfile, "Logfile to use for application. Accepts 'STDERR' and 'STDOUT' as such.")

viper.BindPFlag("log.file", pflag.Lookup("log-file"))
viper.BindPFlag("log.level", pflag.Lookup("log-level"))
viper.BindPFlag("log.file", pflag.Lookup("logfile"))
viper.BindPFlag("log.level", pflag.Lookup("loglevel"))

viper.SetDefault("log.level", defaultLogLevel)

Expand Down
42 changes: 16 additions & 26 deletions config/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const (
defaultDiscoveryAdvertiseInterval time.Duration = time.Minute * 5
defaultDiscoveryAdvertiseTTL time.Duration = time.Minute * 60
defaultDiscoveryAdvertiseLimit int = 100
DEFAULT_ALLOW_ALL bool = true // Allow all peers by default. This is the norm for now. Use connmgr threshold and protection instead.

ALLOW_ALL_PEERS bool = true // Allow all peers by default. This is the norm for now. Use connmgr threshold and protection instead.

defaultListenPort int = 0
fakeP2PIdentity string = "NO_DEFAULT_NODE_IDENITY"
Expand All @@ -27,37 +28,34 @@ func init() {
// P2P Settings

// CONNMGR
pflag.Duration("p2p-connmgr-grace-period", defaultConnmgrGracePeriod, "Grace period for connection manager.")
pflag.Int("p2p-connmgr-high-watermark", defaultConnmgrHighWatermark, "High watermark for peer discovery.")
pflag.Int("p2p-connmgr-low-watermark", defaultConnmgrLowWatermark, "Low watermark for peer discovery.")
pflag.Duration("connmgr-grace-period", defaultConnmgrGracePeriod, "Grace period for connection manager.")
pflag.Int("connmgr-high-watermark", defaultConnmgrHighWatermark, "High watermark for peer discovery.")
pflag.Int("connmgr-low-watermark", defaultConnmgrLowWatermark, "Low watermark for peer discovery.")

viper.BindPFlag("p2p.connmgr.grace-period", pflag.Lookup("p2p-connmgr-grace-period"))
viper.BindPFlag("p2p.connmgr.high-watermark", pflag.Lookup("p2p-connmgr-high-watermark"))
viper.BindPFlag("p2p.connmgr.low-watermark", pflag.Lookup("p2p-connmgr-low-watermark"))
viper.BindPFlag("p2p.connmgr.grace-period", pflag.Lookup("connmgr-grace-period"))
viper.BindPFlag("p2p.connmgr.high-watermark", pflag.Lookup("connmgr-high-watermark"))
viper.BindPFlag("p2p.connmgr.low-watermark", pflag.Lookup("connmgr-low-watermark"))

viper.SetDefault("p2p.connmgr.grace-period", defaultConnmgrGracePeriod)
viper.SetDefault("p2p.connmgr.high-watermark", defaultConnmgrHighWatermark)
viper.SetDefault("p2p.connmgr.low-watermark", defaultConnmgrLowWatermark)

// DISCOVERY
pflag.Int("p2p-discovery-advertise-limit", defaultDiscoveryAdvertiseLimit, "Limit for advertising peer discovery.")
pflag.Duration("p2p-discovery-advertise-ttl", defaultDiscoveryAdvertiseTTL, "Hint of TimeToLive for advertising peer discovery.")
pflag.Duration("p2p-discovery-advertise-interval", defaultDiscoveryAdvertiseInterval, "How often to advertise our presence to libp2p")
pflag.Bool("p2p-discovery-allow-all", DEFAULT_ALLOW_ALL, "Number of concurrent peer discovery routines.")
pflag.Int("discovery-advertise-limit", defaultDiscoveryAdvertiseLimit, "Limit for advertising peer discovery.")
pflag.Duration("discovery-advertise-ttl", defaultDiscoveryAdvertiseTTL, "Hint of TimeToLive for advertising peer discovery.")
pflag.Duration("discovery-advertise-interval", defaultDiscoveryAdvertiseInterval, "How often to advertise our presence to libp2p")

viper.BindPFlag("p2p.discovery.advetise-interval", pflag.Lookup("p2p-discovery-advertise-interval"))
viper.BindPFlag("p2p.discovery.advertise-limit", pflag.Lookup("p2p-discovery-advertise-limit"))
viper.BindPFlag("p2p.discovery.advertise-ttl", pflag.Lookup("p2p-discovery-advertise-ttl"))
viper.BindPFlag("p2p.discovery.allow-all", pflag.Lookup("p2p-discovery-allow-all"))
viper.BindPFlag("p2p.discovery.advertise-interval", pflag.Lookup("discovery-advertise-interval"))
viper.BindPFlag("p2p.discovery.advertise-limit", pflag.Lookup("discovery-advertise-limit"))
viper.BindPFlag("p2p.discovery.advertise-ttl", pflag.Lookup("discovery-advertise-ttl"))

viper.SetDefault("p2p.discovery.advertise-interval", defaultDiscoveryAdvertiseInterval)
viper.SetDefault("p2p.discovery.advertise-limit", defaultDiscoveryAdvertiseLimit)
viper.SetDefault("p2p.discovery.advertise-ttl", defaultDiscoveryAdvertiseTTL)
viper.SetDefault("p2p.discovery.allow-all", DEFAULT_ALLOW_ALL)

// Port
pflag.Int("p2p-port", defaultListenPort, "Port for libp2p node to listen on.")
viper.BindPFlag("p2p.port", pflag.Lookup("p2p-port"))
pflag.Int("port", defaultListenPort, "Port for libp2p node to listen on.")
viper.BindPFlag("p2p.port", pflag.Lookup("port"))
viper.SetDefault("p2p.port", defaultListenPort)

// Identity
Expand All @@ -84,10 +82,6 @@ func P2PDiscoveryAdvertiseLimit() int {
return viper.GetInt("p2p.discovery.advertise-limit")
}

func P2PDiscoveryAllowAll() bool {
return viper.GetBool("p2p.discovery.allow-all")
}

func P2PConnmgrLowWatermark() int {
return viper.GetInt("p2p.connmgr.low-watermark")
}
Expand All @@ -104,10 +98,6 @@ func P2PPort() int {
return viper.GetInt("p2p.port")
}

func P2PDiscoveryRetryInterval() time.Duration {
return viper.GetDuration("p2p.discovery.retry")
}

// String functions

func P2PPortString() string {
Expand Down
5 changes: 0 additions & 5 deletions config/ui.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package config

import (
"time"

"github.com/spf13/viper"
)

func UIPeerslistWidth() int {
return viper.GetInt("ui.peerslist-width")
}
func UIPeersRefreshInterval() time.Duration {
return viper.GetDuration("ui.refresh")
}
4 changes: 2 additions & 2 deletions p2p/connmgr/gater.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ConnectionGater struct {
// New creates a new CustomConnectionGater instance.
func NewConnectionGater(connMgr *p2pConnmgr.BasicConnMgr) *ConnectionGater {
return &ConnectionGater{
AllowAll: config.P2PDiscoveryAllowAll(), // Here we use a lookup, not the constant
AllowAll: config.ALLOW_ALL_PEERS, // Here we use a lookup, not the constant
ConnMgr: connMgr,
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (cg *ConnectionGater) InterceptUpgraded(_ network.Conn) (allow bool, reason

func (cg *ConnectionGater) isAllowed(p p2peer.ID) bool {

if config.P2PDiscoveryAllowAll() || cg.AllowAll { // This is an appriprotiate place to use the function
if cg.AllowAll {
return true
}

Expand Down
3 changes: 1 addition & 2 deletions ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma-actor/config"
log "github.com/sirupsen/logrus"
)

Expand All @@ -31,7 +30,7 @@ func (ui *ChatUI) displayDebugMessage(m string) {
// refreshes the list of peers in the UI.

func (ui *ChatUI) handleEvents() {
peerRefreshTicker := time.NewTicker(config.UIPeersRefreshInterval())
peerRefreshTicker := time.NewTicker(time.Second)
defer peerRefreshTicker.Stop()

for {
Expand Down
17 changes: 6 additions & 11 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"time"

"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
Expand All @@ -22,24 +21,20 @@ const (

defaultLimbo = "closet"

defaultPeerslistWidth = 10
defaultPeerslistRefresh = time.Second * 3
defaultHistorySize = 50
defaultPeerslistWidth = 10
defaultHistorySize = 50

separator = " "
)

func init() {

// UI
pflag.Int("ui-peerslist-width", defaultPeerslistWidth, "Sets the width of the peerslist pane in the UI")
viper.BindPFlag("ui.peerslist-width", pflag.Lookup("ui-peerslist-width"))
pflag.Int("peerslist-width", defaultPeerslistWidth, "Sets the width of the peerslist pane in the UI")
viper.BindPFlag("ui.peerslist-width", pflag.Lookup("peerslist-width"))

pflag.Duration("ui-refresh", defaultPeerslistRefresh, "Sets the update frequency of the peerslist pane in the UI")
viper.BindPFlag("ui.refresh", pflag.Lookup("ui-refresh"))

pflag.Int("ui-history-size", defaultHistorySize, "Sets the update frequency of the peerslist pane in the UI")
viper.BindPFlag("ui.history-size", pflag.Lookup("ui-history-size"))
pflag.Int("history-size", defaultHistorySize, "Sets the size of the history buffer in the UI.")
viper.BindPFlag("ui.history-size", pflag.Lookup("history-size"))

}

Expand Down

0 comments on commit 99fd575

Please sign in to comment.