okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

allow configuring web socket close timeout

Open ArloL opened this issue 1 year ago • 2 comments

The default timeout of 60 seconds is too high for one of my use cases. I tried my best to match the existing API. Please feel free to give me feedback on how I can make this as good as possible to increase the likelihood of it being merged.

PS Thanks for the efforts maintaining critical infrastructure like this library.

Edit: Figured it out. My JVM was set to Java 8 SDK. I sadly could not run `./gradlew checks`. I included the errors I got.
❯ ./gradlew checks                                                  
To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.5/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
Daemon will be stopped at the end of the build 
Type-safe project accessors is an incubating feature.
Project accessors enabled, but root project name not explicitly set for 'buildSrc'. Checking out the project in different folders will impact the generated code and implicitly the buildscript classpath, breaking caching.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'okhttp-parent'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve de.mannodermaus.gradle.plugins:android-junit5:1.10.0.0.
     Required by:
         project :
      > No matching variant of de.mannodermaus.gradle.plugins:android-junit5:1.10.0.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability de.mannodermaus.gradle.plugins:android-junit5:1.10.0.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability de.mannodermaus.gradle.plugins:android-junit5:1.10.0.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
   > Could not resolve com.android.tools.build:gradle:8.2.0.
     Required by:
         project :
      > No matching variant of com.android.tools.build:gradle:8.2.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability com.android.tools.build:gradle:8.2.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:8.2.0 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability com.android.tools.build:gradle:8.2.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability com.android.tools.build:gradle:8.2.0 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
   > Could not resolve com.diffplug.spotless:spotless-plugin-gradle:6.23.3.
     Required by:
         project :
      > No matching variant of com.diffplug.spotless:spotless-plugin-gradle:6.23.3 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability com.diffplug.spotless:spotless-plugin-gradle:6.23.3 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability com.diffplug.spotless:spotless-plugin-gradle:6.23.3 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability com.diffplug.spotless:spotless-plugin-gradle:6.23.3 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability com.diffplug.spotless:spotless-plugin-gradle:6.23.3 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
   > Could not resolve com.vanniktech:gradle-maven-publish-plugin:0.27.0.
     Required by:
         project :
      > No matching variant of com.vanniktech:gradle-maven-publish-plugin:0.27.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability com.vanniktech:gradle-maven-publish-plugin:0.27.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability com.vanniktech:gradle-maven-publish-plugin:0.27.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability com.vanniktech:gradle-maven-publish-plugin:0.27.0 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 5s
4 actionable tasks: 1 executed, 3 up-to-date

ArloL avatar Jan 13 '24 10:01 ArloL

e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:257:33 Type mismatch: inferred type is Long but Int was expected
e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:615:27 Type mismatch: inferred type is Int but Long was expected
e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:1298:22 Type mismatch: inferred type is Int but Long was expected
e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:1326:24 Type mismatch: inferred type is Int but Long was expected

yschimke avatar Jan 13 '24 11:01 yschimke

e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:257:33 Type mismatch: inferred type is Long but Int was expected
e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:615:27 Type mismatch: inferred type is Int but Long was expected
e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:1298:22 Type mismatch: inferred type is Int but Long was expected
e: file:///home/runner/work/okhttp/okhttp/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt:1326:24 Type mismatch: inferred type is Int but Long was expected

Thanks. I figured out the issue with gradle. I had my JVM set to Java 8. It works with Java 21.

Now these should be gone.

ArloL avatar Jan 13 '24 13:01 ArloL

The default timeout of 60 seconds is too high for one of my use cases.

Could you explain your use case a bit more? I’d expect a well-behaved server to respond to a close frame immediately, so this 60 second limit shouldn’t be relevant in practice.

That said I’m happy with the shape of this PR!

swankjesse avatar Mar 31 '24 15:03 swankjesse

Small follow-up: https://github.com/square/okhttp/pull/8316

swankjesse avatar Mar 31 '24 15:03 swankjesse

Could you explain your use case a bit more? I’d expect a well-behaved server to respond to a close frame immediately, so this 60 second limit shouldn’t be relevant in practice.

Sure. The context is an android application in an (air-gapped) Wi-Fi network environment where the mobile devices are moving and need to inform the users if the connection has been lost. So I hit this timeout in tests where the network connection was flakey and it was not a proper close of the connection; which is where the default comes in. And for my use case setting a much lower timeout (ie 10 seconds) made sense because latency is mostly not really an issue - unless it is and that is exactly what I want to know and inform the users that the notifications might be out of date.

ArloL avatar Apr 01 '24 14:04 ArloL

Oh neat! Thanks for the explanation!

swankjesse avatar Apr 02 '24 02:04 swankjesse