Skip to content

Commit

Permalink
Merge pull request #235 from batchcorp/blinktag/fix_tunnel_updates
Browse files Browse the repository at this point in the history
Tunnel update fixes
  • Loading branch information
blinktag authored Feb 15, 2022
2 parents c58940f + 08ede74 commit 947bfa8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
23 changes: 18 additions & 5 deletions actions/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ func (a *Actions) UpdateTunnel(ctx context.Context, tunnelID string, tunnelOpts
return nil, errors.New("tunnel does not exist")
}

if d.Active {
// Close existing tunnel
d.CancelFunc()
d.Active = false
d.Options.XActive = false
d.Close()

// Give it a sec to close out connections and goroutines
time.Sleep(time.Second)
}

d.Options = tunnelOpts

// New contexts
Expand All @@ -161,12 +172,14 @@ func (a *Actions) UpdateTunnel(ctx context.Context, tunnelID string, tunnelOpts
}
d.Backend = be

if err := d.StartTunnel(5 * time.Second); err != nil {
return nil, errors.Wrap(err, "unable to start tunnel")
}
if tunnelOpts.XActive {
if err := d.StartTunnel(5 * time.Second); err != nil {
return nil, errors.Wrap(err, "unable to start tunnel")
}

d.Active = true
d.Options.XActive = true
d.Active = true
d.Options.XActive = true
}

// Update in-memory config
a.cfg.PersistentConfig.SetTunnel(tunnelID, d)
Expand Down
15 changes: 14 additions & 1 deletion server/tunnel_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,24 @@ func (s *Server) UpdateTunnel(ctx context.Context, req *protos.UpdateTunnelReque
return nil, CustomError(common.Code_UNAUTHENTICATED, fmt.Sprintf("invalid auth: %s", err))
}

currentTunnel := s.PersistentConfig.GetTunnel(req.TunnelId)
if currentTunnel.Active {
// Publish StopTunnel event
if err := s.Bus.PublishStopTunnel(ctx, currentTunnel.Options); err != nil {
return nil, fmt.Errorf("unable to publish stop tunnel event: %s", err)
}
}

if _, err := s.Actions.UpdateTunnel(ctx, req.TunnelId, req.Opts); err != nil {
// No need to roll back here since we haven't updated anything yet
return nil, CustomError(common.Code_ABORTED, err.Error())
}

if err := s.Bus.PublishUpdateTunnel(ctx, req.Opts); err != nil {
// TODO: Should have rollback
return nil, fmt.Errorf("unable to publish update tunnel event: %s", err)
}

s.Log.Infof("Tunnel '%s' updated", req.TunnelId)

return &protos.UpdateTunnelResponse{
Expand All @@ -134,7 +147,7 @@ func (s *Server) StopTunnel(ctx context.Context, req *protos.StopTunnelRequest)
return nil, CustomError(common.Code_ABORTED, err.Error())
}

// Publish CreateTunnel event
// Publish StopTunnel event
if err := s.Bus.PublishStopTunnel(ctx, tunnelOptions.Options); err != nil {
// TODO: Should have rollback
s.Log.Errorf("unable to publish stop tunnel event: %s", err)
Expand Down

0 comments on commit 947bfa8

Please sign in to comment.