I'm writing a REST client and it has an access token with a fixed expiration time.
How do I ensure that a new token is requested before the expiration time? I see two options:
- Timer
- Some shenanigans with the finalize method possibly using the ref package
1) would require me to create a new thread that would run in a given time and request a new token. It seems wasteful to me.
2) would take advantage of the GC thread which will run anyways, so I would not need to create a new thread. When finalize
runs just check if the token is about to expire, if yes request a new one, if not make the object available again until the next GC cycle runs.
Comments or other ideas?