title: Retrofit + Rxjava takes long time to invoke onError when server connection failed
body: <p>
  I'm using Retrofit + Rxjava to get a list of entities from server, by my design when the task fails, first it checks Internet connection and after that it checks the connection to the server in <code>doOnError</code> method of <code>Observable</code>.</p>
  
  <p>When Client is not connected to the Internet <code>doOnError</code> is invoked in a reasonable time and user get the error message but the problem is when Internet is connected and I get wrong port or domain (to check the server problem error) It takes a long time (about 1 min or longer) and it's really annoying.
  How can I reduce this time and what's the reason?</p>
  
  <h3>how I check Internet and Server connection :</h3>
  
  <pre class="lang-java prettyprint-override"><code>public static boolean checkConnection(String ipOrUrl, int port) {
      try {
          int timeoutMs = 100;
          Socket socket = new Socket();
          SocketAddress soketAddress = new InetSocketAddress(ipOrUrl, port);
          socket.connect(soketAddress, timeoutMs);
          socket.close();
          return true;
      } catch (IOException e) {
          e.printStackTrace();
          return false;
      }
  }
  </code></pre>
  
  <h3>how I try to get list of entities :</h3>
  
  <pre class="lang-java prettyprint-override"><code>    foodgetRetorfitService.getRestaurants()
              .subscribeOn(Schedulers.newThread())
              .observeOn(AndroidSchedulers.mainThread())
  //here I'm checking the internet and server connection in the onlineTask if retrofit fail
  //and if clients get Online this ( getRestaurantFromServer ) is called again.
              .doOnError(error -&gt; {
                  error.printStackTrace();
                  NetworkUtils.doOnlineTask(new OnlineTask() {
                      public void doWhenOnline() {
                          getResturantsFromServer();
                      }
                  }, true);
              })
              .subscribe(new Observer&lt;List&lt;Restaurant&gt;&gt;() {
                  @Override
                  public void onNext(List&lt;Restaurant&gt; restaurants) {
                      restaurantItemAdapter.updateAdapterData(restaurants);
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      Log.e(TAG, "onError: rxjava and retrofit error : can't get restaurant list");
                      e.printStackTrace();
                  }
              });
  </code></pre>
  
  <h3>how <code>doOnlineTask</code> is Implemented</h3>
  
  <pre class="lang-java prettyprint-override"><code>    public static void doOnlineTask(OnlineTask onlineTask, boolean autoRetry, int retryTimeout) {
          NetworkUtils.isOnline(autoRetry)
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .doOnError(error -&gt; {
  //here I check the Exception type ( I raised them when checking the connection )
                      if (error instanceof InternetConnectionException)
                          onlineTask.doWhenInternetFaild();
  
                      if (error instanceof ServerConnectionException)
                          onlineTask.doWhenServerFaild();
                  })
                  .retryWhen(t -&gt; t.delay(2, TimeUnit.SECONDS))
                  .subscribe(result -&gt; {
                              if (result.equals(NetworkStatus.CONNECTED))
                                  onlineTask.doWhenOnline();
                              else {
                                  if (result.equals(NetworkStatus.INTERNET_FAILD))
                                      onlineTask.doWhenInternetFaild();
                                  else if (result.equals(NetworkStatus.SERVER_FAILD))
                                      onlineTask.doWhenInternetFaild();
                              }
                          }, error -&gt; error.printStackTrace()
                  );
      }
  </code></pre>
  
  <h3><code>onlineTask</code> is just an abstract class</h3>
  
  <pre class="lang-java prettyprint-override"><code>abstract public void doWhenOnline();
  
  public void doWhenInternetFaild() {
     //implemented somehow
  }
  
  
  public void doWhenServerFaild() {
     //implemented somehow
  }
  </code></pre>
  
  <h3>What I tried :</h3>
  
  <p>I guessed it's timeout problem so I changed Retrofit timeout with <code>OkHttpClient</code> and It does not worked. also I changed timeouts set by myself and I reduced them. not working.</p>