Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 503 and retry #108

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions internal/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,20 +397,39 @@ func (d *Dispatcher) Send(ctx context.Context, message *universal.RoutableMessag
}
}()

possibleSuccess := false
for {
err = d.conn.Send(ctx, encodedMessage)
if err == nil {
return resp, nil
}
if !protocol.ShouldRetry(err) {
log.Warning("[%02x] Terminal transmission error: %s", message.GetUuid(), err)
return nil, err
log.Debug("[%02x] Received error %s", message.GetUuid(), err)

// The vehicle will de-duplicate exact copies of encodedMessage and re-send the cached
// response, so we're free to resend without risk of executing the action multiple times.
// However, we need to notify callers higher up on the stack that a command may have
// succeeded so that they do not retry with, e.g., a higher counter value.
if protocol.MayHaveSucceeded(err) {
log.Debug("[%02x] Command may have succeed (vehicle discards duplicates)", message.GetUuid())
possibleSuccess = true
}
if !protocol.Temporary(err) {
log.Warning("[%02x] Error is terminal", message.GetUuid())
return nil, &protocol.CommandError{
Err: err,
PossibleSuccess: possibleSuccess,
PossibleTemporary: false,
}
}
log.Debug("[%02x] Retrying transmission after error: %s", message.GetUuid(), err)
select {
case <-ctx.Done():
return nil, &protocol.CommandError{Err: ctx.Err(), PossibleSuccess: false, PossibleTemporary: true}
return nil, &protocol.CommandError{
Err: ctx.Err(),
PossibleSuccess: possibleSuccess,
PossibleTemporary: true,
}
case <-time.After(d.conn.RetryInterval()):
log.Debug("[%02x] Retrying transmission", message.GetUuid())
continue
}
}
Expand Down
61 changes: 59 additions & 2 deletions internal/dispatcher/dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func TestVehicleUnreachable(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), quiescentDelay)
defer cancel()

if _, err := dispatcher.Send(ctx, testCommand(), connector.AuthMethodNone); err != errTimeout {
if _, err := dispatcher.Send(ctx, testCommand(), connector.AuthMethodNone); !errors.Is(err, errTimeout) {
t.Errorf("Unexpected error: %s", err)
}
}
Expand Down Expand Up @@ -706,7 +706,7 @@ func TestRetrySend(t *testing.T) {
conn.EnqueueSendError(&protocol.CommandError{Err: errTimeout, PossibleSuccess: false, PossibleTemporary: true})
}
errFoo := errors.New("not enough pylons")
conn.EnqueueSendError(&protocol.CommandError{Err: errFoo, PossibleSuccess: true, PossibleTemporary: true})
conn.EnqueueSendError(&protocol.CommandError{Err: errFoo, PossibleSuccess: true, PossibleTemporary: false})

req := testCommand()
rsp, err := dispatcher.Send(ctx, req, connector.AuthMethodNone)
Expand All @@ -718,6 +718,63 @@ func TestRetrySend(t *testing.T) {
}
}

func TestReportPossibleSuccess(t *testing.T) {
dispatcher, conn := getTestSetup(t)
defer conn.Close()

const errCount = 3
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

errFoo := errors.New("not enough pylons")
conn.EnqueueSendError(&protocol.CommandError{Err: errFoo, PossibleSuccess: true, PossibleTemporary: true})

for i := 0; i < errCount; i++ {
conn.EnqueueSendError(&protocol.CommandError{Err: errTimeout, PossibleSuccess: false, PossibleTemporary: true})
}

conn.EnqueueSendError(&protocol.CommandError{Err: errTimeout, PossibleSuccess: false, PossibleTemporary: false})

req := testCommand()
rsp, err := dispatcher.Send(ctx, req, connector.AuthMethodNone)
if err == nil {
rsp.Close()
}
if !errors.Is(err, errTimeout) {
t.Errorf("Unexpected error: %s", err)
}
if !protocol.MayHaveSucceeded(err) {
t.Errorf("Dispatcher did not flag possible success: %+v", err)
}
}

func TestReportCertainFailure(t *testing.T) {
dispatcher, conn := getTestSetup(t)
defer conn.Close()

const errCount = 3
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

for i := 0; i < errCount; i++ {
conn.EnqueueSendError(&protocol.CommandError{Err: errTimeout, PossibleSuccess: false, PossibleTemporary: true})
}

conn.EnqueueSendError(&protocol.CommandError{Err: errTimeout, PossibleSuccess: false, PossibleTemporary: false})

req := testCommand()
rsp, err := dispatcher.Send(ctx, req, connector.AuthMethodNone)
if err == nil {
rsp.Close()
}
if !errors.Is(err, errTimeout) {
t.Errorf("Unexpected error: %s", err)
}
if protocol.MayHaveSucceeded(err) {
t.Errorf("Dispatcher flagged as possible success: %+v", err)
}
}

func TestSendTimeout(t *testing.T) {
dispatcher, conn := getTestSetup(t)
defer conn.Close()
Expand Down
10 changes: 4 additions & 6 deletions pkg/connector/inet/inet.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,13 @@ func (e *HttpError) MayHaveSucceeded() bool {
if e.Code >= 400 && e.Code < 500 {
return false
}
return e.Code != http.StatusServiceUnavailable
return true
}

func (e *HttpError) Temporary() bool {
return e.Code == http.StatusServiceUnavailable ||
e.Code == http.StatusGatewayTimeout ||
e.Code == http.StatusRequestTimeout ||
e.Code == http.StatusMisdirectedRequest ||
e.Code == http.StatusTooManyRequests
// See https://developer.tesla.com/docs/tesla-fleet-api#response-codes
return e.Code == http.StatusServiceUnavailable || // 503 - Did not receive vehicle response
e.Code == http.StatusMisdirectedRequest // 421 - Wrong domain (client corrects domain based on server response)
}

func SendFleetAPICommand(ctx context.Context, client *http.Client, userAgent, authHeader string, url string, command interface{}) ([]byte, error) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/protocol/protobuf/vcsec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ message InformationRequest {
enum RKEAction_E {
RKE_ACTION_UNLOCK = 0;
RKE_ACTION_LOCK = 1;
RKE_ACTION_OPEN_TRUNK = 2;
RKE_ACTION_OPEN_FRUNK = 3;
RKE_ACTION_OPEN_CHARGE_PORT = 4;
RKE_ACTION_CLOSE_CHARGE_PORT = 5;
RKE_ACTION_REMOTE_DRIVE = 20;
RKE_ACTION_AUTO_SECURE_VEHICLE = 29;
RKE_ACTION_WAKE_VEHICLE = 30;
Expand Down
Loading
Loading