Vehicle: refresh drivesomethinggreater on portal schedule#30368
Merged
Conversation
The EU Data Act portal stores a new dataset roughly every 15 minutes and answers the datadelivery list endpoint with 404 "No files available for this request" until the vehicle has delivered its first dataset. Map that 404 to api.ErrNotAvailable so readings report "not available" instead of a raw status error. Status now returns the dataset timestamp, parsed from the compact file-name prefix (createdOn as fallback). The provider keeps a resettable cache and schedules a time.AfterFunc to reset it when the next dataset is expected (timestamp + 15min + 30s), aligning polling with the portal's delivery instead of a blind wall-clock interval.
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- The
Provider's use oftimer *time.TimerinNewProviderlooks racy under concurrent calls, sincev.timeris read/written andtimer.Stop()/time.AfterFuncare invoked without any synchronization; consider guardingtimerwith a mutex or otherwise ensuring thread safety around cache reset scheduling.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `Provider`'s use of `timer *time.Timer` in `NewProvider` looks racy under concurrent calls, since `v.timer` is read/written and `timer.Stop()`/`time.AfterFunc` are invoked without any synchronization; consider guarding `timer` with a mutex or otherwise ensuring thread safety around cache reset scheduling.
## Individual Comments
### Comment 1
<location path="vehicle/vw/eudataact/provider.go" line_range="29" />
<code_context>
+// margin), so the data is refreshed as soon as the portal delivers it.
type Provider struct {
statusG func() (map[string]string, error)
+ timer *time.Timer
}
</code_context>
<issue_to_address>
**issue (bug_risk):** Timer field updates are not synchronized and can lead to data races under concurrent access
The supplier passed to `util.ResettableCached` may run in multiple goroutines. Inside that supplier you read, stop, and reassign `v.timer` without synchronization, causing a data race on both the field and the underlying `time.Timer`, which is not concurrency-safe.
Please guard all accesses and `Stop` calls on `v.timer` with a mutex (e.g., wrap it in a small struct with its own lock). Alternatively, avoid storing the timer on `Provider` and keep it internal to the cache implementation if that better suits the design.
</issue_to_address>
### Comment 2
<location path="vehicle/vw/eudataact/provider.go" line_range="37" />
<code_context>
+ v := &Provider{}
+
+ var cached util.Cacheable[map[string]string]
+ cached = util.ResettableCached(func() (map[string]string, error) {
+ data, ts, err := api.Status(vin)
+ if err == nil && !ts.IsZero() {
</code_context>
<issue_to_address>
**issue (complexity):** Consider extracting the timer management into a Provider helper method to simplify the cache closure and clarify responsibilities.
You can simplify the lifecycle concerns a bit without changing behaviour by:
1. Pulling the timer logic out of the cache factory into a small helper method on `Provider`.
2. Making the self-referential use of `cached.Reset` more explicit/isolated.
That keeps the API and semantics intact, but reduces the amount of work happening inside the closure and makes the responsibilities clearer.
For example:
```go
type Provider struct {
statusG func() (map[string]string, error)
timer *time.Timer
}
func (p *Provider) scheduleReset(ts time.Time, reset func()) {
if ts.IsZero() {
return
}
if p.timer != nil {
p.timer.Stop()
}
p.timer = time.AfterFunc(resetDelay(ts, time.Now()), reset)
}
func NewProvider(api *API, vin string, cache time.Duration) *Provider {
v := &Provider{}
var cached util.Cacheable[map[string]string]
cached = util.ResettableCached(func() (map[string]string, error) {
data, ts, err := api.Status(vin)
if err == nil {
// delegate timer management to Provider helper
v.scheduleReset(ts, cached.Reset)
}
return data, err
}, cache)
v.statusG = cached.Get
return v
}
```
This keeps:
- `util.ResettableCached` usage unchanged.
- The self-referential pattern (`cached.Reset` in the closure) intact, so behaviour is the same.
But it:
- Removes timer stop/restart details from the cache factory, so the closure only coordinates fetching + scheduling.
- Centralises timer lifecycle into `scheduleReset`, making it easier to reason about and unit test independently.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Propagate the parse failure from nameTime instead of swallowing it; the dataset timestamp falls back to createdOn on error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The EU Data Act portal stores a new dataset roughly every 15 minutes and answers the datadelivery list endpoint with 404 "No files available for this request" until the vehicle has delivered its first dataset.
20260531102941_VIN_..._no_content_found.zip), with createdOn as fallback.Refs #30324