Repository: square/okhttp
File: okhttp/src/commonJvmAndroid/kotlin/okhttp3/Dispatcher.kt (Kotlin)

Code before:
        if (runningAsyncCalls.size >= this.maxRequests) break // Max capacity.
        if (asyncCall.callsPerHost.get() >= this.maxRequestsPerHost) continue // Host max capacity.

        i.remove()
        asyncCall.callsPerHost.incrementAndGet()
        executableCalls.add(asyncCall)
        runningAsyncCalls.add(asyncCall)
      }
      isRunning = runningCallsCount() > 0
    }

    // Avoid resubmitting if we can't logically progress
    // particularly because RealCall handles a RejectedExecutionException
    // by executing on the same thread.
    if (executorService.isShutdown) {
      for (i in 0 until executableCalls.size) {
        val asyncCall = executableCalls[i]
        asyncCall.callsPerHost.decrementAndGet()

        synchronized(this) {
          runningAsyncCalls.remove(asyncCall)
        }

        asyncCall.failRejected()
      }
      idleCallback?.run()
    } else {
      for (i in 0 until executableCalls.size) {
        val asyncCall = executableCalls[i]
        asyncCall.executeOn(executorService)
      }
    }

    return isRunning
  }

  /** Used by [Call.execute] to signal it is in-flight. */
  @Synchronized
  internal fun executed(call: RealCall) = runningSyncCalls.add(call)

  /** Used by [AsyncCall.run] to signal completion. */
  internal fun finished(call: AsyncCall) {
    call.callsPerHost.decrementAndGet()
    finished(runningAsyncCalls, call)
  }

  /** Used by [Call.execute] to signal completion. */
  internal fun finished(call: RealCall) {
    finished(runningSyncCalls, call)
  }


Review comment (suggestion):
I believe this would have triggered unnecessary calls to the idle callback. For example, each time `maxRequests` is mutated, this would be called if the executor service was shut down, even if it was already idle.

Code after:
          check(runningSyncCalls.remove(finishedCall)) { "Call wasn't in-flight!" }
          becameIdle = runningSyncCalls.isEmpty()
        }

        if (finishedAsyncCall != null) {
          finishedAsyncCall.callsPerHost.decrementAndGet()
          check(runningAsyncCalls.remove(finishedAsyncCall)) { "Call wasn't in-flight!" }
          becameIdle = runningAsyncCalls.isEmpty()
        }

        val idleCallbackToRun = if (becameIdle) idleCallback else null

        if (enqueuedCall != null) {
          readyAsyncCalls.add(enqueuedCall)

          // Mutate the AsyncCall so that it shares the AtomicInteger of an existing running call to
          // the same host.
          if (!enqueuedCall.call.forWebSocket) {
            val existingCall = findExistingCallWithHost(enqueuedCall.host)
            if (existingCall != null) enqueuedCall.reuseCallsPerHostFrom(existingCall)
          }
        }

        if (executorIsShutdown) {
          return@synchronized Effects(
            callsToReject =
              readyAsyncCalls
                .toList()
                .also { readyAsyncCalls.clear() },
            idleCallbackToRun = idleCallbackToRun,
          )
        }

        val callsToExecute = mutableListOf<AsyncCall>()
        val i = readyAsyncCalls.iterator()
        while (i.hasNext()) {
          val asyncCall = i.next()

          if (runningAsyncCalls.size >= this.maxRequests) break // Max capacity.
          if (asyncCall.callsPerHost.get() >= this.maxRequestsPerHost) continue // Host max capacity.

          i.remove()

          asyncCall.callsPerHost.incrementAndGet()
          callsToExecute.add(asyncCall)
          runningAsyncCalls.add(asyncCall)
        }

        return@synchronized Effects(
          callsToExecute = callsToExecute,
          idleCallbackToRun = idleCallbackToRun,