microg / GmsCore

Free implementation of Play Services
https://microg.org
Apache License 2.0
8.36k stars 1.7k forks source link

Use PCP and/or IGD to reduce keepalive messages for push notification connection if network supports it #1141

Open lndnv opened 4 years ago

lndnv commented 4 years ago

If network supports PCP (applicable to both carrier grade network and home network) or UPnP IGD (applicable to home network). Middlebox (e.g. NAT, firewall) connection state lifetime could be controlled by endpoint devices. MicroG could set a long lifetime to reduce keepalive/ping messages which would reduce battery consumption and network usage.

This is applicable to both IPv4 (usually NAT+firewall) and IPv6 (usually firewall).

IETF RFC6887: https://tools.ietf.org/html/rfc6887#section-10.3

PCP client library: https://github.com/libpcp/pcp UPnP IGD client library: https://github.com/miniupnp/miniupnp/blob/master/miniupnpc/java/JavaBridgeTest.java

mar-v-in commented 4 years ago

The Google servers have a ping timeout of ~30minutes themselves. Most mobile and wifi network already support 30min connection timeouts anyway (mobile network providers also sometimes do this specifically for the google cloud messaging connection). So I'm not sure how much this will be of use.

lndnv commented 4 years ago

The Google servers have a ping timeout of ~30minutes themselves.

Does this mean if there's no traffic for more than 30 minutes, the server would close the connection even if the device is still connected?

On my device, my Wi-Fi ping interval is automatically set to 30 minutes (I remember seeing 37 minutes at one point before it came down to 30 minutes), my mobile ping interval is automatically set to 11 minutes, some networks seemingly could still benefit from PCP assuming the networks support PCP.

mar-v-in commented 4 years ago

To be precise, Google servers just close the connection if there is no traffic for more than ~36-37 minutes. microG will thus ping after 30 minutes. This was hard-coded as a maximum at some point so that even if you have a bad connection (that can lead to not being able to transmit data for a minute or two) the connection keeps alive (and pinging every 30 minutes is not that different to every 36 minutes in terms of energy/data consumption).

I agree that implementing PCP might make sense and I'd be happy to accept a small and clean PR that does it, but it doesn't have a high priority for me as I believe the impact to be small. I also haven't checked if the original Google implementation actually uses PCP.

lndnv commented 4 years ago

To me this is also a nice to have feature rather than a high priority feature.

It's a pity that the servers don't do something similar to what microG does - ping back before disconnecting and increase interval after successful ping.

Here's an example of the Java binding of PCP's predecessor NAT-PMP, presumably something similar could be done for PCP - https://github.com/miniupnp/libnatpmp/tree/master/fr/free/miniupnp/libnatpmp . I might implement this and submit a patch should this feature become too important for me to ignore. For now I'm content with waiting for the official implementation from you or someone else's PR.

lndnv commented 4 years ago

Miniupnpd could be used for PCP testing (it also supports UPnP IGD). It could be installed on a computer or on an OpenWrt router.

https://github.com/miniupnp/miniupnp