Skip to content

Commit

Permalink
Add retry capability
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Jan 8, 2024
1 parent 0acb927 commit 5ddaa8f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ If all goes fine, later this repository will provide you both a GoLang SDK and a

## TODOs
* [ ] Create json output for machines. Improve documentation accordingly.
* [ ] Add retries. `status` command produces `PORT_ERROR` quite frequently while second try works fine.
* [x] Add retries. `status` command produces `PORT_ERROR` quite frequently while second try works fine.
* [x] Improve token handling. Token is stored in `config.yaml` but it seems to be invalidated after a while. It should be renewed on demand.

## Usage
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ const (
LogoutCmdName = "logout"
PingCmdName = "ping"
TokenExpirationTime = time.Minute * 5
retryCount = 3
)
10 changes: 8 additions & 2 deletions cli/cmd/getName.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ func GetName(localMac, mac [6]byte, host string, port int, token uint32) error {
}
}()

name, err := client.GetName()
var name string
err = retry(func() error {
var err2 error
name, err2 = client.GetName()
return err2
})

if err != nil {
return err
return nil
}

log.Infof("Received name: %s", name)
Expand Down
8 changes: 7 additions & 1 deletion cli/cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func listGroups(localMac [6]byte, mac [6]byte, host string, port int, token uint
}
}()

groups, err := client.GetGroups()
var groups *sdk.Groups
err = retry(func() error {
var err2 error
groups, err2 = client.GetGroups()
return err2
})

if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions cli/cmd/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

func retry(f func() error) error {
var err error

for i := 0; i < retryCount; i++ {
err = f()
if err == nil {
break
}
log.Debugf("Retriable error: %v", err)
}

return err
}
6 changes: 5 additions & 1 deletion cli/cmd/setState.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func setStatus(localMac [6]byte, mac [6]byte, host string, port int, devicePort
}
}()

err = client.SetState(devicePort)
err = retry(func() error {
err2 := client.SetState(devicePort)
return err2
})

if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion cli/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bisecure/cli"
"bisecure/sdk"
"bisecure/sdk/payload"
"github.com/spf13/viper"
"os"

Expand Down Expand Up @@ -57,7 +58,13 @@ func getStatus(localMac [6]byte, mac [6]byte, host string, port int, devicePort
}
}()

status, err := client.GetTransition(devicePort)
var status *payload.HmGetTransitionResponse
err = retry(func() error {
var err2 error
status, err2 = client.GetTransition(devicePort)
return err2
})

if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion cli/cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ func listUsers(localMac [6]byte, mac [6]byte, host string, port int, token uint3
}
}()

users, err := client.GetUsers()
var users *sdk.Users
err = retry(func() error {
var err2 error
users, err2 = client.GetUsers()
return err2
})

if err != nil {
return err
}
Expand Down

0 comments on commit 5ddaa8f

Please sign in to comment.