close
Skip to content

Fritz!: cache session in Settings instead of per-API connection#29574

Merged
andig merged 3 commits into
masterfrom
refactor/fritz-session
May 2, 2026
Merged

Fritz!: cache session in Settings instead of per-API connection#29574
andig merged 3 commits into
masterfrom
refactor/fritz-session

Conversation

@andig
Copy link
Copy Markdown
Member

@andig andig commented May 2, 2026

Summary

Refactor: pull session caching out of the AHA and Smarthome connections and let `fritz.Settings.GetSessionID` own it. Both API clients now just call the shared helper instead of maintaining their own `SID`/`updated` fields and `refreshSession` plumbing.

Test plan

  • `go build ./...`
  • `go test ./meter/... ./charger/...`
  • `go vet ./meter/fritz/... ./charger/...`
  • Smoke-test a fritzdect (AHA) and fritzdect (smarthome) device live

Moves session caching (SID + last-fetch timestamp) into fritz.Settings.
GetSessionID now returns the cached SID until the session expires and
refreshes transparently. The aha and smarthome connections drop their
own SID/updated fields and refreshSession helpers and just call
GetSessionID where they need a token.
@andig andig added the devices Specific device support label May 2, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Now that Settings caches sid/updated and is shared via pointers, GetSessionID introduces concurrent reads/writes to these fields across connections without synchronization; consider adding a mutex or other concurrency control around the cache to avoid data races.
  • Changing GetSessionID to use a pointer receiver and internal mutable state assumes each logical Fritzbox configuration uses a single shared *Settings instance; double-check that other call sites don’t copy Settings by value in a way that would unintentionally bypass the shared session cache.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `Settings` caches `sid`/`updated` and is shared via pointers, `GetSessionID` introduces concurrent reads/writes to these fields across connections without synchronization; consider adding a mutex or other concurrency control around the cache to avoid data races.
- Changing `GetSessionID` to use a pointer receiver and internal mutable state assumes each logical Fritzbox configuration uses a single shared `*Settings` instance; double-check that other call sites don’t copy `Settings` by value in a way that would unintentionally bypass the shared session cache.

## Individual Comments

### Comment 1
<location path="meter/fritz/types.go" line_range="24-25" />
<code_context>
 	URI, AIN, User, Password string
 	Firmware82               bool // use new REST API (FritzOS 8.2+)
+
+	sid     string
+	updated time.Time
 }

</code_context>
<issue_to_address>
**issue (bug_risk):** Accessing the cached SID and updated time via Settings may introduce data races if Settings is shared across goroutines.

Previously these values were per-connection, so contention wasn’t an issue. Now they’re shared via `*Settings`, so concurrent `GetSessionID` calls on the same instance will read/write `sid` and `updated` without synchronization, leading to races. If `Settings` can be used from multiple goroutines, please add appropriate synchronization (e.g., a mutex) or otherwise make `GetSessionID` concurrency-safe.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread meter/fritz/types.go
@andig andig merged commit c96dbed into master May 2, 2026
7 checks passed
@andig andig deleted the refactor/fritz-session branch May 2, 2026 12:51
@andig andig added the infrastructure Basic functionality label May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

devices Specific device support infrastructure Basic functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant