From 5cd4cd6485cd66dfdaa5ecc5f49de55b3d40ee6a Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 10 Apr 2020 01:33:02 +0530 Subject: [PATCH 1/2] Handle context cancellation for temp tunnel Signed-off-by: Vivek Singh --- cmd/create.go | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 18407b60..647b0d92 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -260,22 +260,46 @@ func runCreate(cmd *cobra.Command, _ []string) error { if delTunnel == true { sig := make(chan os.Signal) done := make(chan bool) + deleted := make(chan bool) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) + fmt.Printf("Your IP is: %s\n", hostStatus.IP) + + var err error = nil + ctx, cancelFunction := context.WithCancel(context.Background()) + go func() { sigval, ok := <-sig fmt.Printf("\n%v\n", sigval) + cancelFunction() done <- true if ok { close(sig) } }() - fmt.Printf("Your IP is: %s\n", hostStatus.IP) + go func() { + _, ok := <-done + if ok { + close(done) + } + + hostDelReq := provision.HostDeleteRequest{ + ID: hostStatus.ID, + IP: hostStatus.IP, + ProjectID: projectID, + Zone: zone, + } + fmt.Printf("Deleting host: %s with IP: %s from %s\n", hostStatus.ID, hostStatus.IP, provider) + err = provisioner.Delete(hostDelReq) + if err != nil { + fmt.Printf("error deleting the exitnode: %v\n", err) + } + + deleted <- true + }() - var err error = nil - ctx, cancelFunction := context.WithCancel(context.Background()) if pro { timeout := 30 retries := 90 @@ -294,27 +318,12 @@ func runCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("Error running inlets: %v", err) } - _, ok := <-done - if ok { - close(done) - } - cancelFunction() + <-deleted + close(deleted) - hostDelReq := provision.HostDeleteRequest{ - ID: hostStatus.ID, - IP: hostStatus.IP, - ProjectID: projectID, - Zone: zone, - } - fmt.Printf("Deleting host: %s with IP: %s from %s\n", hostStatus.ID, hostStatus.IP, provider) - err = provisioner.Delete(hostDelReq) - if err != nil { - return fmt.Errorf("error deleting the exitnode: %v", err) - } fmt.Println("exiting") return nil } - printExample(pro, hostStatus, inletsToken, provider, inletsControlPort) return nil } @@ -582,7 +591,7 @@ func checkServiceUp(ctx context.Context, url string, timeout int, retries int) ( for i := 0; i < retries; i++ { select { - case <-req.Context().Done(): + case <-ctx.Done(): return false, fmt.Errorf("%s", "interrupt") default: res, err := httpClient.Do(req) From 764f5e0af7fae1dc203a6ec0e05c15d32a2e4fc2 Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Fri, 10 Apr 2020 11:55:08 +0530 Subject: [PATCH 2/2] Remove host on error from inlets client exec THis commit handles deletion of the host when there fromr nlets client exec. It also removes reduntant goroutine and channel. Signed-off-by: Vivek Singh --- cmd/create.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 647b0d92..22dc193e 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -260,7 +260,6 @@ func runCreate(cmd *cobra.Command, _ []string) error { if delTunnel == true { sig := make(chan os.Signal) done := make(chan bool) - deleted := make(chan bool) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) @@ -273,17 +272,9 @@ func runCreate(cmd *cobra.Command, _ []string) error { sigval, ok := <-sig fmt.Printf("\n%v\n", sigval) cancelFunction() - done <- true if ok { close(sig) } - }() - - go func() { - _, ok := <-done - if ok { - close(done) - } hostDelReq := provision.HostDeleteRequest{ ID: hostStatus.ID, @@ -297,7 +288,7 @@ func runCreate(cmd *cobra.Command, _ []string) error { fmt.Printf("error deleting the exitnode: %v\n", err) } - deleted <- true + done <- true }() if pro { @@ -306,20 +297,32 @@ func runCreate(cmd *cobra.Command, _ []string) error { url := fmt.Sprintf("https://%s:%d/.well-known/ca.crt", hostStatus.IP, inletsControlPort) up, err := checkServiceUp(ctx, url, timeout, retries) if err != nil { - return fmt.Errorf("%v", err) + fmt.Printf("%v\n", err) } if up { err = runInletsClient(pro, hostStatus.IP, inletsControlPort, remoteTCP, inletsToken, tcpPorts, inletsProLicenseKey) + // Delete the host after getting an error while running inlets client by + // sending SIGINT to the sig channel + if err != nil { + fmt.Printf("Error running inlets: %v\n", err) + fmt.Println("Sending SIGINT.") + sig <- syscall.SIGINT + } } } else { err = runInletsClient(pro, hostStatus.IP, inletsControlPort, upstream, inletsToken, tcpPorts, "") - } - if err != nil { - return fmt.Errorf("Error running inlets: %v", err) + // Delete the host after getting an error while running inlets client by + // sending SIGINT to the sig channel + if err != nil { + fmt.Printf("Error running inlets: %v\n", err) + fmt.Println("Sending SIGINT.") + sig <- syscall.SIGINT + } } - <-deleted - close(deleted) + // Wait for the host deletion to complete + <-done + close(done) fmt.Println("exiting") return nil