Closed
Description
Background: #301
The token fetched from the token server may have a TTL, see TokenResponse::expires_in. In most implementations, the value is about several hours. Our catalog client is a long-lived object, which means that we should handle the token expiration event.
There are two ways:
- Spawn a background task, and setup a ticker with a specified time interval, e.g.
expires_in * 0.9seconds, and refetch the token when triggered.
- Pros: Easy to implement
- Cons: Must trust the local timer skew
- Cons: Must introduce a timer, which means depending on a specified async runtime
- Call every methods with a retry wrapper. When meeting an unauthorized error, refetch the token and retry the method.
- Pros: Consistent with iceberg-python
- Pros: Does not rely on local clock and specific runtime
- Cons: When expired, thousands of concurrent requests may fail, and then all of them will trigger a token refetch, which is not ideal.
- This can be workaround by some concurrency control, to force only one request will refetch the token and others must wait for the result, but it introduced complexity.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Xuanwo commentedon Jul 5, 2024
Our rest catalog client now has a
authenticatefunction.iceberg-rust/crates/catalog/rest/src/client.rs
Line 88 in 0937f19
We can perform the expiration logic here.
liurenjie1024 commentedon Jul 5, 2024
I also prefer 2. The iceberg community may deprecate current auth approach in future, and introduce true oauth client in future. I prefer to have a simpler solution without introducing too much extra dependency.
TennyZhuang commentedon Jul 5, 2024
(expired_at, token)pair in an asyncMutex, and check whether it's close to expiration when needed. If it's close to expiration, lock the mutex, update the pair, and unlock. Then only one request will update the token.Xuanwo commentedon Jul 5, 2024
Great!
liurenjie1024 commentedon Jul 5, 2024
Sounds reasonable to me.
TennyZhuang commentedon Jul 5, 2024
Then we need https://docs.rs/tokio/latest/tokio/sync/struct.RwLock.html, still depends on tokio :(
This is rust
liurenjie1024 commentedon Jul 5, 2024
How about futures Mutex?
Xuanwo commentedon Jul 5, 2024
The current implementation utilizes
std::sync::Mutexwith a precisely controlled locking scope.iceberg-rust/crates/catalog/rest/src/client.rs
Lines 88 to 179 in 8562708
The worst thing that could happen here is that we might send multiple token refresh requests, which I think is fine for a catalog.
TennyZhuang commentedon Jul 5, 2024
It's acceptable, but not as expected. When a client request a token, the server may expect it will be used for several hours.
cmcarthur commentedon Jun 23, 2025
as an alternative, can we simply expose a public
refresh_tokenmethod that allows the caller to implement their own token invalidation (for my use case, via option 1, using tokio)this avoids the downside of "thousands of concurrent requests may fail", and does not require the core library to take any additional dependencies
this change perhaps does not remove the need to implement option 2 in this library, but it does provide an option for downstream consumers to implement token expiry as they wish (where there currently is no such option)
cmcarthur commentedon Jun 24, 2025
@liurenjie1024 I've implemented this here: #1465
feat(iceberg-catalog-rest): expose invalidate_token, regenerate_token…
liurenjie1024 commentedon Jun 25, 2025
Thanks, we could close this for now.