vehiclediscovery

package
v0.41.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package vehiclediscovery finds the linked users nobody records, and primes their vehicles (single-owner M3-6).

A user's vehicles are leased to a pod by group; a user with a stored Rivian session but no vehicle row has no group, so no lease and no owner, and nothing records them. The sign-in's vehicle prime creates the rows, and /api/vehicles retries it when the user opens the app, but a user who does not open the app would be silently never recorded. (The boot-time refresh that used to find such users on every pod is gone.) So one pod, every Interval, looks up the vehicles of each such user and primes the rows; the lease coordinator then picks them up.

One pod runs it (a Postgres advisory lock). Per user, a backoff held in Redis under the install's key prefix, named by an HMAC of the user id, bounds the Rivian calls: an error is retried after ErrorBackoff, an empty result (an account with no vehicle yet, e.g. a reservation) after EmptyBackoff, so a vehicle-less account does not cost a Rivian call every half hour for ever. A lookup that says the Rivian session is invalid goes through the client's own reauth path (the flag and the one-time email) and the user is not looked up again until they sign in: the candidate query leaves flagged users out.

Index

Constants

View Source
const (
	Interval     = 30 * time.Minute
	ErrorBackoff = 30 * time.Minute
	EmptyBackoff = 24 * time.Hour

	Pace = 3 * time.Second
)
View Source
const (
	OutcomeStored      = "stored"
	OutcomeEmpty       = "empty"
	OutcomeError       = "error"
	OutcomeSkipped     = "skipped"
	OutcomeNeedsReauth = "needs_reauth"
	OutcomeRefreshed   = "refreshed" // a user with vehicles: the owner's slow re-read of the account's vehicle list
	// OutcomeRefreshError is a failed owner re-read (lookup or upsert), kept apart from OutcomeError so discovery failures read on their own.
	OutcomeRefreshError = "refresh_error"
)

Outcomes of one user's turn, the label of the counter.

View Source
const ForceEvery = 10 * time.Minute

ForceEvery is the shortest time between two sign-in lookups of the same user: a fresh sign-in looks up again at once whatever the 'none' or 'error' backoff says (the user may have just got the truck), but a user who signs in over and over costs Rivian one call per ForceEvery.

View Source
const OwnerInterval = 5 * time.Minute

OwnerInterval is how often the owner job looks for users that are due; each user's own next_check_at decides whether they are looked up.

View Source
const RefreshEvery = 24 * time.Hour

RefreshEvery is how often the owner re-reads the vehicle list of a user who already has vehicles, so a second truck added to the Rivian account (or a changed name or trim) reaches the rows without /api/vehicles asking Rivian. The first look is RefreshEvery (plus up to an eighth of it, per user) after this pod first owns the user: a pod that has just taken a group has just started its subscriptions, and the monitor's own refresh at subscription start covers that moment. A failed refresh is tried again after ErrorBackoff. A truck that left the account keeps its row, as it did when /api/vehicles upserted the list on each call.

Variables

This section is empty.

Functions

func BackoffKey

func BackoffKey(limiterKey []byte) []byte

BackoffKey derives the HMAC key from the limiters' key (a purpose-labelled subkey, as FailLimiterKey does from the server's secret). nil stays nil.

Types

type Backoff

type Backoff interface {
	Held(ctx context.Context, uid uuid.UUID) bool
	Hold(ctx context.Context, uid uuid.UUID, d time.Duration)
}

Backoff remembers, per user, until when not to look up again.

type Job

type Job struct {
	DB      *sql.DB
	Lookup  Lookup
	Backoff Backoff
	Observe func(outcome string)
	Logger  *slog.Logger
	Sleep   func(ctx context.Context, d time.Duration)

	// Owner mode (single-owner M4-2b, RIVOLT_OWNER_DISCOVERY): Users returns the users of the groups this pod owns. The job then runs on every
	// pod for its own users, takes no advisory lock, and keeps what it found in vehicle_discovery (so the API can tell "looking" from "none")
	// instead of a Redis backoff. Nil: the original single-pod job.
	Users func(ctx context.Context) ([]OwnedUser, error)

	// Now is the clock of the refresh schedule (tests); nil is time.Now.
	Now func() time.Time
	// contains filtered or unexported fields
}

func (*Job) LookUpNow added in v0.41.7

func (j *Job) LookUpNow(ctx context.Context, uid uuid.UUID)

LookUpNow looks one user up at once if this pod owns them and they are due (the adopt nudge, synchronously).

func (*Job) NudgeUser added in v0.41.7

func (j *Job) NudgeUser(uid uuid.UUID)

NudgeUser asks the owner job to look this user up at once (best effort: a full queue drops it, the next tick finds the user anyway).

func (*Job) OwnerMode added in v0.41.7

func (j *Job) OwnerMode(users func(ctx context.Context) ([]OwnedUser, error))

OwnerMode switches the job to owner mode with users as the pod's own users and creates the nudge queue. Call it before Run and before the job is shared with NudgeUser; nothing after that writes the queue.

func (*Job) OwnerTick added in v0.41.7

func (j *Job) OwnerTick(ctx context.Context) error

OwnerTick is one owner-mode pass now (tests, and a caller that wants a pass without waiting for the ticker).

func (*Job) Run

func (j *Job) Run(ctx context.Context)

Run ticks every Interval until ctx ends (owner mode: every OwnerInterval, and when a user is nudged).

func (*Job) SignedIn added in v0.41.7

func (j *Job) SignedIn(ctx context.Context, uid uuid.UUID)

SignedIn is signedIn for a caller that runs it itself (tests).

func (*Job) Tick

func (j *Job) Tick(ctx context.Context)

Tick is one pass, under the cross-replica lock: a pod that does not get it does nothing.

type Lookup

type Lookup func(ctx context.Context, uid uuid.UUID) ([]rivian.Vehicle, error)

Lookup asks Rivian for the user's vehicles with the user's own session (the caller restores it from the secrets store if this pod's client has none). It is the only Rivian call the job makes.

type OwnedUser added in v0.41.7

type OwnedUser struct {
	UID   uuid.UUID
	Token fence.Token
}

OwnedUser is a user this pod owns, with the fence token of their group: the job writes its answer under it, so a pod that has lost the group writes nothing.

type RedisBackoff

type RedisBackoff struct {
	// contains filtered or unexported fields
}

RedisBackoff keeps the backoff in Redis under prefix, named by an HMAC of the user id (a user id never reaches Redis in clear), with the backoff as the TTL. A pod without Redis (or when Redis errors) keeps it in memory; only one pod runs the job at a time, so memory loses at most one extra attempt per handover.

func NewRedisBackoff

func NewRedisBackoff(rdb *redis.Client, prefix string, limiterKey []byte) *RedisBackoff

func (*RedisBackoff) Held

func (b *RedisBackoff) Held(ctx context.Context, uid uuid.UUID) bool

func (*RedisBackoff) Hold

func (b *RedisBackoff) Hold(ctx context.Context, uid uuid.UUID, d time.Duration)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL