Added close node support

This commit is contained in:
Alfonso de la Rocha 2020-08-26 16:45:42 +02:00
parent a9ff54fb16
commit 6cb478cf97
2 changed files with 13 additions and 7 deletions

View File

@ -18,7 +18,7 @@ In order to complete this tutorial, you will need:
**Disclaimer**: This example was an attempt to understand how to manually inject dependencies to spawn an IPFS node with custom configurations
without having to rely on `repo.Repo` and `BuildCfg`.
For this specific example the focus was on being able to initialize our own `exchange.Interface` and set IPFS nodes on different ports. Expect in
the future further improvements to this example in order to clean the code and include additional custom configurations. The example is inspired on the [following function](../../../core/builder.go#L27)
the future further improvements to this example in order to clean the code and include additional custom configurations. The example is inspired by the [following function](../../../core/builder.go#L27)
## Getting started
@ -56,6 +56,7 @@ The example comprises the following parts:
* A [main function](./main.go#L309-L362) where all the action happens. Here we define the [size of the random file](./main.go#L312) to be generated, we [spawn two IPFS nodes](./main.go#L317-L335), we [connect both nodes](./main.go#L337-L342), we [generate a random file and added it to the network from node 1](./main.go#L344-L351) and finally [retrieve it from node2](./main.go#L353-L363).
* The nodes are spawned using the same [`NewNode` function](./main.go#L248-L307) which initializes the node and injects all the corresponding dependencies. In this example both nodes are using the same configuration.
* Nodes return their [`close` function](./main.go#L319) in case they want to [be gracefully closed](./main.go#L362-L367), as this way of spawning nodes generate a nil pointer reference error with `node.Close()` as it can't be properly initialized.
* The configuration and dependencies of the node are set in the [`setConfig` function](./main.go#L68-L249). This is the place to go if you want to change some configurations of the nodes to be spawned. In this function you will be able to do some cool stuff such as:
* Setting the listening address for the nodes, or [allow them to listen from any available port](./main.go#L92-L98).

View File

@ -245,7 +245,7 @@ func setConfig(ctx context.Context) fx.Option {
}
// NewNode constructs and returns an IpfsNode using the given cfg.
func NewNode(ctx context.Context) (*core.IpfsNode, error) {
func NewNode(ctx context.Context) (*core.IpfsNode, func() error, error) {
// save this context as the "lifetime" ctx.
lctx := ctx
@ -296,14 +296,14 @@ func NewNode(ctx context.Context) (*core.IpfsNode, error) {
}()
if app.Err() != nil {
return nil, app.Err()
return nil, nil, app.Err()
}
if err := app.Start(ctx); err != nil {
return nil, err
return nil, nil, err
}
return n, n.Bootstrap(bootstrap.DefaultBootstrapConfig)
return n, stopNode, n.Bootstrap(bootstrap.DefaultBootstrapConfig)
}
func main() {
@ -316,12 +316,12 @@ func main() {
// Spawn two different nodes with the same configuration.
// Each will be started in a different port.
n1, err := NewNode(ctx)
n1, n1Close, err := NewNode(ctx)
fmt.Println("[*] Spawned first node listening at: ", n1.PeerHost.Addrs())
if err != nil {
panic(err)
}
n2, err := NewNode(ctx)
n2, n2Close, err := NewNode(ctx)
fmt.Println("[*] Spawned first node listening at: ", n2.PeerHost.Addrs())
if err != nil {
panic(err)
@ -359,4 +359,9 @@ func main() {
// Size of the file.
s, _ := f.Size()
fmt.Println("[*] Retrieved file with size: ", s)
// Close both nodes.
n1Close()
fmt.Println("[*] Gracefully closed node 1")
n2Close()
fmt.Println("[*] Gracefully closed node 2")
}