Random delays using Volley request

770 views Asked by At

I'm using a Volley singleton to send POST messages to a NanoHTTPD server made in java. The messages are short and the process time is fast, and there are no more than 1 message at the same time.

The problem is that randomly there are some delays in the queue to process the request, generating loading times of 3-4 seconds. The most common case is the first message(this is no problem, i guess is making some instances) or when I wait more than 5-10 seconds after any message. But if I send, for example 100 POST messages with 1 second delay between there is no problem.

I've checked the server and there is no request generated, so the problem is in the client side.

In those cases, the logcat show this:

D/Volley: [839] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.2.8:8765 0x355fcdd NORMAL 1> [lifetime=4768], [size=115], [rc=200], [retryCount=0]

Doing some google logSlowRequests means just that... the request took time to process in the client side.

Another solution to try is increasing the threadPoolSize but this didn't work. Always the first request takes a lot of time.

This is the singleton Code:

public class VolleySingleton {

    private static VolleySingleton mInstance;
    private RequestQueue mRequestQueue;
    private static Context mCtx;
    private DiskBasedCache cache;
    private BasicNetwork network;

    /**
     * @param context
     */
    private VolleySingleton(Context context) {
        mCtx = context;
        mRequestQueue = getRequestQueue();
    }

    /**
     * @param context
     * @return
     */
    public static synchronized VolleySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new VolleySingleton(context);
        }
        return mInstance;
    }

    /**
     * @return
     */
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // Instantiate the cache
            cache = new DiskBasedCache(mCtx.getCacheDir(), 1024 * 1024); // 1MB cap
            // Set up the network to use HttpURLConnection as the HTTP client.
            network = new BasicNetwork(new HurlStack());
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = new RequestQueue(cache, network, 20);
            mRequestQueue.start();
        }
        return mRequestQueue;
    }

    /**
     * @param req
     * @param <T>
     */
    public <T> void addToRequestQueue(Request<T> req) {
        req.setRetryPolicy(new DefaultRetryPolicy(
                30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        getRequestQueue().add(req);
    }

}
1

There are 1 answers

0
Mariano L On BEST ANSWER

Did a more deep debug and the problem was the server:

    this.remoteHostname = !inetAddress.isLoopbackAddress() && !inetAddress.isAnyLocalAddress()?inetAddress.getHostName().toString():"localhost";

This generate a DNS lookup that take several seconds.

There is an issue already uploaded in the library github project:

https://github.com/NanoHttpd/nanohttpd/issues/396