periodically renew endpoints
This commit is contained in:
parent
1e0904a48d
commit
0e10ae387a
|
@ -19,6 +19,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
|
import eu.siacs.conversations.services.UnifiedPushBroker;
|
||||||
|
|
||||||
public class UnifiedPushDatabase extends SQLiteOpenHelper {
|
public class UnifiedPushDatabase extends SQLiteOpenHelper {
|
||||||
private static final String DATABASE_NAME = "unified-push-distributor";
|
private static final String DATABASE_NAME = "unified-push-distributor";
|
||||||
|
@ -84,8 +85,7 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper {
|
||||||
|
|
||||||
public List<PushTarget> getRenewals(final String account, final String transport) {
|
public List<PushTarget> getRenewals(final String account, final String transport) {
|
||||||
final ImmutableList.Builder<PushTarget> renewalBuilder = ImmutableList.builder();
|
final ImmutableList.Builder<PushTarget> renewalBuilder = ImmutableList.builder();
|
||||||
// TODO use a date somewhat in the future to account for period renewal triggers
|
final long expiration = System.currentTimeMillis() + UnifiedPushBroker.TIME_TO_RENEW;
|
||||||
final long expiration = System.currentTimeMillis();
|
|
||||||
final SQLiteDatabase sqLiteDatabase = getReadableDatabase();
|
final SQLiteDatabase sqLiteDatabase = getReadableDatabase();
|
||||||
try (final Cursor cursor =
|
try (final Cursor cursor =
|
||||||
sqLiteDatabase.query(
|
sqLiteDatabase.query(
|
||||||
|
@ -108,13 +108,14 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper {
|
||||||
|
|
||||||
public ApplicationEndpoint getEndpoint(
|
public ApplicationEndpoint getEndpoint(
|
||||||
final String account, final String transport, final String instance) {
|
final String account, final String transport, final String instance) {
|
||||||
|
final long expiration = System.currentTimeMillis() + UnifiedPushBroker.TIME_TO_RENEW;
|
||||||
final SQLiteDatabase sqLiteDatabase = getReadableDatabase();
|
final SQLiteDatabase sqLiteDatabase = getReadableDatabase();
|
||||||
try (final Cursor cursor =
|
try (final Cursor cursor =
|
||||||
sqLiteDatabase.query(
|
sqLiteDatabase.query(
|
||||||
"push",
|
"push",
|
||||||
new String[] {"application", "endpoint"},
|
new String[] {"application", "endpoint"},
|
||||||
"account = ? AND transport = ? AND instance = ? AND endpoint IS NOT NULL AND expiration >= "
|
"account = ? AND transport = ? AND instance = ? AND endpoint IS NOT NULL AND expiration >= "
|
||||||
+ System.currentTimeMillis(),
|
+ expiration,
|
||||||
new String[] {account, transport, instance},
|
new String[] {account, transport, instance},
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|
|
@ -15,6 +15,9 @@ import com.google.common.io.BaseEncoding;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
import eu.siacs.conversations.R;
|
||||||
|
@ -28,10 +31,24 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
||||||
|
|
||||||
public class UnifiedPushBroker {
|
public class UnifiedPushBroker {
|
||||||
|
|
||||||
|
// time to expiration before a renewal attempt is made (24 hours)
|
||||||
|
public static final long TIME_TO_RENEW = 86_400_000L;
|
||||||
|
|
||||||
|
// interval for the 'cron tob' that attempts renewals for everything that expires is lass than
|
||||||
|
// `TIME_TO_RENEW`
|
||||||
|
public static final long RENEWAL_INTERVAL = 3_600_000L;
|
||||||
|
|
||||||
|
private static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(1);
|
||||||
|
|
||||||
private final XmppConnectionService service;
|
private final XmppConnectionService service;
|
||||||
|
|
||||||
public UnifiedPushBroker(final XmppConnectionService xmppConnectionService) {
|
public UnifiedPushBroker(final XmppConnectionService xmppConnectionService) {
|
||||||
this.service = xmppConnectionService;
|
this.service = xmppConnectionService;
|
||||||
|
SCHEDULER.scheduleAtFixedRate(
|
||||||
|
this::renewUnifiedPushEndpoints,
|
||||||
|
RENEWAL_INTERVAL,
|
||||||
|
RENEWAL_INTERVAL,
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void renewUnifiedPushEndpointsOnBind(final Account account) {
|
public void renewUnifiedPushEndpointsOnBind(final Account account) {
|
||||||
|
@ -68,6 +85,13 @@ public class UnifiedPushBroker {
|
||||||
final List<UnifiedPushDatabase.PushTarget> renewals =
|
final List<UnifiedPushDatabase.PushTarget> renewals =
|
||||||
unifiedPushDatabase.getRenewals(
|
unifiedPushDatabase.getRenewals(
|
||||||
account.getUuid(), transport.transport.toEscapedString());
|
account.getUuid(), transport.transport.toEscapedString());
|
||||||
|
Log.d(
|
||||||
|
Config.LOGTAG,
|
||||||
|
account.getJid().asBareJid()
|
||||||
|
+ ": "
|
||||||
|
+ renewals.size()
|
||||||
|
+ " UnifiedPush endpoints scheduled for renewal on "
|
||||||
|
+ transport.transport);
|
||||||
for (final UnifiedPushDatabase.PushTarget renewal : renewals) {
|
for (final UnifiedPushDatabase.PushTarget renewal : renewals) {
|
||||||
Log.d(
|
Log.d(
|
||||||
Config.LOGTAG,
|
Config.LOGTAG,
|
||||||
|
@ -110,6 +134,8 @@ public class UnifiedPushBroker {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
renewUnifiedPushEndpoint(transport, renewal, endpoint, expiration);
|
renewUnifiedPushEndpoint(transport, renewal, endpoint, expiration);
|
||||||
|
} else {
|
||||||
|
Log.d(Config.LOGTAG, "could not register UP endpoint " + response.getErrorCondition());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue