Skip to content

Commit

Permalink
Add p2p options to Init
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Nov 26, 2023
1 parent ea204cb commit 31e1b68
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
29 changes: 13 additions & 16 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import (
"os"
"time"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma/key/set"
log "github.com/sirupsen/logrus"
"go.deanishe.net/env"
)

const (
name = "go-ma-actor"
keyset_var = "GO_MA_ACTOR_KEYSET"
entity_var = "GO_MA_ACTOR_ENTITY"
discovery_timeout_var = "GO_MA_ACTOR_DISCOVERY_TIMEOUT"
low_watermark_var = "GO_MA_ACTOR_LOW_WATERMARK"
high_watermark_var = "GO_MA_ACTOR_HIGH_WATERMARK"
connmgr_grace_var = "GO_MA_ACTOR_CONNMGR_GRACE"
log_level_var = "GO_MA_ACTOR_LOG_LEVEL"
name = "go-ma-actor"

// The default entity to connect to.
GO_MA_ACTOR_ENTITY_VAR = "GO_MA_ACTOR_ENTITY"

defaultDiscoveryTimeout int = 300
defaultLowWaterMark int = 2
Expand All @@ -38,19 +35,19 @@ var (
)

var (
discoveryTimeout int = env.GetInt(discovery_timeout_var, defaultDiscoveryTimeout)
lowWaterMark int = env.GetInt(low_watermark_var, defaultLowWaterMark)
highWaterMark int = env.GetInt(high_watermark_var, defaultHighWaterMark)
connmgrGracePeriod time.Duration = env.GetDuration(connmgr_grace_var, defaultConnMgrGrace)
discoveryTimeout int = env.GetInt(ma.DISCOVERY_TIMEOUT_VAR, defaultDiscoveryTimeout)
lowWaterMark int = env.GetInt(ma.LOW_WATERMARK_VAR, defaultLowWaterMark)
highWaterMark int = env.GetInt(ma.HIGH_WATERMARK_VAR, defaultHighWaterMark)
connmgrGracePeriod time.Duration = env.GetDuration(ma.CONNMGR_GRACE_VAR, defaultConnMgrGrace)

logLevel string = env.Get(log_level_var, "info")
logfile string = env.Get("GO_MA_LOG_FILE", name+"log")
logLevel string = env.Get(ma.LOGLEVEL_VAR, "info")
logfile string = env.Get(ma.LOGFILE_VAR, name+".log")

// What we want to communicate with initially
entity string = env.Get(entity_var, "")
entity string = env.Get(GO_MA_ACTOR_ENTITY_VAR, "")

// Actor
keyset_string string = env.Get(keyset_var, "")
keyset_string string = env.Get(ma.KEYSET_VAR, "")

// Nick is only used for keyset generation. Must be a valid NanoID.
nick string = env.Get("USER")
Expand Down
3 changes: 2 additions & 1 deletion config/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/bahner/go-ma"
"github.com/bahner/go-ma/did/doc"
"github.com/bahner/go-ma/key/set"
"github.com/pkg/errors"
Expand Down Expand Up @@ -65,7 +66,7 @@ func generateKeyset() string {
}

if *genenv {
fmt.Println("export " + keyset_var + "=" + pks)
fmt.Println("export " + ma.KEYSET_VAR + "=" + pks)
}

return pks
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bahner/go-ma-actor
go 1.21

require (
github.com/bahner/go-ma v0.0.5
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5
github.com/c-bata/go-prompt v0.2.6
github.com/gdamore/tcell/v2 v2.6.0
github.com/libp2p/go-libp2p v0.32.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/bahner/go-ma v0.0.5 h1:RgQrINqn6zU4avHDwoYfPCtMnpU9OpTkOUaFcTsS0MM=
github.com/bahner/go-ma v0.0.5/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/bahner/go-ma v0.0.6-0.20231125235358-e5f8a113aef4 h1:/YfOUHvRcDYO3nAYOXzwwosZzSmOgbEQVhMM6m2N2Ew=
github.com/bahner/go-ma v0.0.6-0.20231125235358-e5f8a113aef4/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5 h1:Olf3MlIvYYfgdDQnofqcKSQOdO7hcGleD68ofrptxsA=
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
Expand Down
16 changes: 11 additions & 5 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,24 @@ var (
// discoveryTimeout is the timeout duration for peer discovery.
// It's a time.Duration type.
//
// Also takes a variadic list of libp2p options.
// Of it's nil, an empty list will be used.
//
// The function return the libp2p node and a PubSub Service

func Init(ctx context.Context, i *ipns.Key, discoveryTimeout time.Duration) (host.Host, *p2ppubsub.PubSub, error) {
func Init(ctx context.Context, i *ipns.Key, discoveryTimeout time.Duration, p2pOpts ...libp2p.Option) (host.Host, *p2ppubsub.PubSub, error) {

// Initiate libp2p options, if none are provided
if p2pOpts == nil {
p2pOpts = []libp2p.Option{}
}

// Add the connection manager to the options
connMgr, err := connmgr.Init()
if err != nil {
return nil, nil, fmt.Errorf("p2p.Init: failed to create connection manager: %w", err)
}

p2pOpts := []libp2p.Option{
libp2p.ConnectionManager(connMgr),
}
p2pOpts = append(p2pOpts, libp2p.ConnectionManager(connMgr))

// Create a new libp2p Host that listens on a random TCP port
n, err = node.New(i, p2pOpts...)
Expand Down

0 comments on commit 31e1b68

Please sign in to comment.