Skip to content

Commit

Permalink
Add timeout command-line options
Browse files Browse the repository at this point in the history
Adds timeout command-line options that override the defaults.
  • Loading branch information
Seth Terashima committed Mar 8, 2024
1 parent 9d9cbc8 commit de089b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
22 changes: 13 additions & 9 deletions cmd/tesla-control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func Usage() {
}
}

func runCommand(acct *account.Account, car *vehicle.Vehicle, args []string) int {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
func runCommand(acct *account.Account, car *vehicle.Vehicle, args []string, timeout time.Duration) int {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

if err := execute(ctx, acct, car, args); err != nil {
Expand All @@ -73,7 +73,7 @@ func runCommand(acct *account.Account, car *vehicle.Vehicle, args []string) int
return 0
}

func runInteractiveShell(acct *account.Account, car *vehicle.Vehicle) int {
func runInteractiveShell(acct *account.Account, car *vehicle.Vehicle, timeout time.Duration) int {
scanner := bufio.NewScanner(os.Stdin)
for fmt.Printf("> "); scanner.Scan(); fmt.Printf("> ") {
args, err := shlex.Split(scanner.Text())
Expand All @@ -87,7 +87,7 @@ func runInteractiveShell(acct *account.Account, car *vehicle.Vehicle) int {
writeErr("Invalid command: %s", err)
continue
}
runCommand(acct, car, args)
runCommand(acct, car, args, timeout)
}
if err := scanner.Err(); err != nil {
writeErr("Error reading command: %s", err)
Expand All @@ -103,8 +103,10 @@ func main() {
}()

var (
debug bool
forceBLE bool
debug bool
forceBLE bool
commandTimeout time.Duration
connTimeout time.Duration
)
config, err := cli.NewConfig(cli.FlagAll)
if err != nil {
Expand All @@ -114,6 +116,8 @@ func main() {
flag.Usage = Usage
flag.BoolVar(&debug, "debug", false, "Enable verbose debugging messages")
flag.BoolVar(&forceBLE, "ble", false, "Force BLE connection even if OAuth environment variables are defined")
flag.DurationVar(&commandTimeout, "command-timeout", 5*time.Second, "Set timeout for commands sent to the vehicle.")
flag.DurationVar(&connTimeout, "connect-timeout", 20*time.Second, "Set timeout for establishing initial connection.")

config.RegisterCommandLineFlags()
flag.Parse()
Expand Down Expand Up @@ -150,7 +154,7 @@ func main() {
return
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), connTimeout)
defer cancel()

acct, car, err := config.Connect(ctx)
Expand All @@ -171,8 +175,8 @@ func main() {
}

if flag.NArg() > 0 {
status = runCommand(acct, car, flag.Args())
status = runCommand(acct, car, flag.Args(), commandTimeout)
} else {
status = runInteractiveShell(acct, car)
status = runInteractiveShell(acct, car, commandTimeout)
}
}
4 changes: 4 additions & 0 deletions cmd/tesla-http-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
"time"

"github.com/teslamotors/vehicle-command/internal/log"
"github.com/teslamotors/vehicle-command/pkg/cli"
Expand Down Expand Up @@ -43,6 +44,7 @@ func main() {
verbose bool
host string
port int
timeout time.Duration
)

config, err := cli.NewConfig(cli.FlagPrivateKey)
Expand All @@ -64,6 +66,7 @@ func main() {
flag.BoolVar(&verbose, "verbose", false, "Enable verbose logging")
flag.StringVar(&host, "host", "localhost", "Proxy server `hostname`")
flag.IntVar(&port, "port", defaultPort, "`Port` to listen on")
flag.DurationVar(&timeout, "timeout", proxy.DefaultTimeout, "Timeout interval when sending commands")
flag.Usage = Usage
config.RegisterCommandLineFlags()
flag.Parse()
Expand Down Expand Up @@ -97,6 +100,7 @@ func main() {
if err != nil {
return
}
p.Timeout = timeout
addr := fmt.Sprintf("%s:%d", host, port)
log.Info("Listening on %s", addr)

Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

const (
defaultTimeout = 10 * time.Second
DefaultTimeout = 10 * time.Second
maxRequestBodyBytes = 512
vinLength = 17
proxyProtocolVersion = "tesla-http-proxy/1.0.0"
Expand Down Expand Up @@ -90,7 +90,7 @@ func (p *Proxy) unlockVIN(vin string) {
// command-authentication key, not a TLS key.)
func New(ctx context.Context, skey protocol.ECDHPrivateKey, cacheSize int) (*Proxy, error) {
return &Proxy{
Timeout: defaultTimeout,
Timeout: DefaultTimeout,
commandKey: skey,
sessions: cache.New(cacheSize),
}, nil
Expand Down

0 comments on commit de089b5

Please sign in to comment.