Skip to content

Instantly share code, notes, and snippets.

@guilhermekrz
Last active June 11, 2020 12:47
Show Gist options
  • Save guilhermekrz/b3a2dfbd62fb84bba19973b04a71638a to your computer and use it in GitHub Desktop.
Save guilhermekrz/b3a2dfbd62fb84bba19973b04a71638a to your computer and use it in GitHub Desktop.
class MoreThanOneOkHttpClientDetector : Detector(), SourceCodeScanner {
private fun reportUsage(context: JavaContext, location: Location) {
context.report(
issue = ISSUE,
location = location,
message = "You should only create one OkHttpClient instance"
)
}
companion object {
private val IMPLEMENTATION = Implementation(
MoreThanOneOkHttpClientDetector::class.java,
Scope.JAVA_FILE_SCOPE
)
val ISSUE: Issue = Issue.create(
id = "MoreThanOneOkHttpClientDetector",
briefDescription = "You should only create one OkHttpClient instance",
explanation = """
According to the official docs,
"OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls.
This is because each client holds its own connection pool and thread pools.
Reusing connections and threads reduces latency and saves memory.
Conversely, creating a client for each request wastes resources on idle pools."
More details at https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/
""".trimIndent(),
category = Category.CORRECTNESS,
priority = 5,
severity = Severity.WARNING,
androidSpecific = true,
implementation = IMPLEMENTATION
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment