Skip to content

Commit

Permalink
added proxyclient retry
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrkefc committed Jan 28, 2025
1 parent d8601ed commit 154d422
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
4 changes: 1 addition & 3 deletions examples/proxyclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,5 @@ func main() {
proxyClient.Stop()
}()

if err := proxyClient.Start(ctx); err != nil {
logrus.Fatal(err)
}
proxyClient.Run(ctx)
}
69 changes: 37 additions & 32 deletions proxyclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func New(serverSharedSecret, namespace, certSecretName, certServerName string, r
return nil, fmt.Errorf("certSecretName required")
}

if serverSharedSecret == "" {
return nil, fmt.Errorf("server shared secret must be provided")
}

serverUrl := fmt.Sprintf("%s:%d%s", defaultServerAddr, defaultServerPort, defaultServerPath)
dialer, err := buildDialer(namespace, certSecretName, certServerName, restConfig)
if err != nil {
Expand Down Expand Up @@ -111,40 +115,41 @@ func buildDialer(namespace, certSecretName, certServerName string, restConfig *r
}, nil
}

func (c *ProxyClient) Start(ctx context.Context) error {
if err := c.forwarder.Start(); err != nil {
return err
}

defer c.forwarder.Stop()

logrus.Infof("ProxyClient connecting to %s", c.serverUrl)

headers := http.Header{}
if c.serverConnectSecret == "" {
return fmt.Errorf("server shared secret must be provided")
}

headers.Set("X-API-Tunnel-Secret", c.serverConnectSecret)

authFn := func(proto, address string) bool {
return true
}

onConnect := func(sessionCtx context.Context, session *remotedialer.Session) error {
logrus.Infoln("ProxyClient: remotedialer session connected!")
if c.onConnect != nil {
return c.onConnect(sessionCtx, session)
func (c *ProxyClient) Run(ctx context.Context) {
go func() {
for {
select {
case <-ctx.Done():
logrus.Infof("ProxyClient: ClientConnect finished. If no error, the session closed cleanly.")
return

default:
if err := c.forwarder.Start(); err != nil {
logrus.Errorf("remotedialer.ProxyClient error: %s ", err)
}

logrus.Infof("ProxyClient connecting to %s", c.serverUrl)

headers := http.Header{}
headers.Set("X-API-Tunnel-Secret", c.serverConnectSecret)

onConnectAuth := func(proto, address string) bool { return true }
onConnect := func(sessionCtx context.Context, session *remotedialer.Session) error {
logrus.Infoln("ProxyClient: remotedialer session connected!")
if c.onConnect != nil {
return c.onConnect(sessionCtx, session)
}
return nil
}

if err := remotedialer.ClientConnect(ctx, c.serverUrl, headers, c.dialer, onConnectAuth, onConnect); err != nil {
logrus.Errorf("remotedialer.ClientConnect error: %w", err)

Check failure on line 146 in proxyclient/client.go

View workflow job for this annotation

GitHub Actions / ci

github.com/sirupsen/logrus.Errorf does not support error-wrapping directive %w

Check failure on line 146 in proxyclient/client.go

View workflow job for this annotation

GitHub Actions / ci

github.com/sirupsen/logrus.Errorf does not support error-wrapping directive %w
}
}
}
return nil
}

if err := remotedialer.ClientConnect(ctx, c.serverUrl, headers, c.dialer, authFn, onConnect); err != nil {
return fmt.Errorf("remotedialer.ClientConnect error: %w", err)
}
}()

logrus.Infof("ProxyClient: ClientConnect finished. If no error, the session closed cleanly.")
return nil
<-ctx.Done()
}

func (c *ProxyClient) Stop() {
Expand Down

0 comments on commit 154d422

Please sign in to comment.