Skip to content

Commit

Permalink
Merge pull request #1 from viveksyngh/temp-tunnel-fix
Browse files Browse the repository at this point in the history
Handle context cancellation for temp tunnel
  • Loading branch information
utsavanand2 authored Apr 10, 2020
2 parents ebdf87e + 764f5e0 commit 8bf5c63
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,58 +263,70 @@ func runCreate(cmd *cobra.Command, _ []string) error {

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)
done <- true
cancelFunction()
if ok {
close(sig)
}
}()

fmt.Printf("Your IP is: %s\n", hostStatus.IP)
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)
}

done <- true
}()

var err error = nil
ctx, cancelFunction := context.WithCancel(context.Background())
if pro {
timeout := 30
retries := 90
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
}
}

_, ok := <-done
if ok {
close(done)
}
cancelFunction()
// Wait for the host deletion to complete
<-done
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 {
return fmt.Errorf("error deleting the exitnode: %v", err)
}
fmt.Println("exiting")
return nil
}

printExample(pro, hostStatus, inletsToken, provider, inletsControlPort)
return nil
}
Expand Down Expand Up @@ -582,7 +594,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)
Expand Down

0 comments on commit 8bf5c63

Please sign in to comment.