Writing a client to fetch data, how to refresh state properly?

Hi, I’m writing a client to fetch data from server. I store the token and expire date in GenServer’s state. I’ m wondering how to refresh the token in time properly. One solution is check the expire date before fetch data. Another elegant way I though is everytime I fetch token from a server, :timer.sender_after/3 will be called and I refresh token in handle_info/2 . Any suggestion?

I’d like to say that it probably depends on your requirements…

If you can do a simple ‘refresh’ of the token as long as it’s still valid, but have to go through a complicated authentication process if it ever expires, definitely definitely always keep it fresh.

On the other hand, if it’s all the same, and the extra round-trip of a token refresh doesn’t really matter when you fetch data, then sure, just check the timestamp and refresh it only as needed while you are connecting to fetch data anyway.