Răsfoiți Sursa

:gear: Add a context to Join()

Also do not wait in a goroutine when context is canceled
Ettore Di Giacinto 3 ani în urmă
părinte
comite
8ec11e05f2
5 a modificat fișierele cu 16 adăugiri și 11 ștergeri
  1. 2 1
      cmd/api.go
  2. 3 2
      cmd/file.go
  3. 3 1
      cmd/join.go
  4. 3 2
      cmd/service.go
  5. 5 5
      pkg/edgevpn/edgevpn.go

+ 2 - 1
cmd/api.go

@@ -16,6 +16,7 @@
 package cmd
 
 import (
+	"context"
 	"time"
 
 	"github.com/mudler/edgevpn/api"
@@ -43,7 +44,7 @@ A simple UI interface is available to display network data.`,
 			displayStart(e)
 
 			// Join the node to the network, using our ledger
-			if err := e.Join(); err != nil {
+			if err := e.Join(context.Background()); err != nil {
 				return err
 			}
 			ledger, _ := e.Ledger()

+ 3 - 2
cmd/file.go

@@ -16,6 +16,7 @@
 package cmd
 
 import (
+	"context"
 	"errors"
 
 	"github.com/mudler/edgevpn/pkg/edgevpn"
@@ -78,7 +79,7 @@ This is also the ID used to refer when receiving it.`,
 			// Join the node to the network, using our ledger
 			e.SendFile(ledger, name, path)
 			// Join the node to the network, using our ledger
-			if err := e.Join(); err != nil {
+			if err := e.Join(context.Background()); err != nil {
 				return err
 			}
 
@@ -116,7 +117,7 @@ func FileReceive() cli.Command {
 			displayStart(e)
 
 			// Join the node to the network, using our ledger
-			if err := e.Join(); err != nil {
+			if err := e.Join(context.Background()); err != nil {
 				return err
 			}
 

+ 3 - 1
cmd/join.go

@@ -16,6 +16,8 @@
 package cmd
 
 import (
+	"context"
+
 	"github.com/mudler/edgevpn/pkg/edgevpn"
 	"github.com/urfave/cli"
 )
@@ -34,7 +36,7 @@ Useful for setting up relays or hop nodes to improve the network connectivity.`,
 			displayStart(e)
 
 			// Join the node to the network, using our ledger
-			if err := e.Join(); err != nil {
+			if err := e.Join(context.Background()); err != nil {
 				return err
 			}
 

+ 3 - 2
cmd/service.go

@@ -16,6 +16,7 @@
 package cmd
 
 import (
+	"context"
 	"errors"
 
 	"github.com/mudler/edgevpn/pkg/edgevpn"
@@ -78,7 +79,7 @@ For example, '192.168.1.1:80', or '127.0.0.1:22'.`,
 			// Join the node to the network, using our ledger
 			e.ExposeService(ledger, name, address)
 			// Join the node to the network, using our ledger
-			if err := e.Join(); err != nil {
+			if err := e.Join(context.Background()); err != nil {
 				return err
 			}
 
@@ -118,7 +119,7 @@ to the service over the network`,
 			displayStart(e)
 
 			// Join the node to the network, using our ledger
-			if err := e.Join(); err != nil {
+			if err := e.Join(context.Background()); err != nil {
 				return err
 			}
 

+ 5 - 5
pkg/edgevpn/edgevpn.go

@@ -98,7 +98,7 @@ func (e *EdgeVPN) Ledger() (*blockchain.Ledger, error) {
 // Join the network with the ledger.
 // It does the minimal action required to be connected
 // without any active packet routing
-func (e *EdgeVPN) Join() error {
+func (e *EdgeVPN) Join(ctx context.Context) error {
 
 	ledger, err := e.Ledger()
 	if err != nil {
@@ -118,11 +118,11 @@ func (e *EdgeVPN) Join() error {
 	}
 
 	// Send periodically messages to the channel with our blockchain content
-	ledger.Syncronizer(context.Background(), e.config.LedgerSyncronizationTime)
+	ledger.Syncronizer(ctx, e.config.LedgerSyncronizationTime)
 
 	// Start eventual declared NetworkServices
 	for _, s := range e.config.NetworkServices {
-		go s(context.Background(), e, ledger)
+		go s(ctx, e, ledger)
 	}
 
 	return nil
@@ -160,7 +160,7 @@ func (e *EdgeVPN) Start(ctx context.Context) error {
 	// Join the node to the network, using our ledger
 	// it also starts up a goroutine that periodically sends
 	// messages to the network with our blockchain content
-	if err := e.Join(); err != nil {
+	if err := e.Join(ctx); err != nil {
 		return err
 	}
 
@@ -311,7 +311,7 @@ func (e *EdgeVPN) readPackets(ctx context.Context, ledger *blockchain.Ledger, if
 
 	defer func() {
 		close(packets)
-		go wg.Wait()
+		wg.Wait()
 	}()
 
 	for i := 0; i < e.config.Concurrency; i++ {