lundi 26 janvier 2015

Pattern for checking online service availability


I'm not sure that this is entirely on-topic, but I'll try to make it so.


I have an online service (API) and an Android application that uses it for all actions in the app. At the current moment, nothing is stored locally, so I have to depend on the API being online, and would like to warn the user when it's not (for whatever reason).


I'm wondering what the preferred method of checking availability is. As this is the first time I've ever done this, I've tried to do my research and have come up with three potential solutions:


1. Check availability on action


When the user takes an action, check the API's status. Currently I'm doing this (Java):



public class ServiceAvailability implements Runnable {

private volatile boolean available;

@Override
public void run() {

available = false;

try {
HttpURLConnection connection =
(HttpURLConnection) new URL("http://myservice.com").openConnection();
connection.setConnectTimeout(3000);
connection.setReadTimeout(1000);
connection.setRequestMethod("HEAD");

int responseCode = connection.getResponseCode();

if (200 <= responseCode && responseCode <= 399) {
available = true;
}
} catch (IOException exception) {
exception.printStackTrace();
}
}

public boolean isAvailable() {

return available;
}


It works just fine. Just send a HTTP HEAD request to the site and see if the response is a good one. Every time the user takes an action (e.g., runs a search, clicks someone's profile, etc.) it calls the isAvailable() method, and displays an error message in the event that the available = false.


The advantage of this is that if the service goes down, the user will immediately know, and will be warned that they will not be able to use the app until the service is brought online again.


I can't think of any disadvantages of this at the moment...


2. Check availability on interval


The other solution (that I haven't actually implemented) is run a thread in the background and HTTP HEAD the service every X seconds/minutes and update the status accordingly; at that point, I could call isAvailable() which would simply return the boolean set by the result of the HEAD request.


The advantage to this is that a visual can be shown (for the duration) when the service is not online, letting the user know ahead of time that their request will not be processed.


The disadvantage that I see is that while HEAD requests are very minimal in the amount of bandwidth they use, 3G users with very little data or users with slow connections might have trouble sending the HEAD and receiving the response in a timely manner.


3. Combine the two


The last approach that I see is a combination, where the thread in the background checks availability every X seconds, and action methods using the API also check availability through an additional HEAD request.


The advantage that I see here is that the user will know (visually) when the service is offline for the duration, as well as have a fallback if the status of the API happens to change during the X seconds duration that the API was last checked.


From what I can see, the Facebook Messenger app (iOS at least) implements this. When you open the app, it 'attempts to connect' (visual up top) and if your connection drops, it will let you know with a bright red visual. Trying to take action when you're not connected (to their service) also gives you an error.


Question


Is there a single one of these 3 methods that are preferable, or a different method that I haven't mentioned? Furthermore, is there an actual design pattern for this (e.g., Singleton on a thread), one more preferable than what I've implemented?





Aucun commentaire:

Enregistrer un commentaire