Пример #1
0
  public void streamFailed(IOException e) {
    boolean noNewStreams = false;

    synchronized (connectionPool) {
      if (e instanceof StreamResetException) {
        StreamResetException streamResetException = (StreamResetException) e;
        if (streamResetException.errorCode == ErrorCode.REFUSED_STREAM) {
          refusedStreamCount++;
        }
        // On HTTP/2 stream errors, retry REFUSED_STREAM errors once on the same connection. All
        // other errors must be retried on a new connection.
        if (streamResetException.errorCode != ErrorCode.REFUSED_STREAM || refusedStreamCount > 1) {
          noNewStreams = true;
          route = null;
        }
      } else if (connection != null && !connection.isMultiplexed()) {
        noNewStreams = true;

        // If this route hasn't completed a call, avoid it for new connections.
        if (connection.successCount == 0) {
          if (route != null && e != null) {
            routeSelector.connectFailed(route, e);
          }
          route = null;
        }
      }
    }

    deallocate(noNewStreams, false, true);
  }
Пример #2
0
  /**
   * Returns a connection to host a new stream. This prefers the existing connection if it exists,
   * then the pool, finally building a new connection.
   */
  private RealConnection findConnection(
      int connectTimeout, int readTimeout, int writeTimeout, boolean connectionRetryEnabled)
      throws IOException {
    Route selectedRoute;
    synchronized (connectionPool) {
      if (released) throw new IllegalStateException("released");
      if (codec != null) throw new IllegalStateException("codec != null");
      if (canceled) throw new IOException("Canceled");

      RealConnection allocatedConnection = this.connection;
      if (allocatedConnection != null && !allocatedConnection.noNewStreams) {
        return allocatedConnection;
      }

      // Attempt to get a connection from the pool.
      RealConnection pooledConnection = Internal.instance.get(connectionPool, address, this);
      if (pooledConnection != null) {
        this.connection = pooledConnection;
        return pooledConnection;
      }

      selectedRoute = route;
    }

    if (selectedRoute == null) {
      selectedRoute = routeSelector.next();
      synchronized (connectionPool) {
        route = selectedRoute;
        refusedStreamCount = 0;
      }
    }
    RealConnection newConnection = new RealConnection(selectedRoute);
    acquire(newConnection);

    synchronized (connectionPool) {
      Internal.instance.put(connectionPool, newConnection);
      this.connection = newConnection;
      if (canceled) throw new IOException("Canceled");
    }

    newConnection.connect(
        connectTimeout,
        readTimeout,
        writeTimeout,
        address.connectionSpecs(),
        connectionRetryEnabled);
    routeDatabase().connected(newConnection.route());

    return newConnection;
  }
  private AddTripPattern getAddTripPattern(RouteSelector sel) throws Exception {
    AddTripPattern atp = new AddTripPattern();
    WKTReader wr = new WKTReader();
    atp.geometry = sel.getGeometry();

    atp.name = "Broad High Express";
    atp.stops = new BitSet();

    // everything is a stop except for (0-based) point 3, on High one block north of Broad
    // or on East Broad, depending on geometry chosen
    for (int i = 0; i < 6; i++) {
      if (i == 3) atp.stops.clear(i);
      else atp.stops.set(i);
    }

    atp.timetables = Lists.newArrayList();

    return atp;
  }
Пример #4
0
 public boolean hasMoreRoutes() {
   return route != null || routeSelector.hasNext();
 }