title: Managing Auth Token using RxJava
body: <p>I've been wondering what's the best way to tackle the issue of token refresh. 
  I'm connecting to an API which supplies me with a auth-token, if sometime time during the calls i get a INVALID_AUTH i need to re-authenticate. 
  So for the naive implementation i did this</p>
  
  <pre><code>@SuppressWarnings("unchecked")
  @Override
  public Observable&lt;User&gt; getUsers() {
      return runCommandAndrefreshAuthIfNecessary(new RequestCommand() {
          @Override
          public Observable create() {
              return createService(UsersApi.class).getUsers();
          }
      });
  }
  private Observable runCommandAndrefreshAuthIfNecessary(final RequestCommand command) {
      return command.create()
            .onErrorResumeNext(new Func1&lt;Throwable, Observable&lt;?&gt;&gt;() {
          @Override
          public Observable&lt;?&gt; call(Throwable throwable) {
              return handleRefreshToken(command);
          }
      });
  }
  private Observable&lt;?&gt; handleRefreshToken(final RequestCommand command) {
      return refreshToken().flatMap(new Func1&lt;Boolean, Observable&lt;?&gt;&gt;() {
          @Override
          public Observable&lt;?&gt; call(Boolean aBoolean) {
              return command.create();
          }
      });
  }
  </code></pre>
  
  <p>As you can see i'm just wrapping the retrofit command, if i get an error i run refreshToken(), the token refreshes and i run the retrofit command again, so finally the Observable is passed back to the subscriber. Works as expected.
  The thing i'm struggling with, is what happens i a multiple calls are made, for example, i'm calling getUsers and getFlags one after another. both of them get the INVALID_AUTH, currently both of the fire refreshToken(), which is bad.
  i'm looking for a rx-java way to manage the calls, meaning after the first call of getUsers fires refreshToken, any call after that needs to wait for the refreshToken to end, only then fire the retrofit command.</p>
  
  <p>Any suggestion will be appreciated.</p>