Expedite keep alive time for token refresh

pull/209/head
M66B 2 years ago
parent 85bd4c6967
commit 8162d8e0b3

@ -889,8 +889,8 @@ public class EmailService implements AutoCloseable {
return false; return false;
} }
public void check() { public Long getAccessTokenExpirationTime() {
authenticator.checkToken(); return authenticator.getAccessTokenExpirationTime();
} }
public boolean isOpen() { public boolean isOpen() {

@ -56,7 +56,7 @@ public class ServiceAuthenticator extends Authenticator {
static final int AUTH_TYPE_GMAIL = 2; static final int AUTH_TYPE_GMAIL = 2;
static final int AUTH_TYPE_OAUTH = 3; static final int AUTH_TYPE_OAUTH = 3;
static final long MIN_EXPIRE_INTERVAL = 12 * 60 * 1000L; static final long MIN_EXPIRE_INTERVAL = 15 * 60 * 1000L;
ServiceAuthenticator( ServiceAuthenticator(
Context context, Context context,
@ -125,24 +125,19 @@ public class ServiceAuthenticator extends Authenticator {
return password; return password;
} }
void checkToken() { Long getAccessTokenExpirationTime() {
Long expiration = null;
try { try {
if (auth == AUTH_TYPE_GMAIL) { if (auth == AUTH_TYPE_GMAIL) {
GmailState authState = GmailState.jsonDeserialize(password); GmailState authState = GmailState.jsonDeserialize(password);
expiration = authState.getAccessTokenExpirationTime(); return authState.getAccessTokenExpirationTime();
} else if (auth == AUTH_TYPE_OAUTH) { } else if (auth == AUTH_TYPE_OAUTH) {
AuthState authState = AuthState.jsonDeserialize(password); AuthState authState = AuthState.jsonDeserialize(password);
expiration = authState.getAccessTokenExpirationTime(); return authState.getAccessTokenExpirationTime();
} }
} catch (JSONException ex) { } catch (JSONException ex) {
Log.e(ex); Log.e(ex);
} }
return null;
long slack = Math.min(keep_alive, MIN_EXPIRE_INTERVAL);
if (expiration != null && expiration - slack < new Date().getTime())
throw new IllegalStateException(Log.TOKEN_REFRESH_REQUIRED);
} }
interface IAuthenticated { interface IAuthenticated {

@ -2239,10 +2239,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
throw new StoreClosedException(iservice.getStore(), "Unrecoverable", cause); throw new StoreClosedException(iservice.getStore(), "Unrecoverable", cause);
} }
// Check token expiration
if (!account.isTransient(this))
iservice.check();
// Sends store NOOP // Sends store NOOP
if (EmailService.SEPARATE_STORE_CONNECTION) { if (EmailService.SEPARATE_STORE_CONNECTION) {
EntityLog.log(this, EntityLog.Type.Account, account, EntityLog.log(this, EntityLog.Type.Account, account,
@ -2357,6 +2353,16 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try { try {
long duration = account.poll_interval * 60 * 1000L; long duration = account.poll_interval * 60 * 1000L;
long trigger = System.currentTimeMillis() + duration; long trigger = System.currentTimeMillis() + duration;
if (!account.isTransient(this)) {
Long expirationTime = iservice.getAccessTokenExpirationTime();
if (expirationTime != null && expirationTime < trigger) {
EntityLog.log(this, EntityLog.Type.Debug, "Expedite keep alive" +
" from " + new Date(trigger) + " to " + new Date(expirationTime));
trigger = expirationTime;
}
}
EntityLog.log(this, EntityLog.Type.Account, account, EntityLog.log(this, EntityLog.Type.Account, account,
"### " + account.name + " keep alive" + "### " + account.name + " keep alive" +
" wait=" + account.poll_interval + " until=" + new Date(trigger)); " wait=" + account.poll_interval + " until=" + new Date(trigger));

Loading…
Cancel
Save