Skip to content

Commit 23a9554

Browse files
committed
Create internal package, delete call count logging
1 parent 1cb22ec commit 23a9554

File tree

7 files changed

+52
-68
lines changed

7 files changed

+52
-68
lines changed

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Api.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ package com.gabrielfeo.gradle.enterprise.api
44

55
import com.gabrielfeo.gradle.enterprise.api.auth.HttpBearerAuth
66
import com.gabrielfeo.gradle.enterprise.api.infrastructure.Serializer
7+
import com.gabrielfeo.gradle.enterprise.api.internal.*
8+
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheEnforcingInterceptor
9+
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheHitLoggingInterceptor
10+
import com.gabrielfeo.gradle.enterprise.api.internal.caching.cache
711
import okhttp3.OkHttpClient
812
import retrofit2.Retrofit
913
import retrofit2.converter.moshi.MoshiConverterFactory
1014
import retrofit2.converter.scalars.ScalarsConverterFactory
1115
import retrofit2.create
12-
import kotlin.time.Duration.Companion.seconds
1316

1417
var baseUrl: () -> String = { requireBaseUrl() }
1518
var accessToken: () -> String = { requireToken() }
1619

1720
var maxConcurrentRequests = 30
1821
var maxCacheSize = 500_000_000L
22+
var debugLoggingEnabled = System.getenv("GRADLE_ENTERPRISE_API_DEBUG_LOGGING").toBoolean()
1923

2024
val cacheablePaths: MutableList<Regex> = mutableListOf(
2125
""".*/api/builds/[\d\w]+/(?:gradle|maven)-attributes""".toRegex(),
@@ -31,7 +35,6 @@ val okHttpClient: OkHttpClient by lazy {
3135
.apply {
3236
dispatcher.maxRequests = maxConcurrentRequests
3337
dispatcher.maxRequestsPerHost = maxConcurrentRequests
34-
startRequestCountLogging(this, period = 2.seconds)
3538
}
3639
}
3740

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/DebugLogging.kt

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Authentication.kt renamed to src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Authentication.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.gabrielfeo.gradle.enterprise.api
1+
package com.gabrielfeo.gradle.enterprise.api.internal
22

33
private const val DEFAULT_KEY_NAME = "gradle-enterprise-api-token"
44
private const val DEFAULT_VAR_NAME = "GRADLE_ENTERPRISE_API_TOKEN"

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/BaseUrl.kt renamed to src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/BaseUrl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.gabrielfeo.gradle.enterprise.api
1+
package com.gabrielfeo.gradle.enterprise.api.internal
22

33
private const val DEFAULT_VAR_NAME = "GRADLE_ENTERPRISE_URL"
44

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.gabrielfeo.gradle.enterprise.api.internal.caching
2+
3+
import com.gabrielfeo.gradle.enterprise.api.baseUrl
4+
import com.gabrielfeo.gradle.enterprise.api.maxCacheSize
5+
import okhttp3.Cache
6+
import okhttp3.HttpUrl.Companion.toHttpUrl
7+
import java.io.File
8+
9+
internal val cache: Cache = run {
10+
val host = baseUrl().toHttpUrl().host
11+
val tempDir = System.getProperty("java.io.tmpdir")
12+
Cache(
13+
directory = File(tempDir, "gradle-enterprise-api-cache-$host"),
14+
maxSize = maxCacheSize,
15+
)
16+
}

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Caching.kt renamed to src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/caching/CacheEnforcingInterceptor.kt

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
package com.gabrielfeo.gradle.enterprise.api
1+
package com.gabrielfeo.gradle.enterprise.api.internal.caching
22

3-
import okhttp3.*
4-
import okhttp3.HttpUrl.Companion.toHttpUrl
5-
import java.io.File
3+
import okhttp3.Interceptor
4+
import okhttp3.Request
5+
import okhttp3.Response
66
import kotlin.time.Duration.Companion.days
77

8-
internal val cache: Cache = run {
9-
val host = baseUrl().toHttpUrl().host
10-
val tempDir = System.getProperty("java.io.tmpdir")
11-
Cache(
12-
directory = File(tempDir, "gradle-enterprise-api-cache-$host"),
13-
maxSize = maxCacheSize,
14-
)
15-
}
16-
178
internal class CacheEnforcingInterceptor(
189
private val cacheablePaths: List<Regex>,
1910
) : Interceptor {
@@ -36,4 +27,4 @@ internal class CacheEnforcingInterceptor(
3627

3728
private fun isCacheable(request: Request) =
3829
cacheablePaths.any { it.matches(request.url.toString()) }
39-
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.gabrielfeo.gradle.enterprise.api.internal.caching
2+
3+
import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
4+
import okhttp3.Interceptor
5+
import okhttp3.Response
6+
import java.util.logging.Level
7+
import java.util.logging.Logger
8+
9+
internal class CacheHitLoggingInterceptor(
10+
private val logger: Logger = Logger.getGlobal(),
11+
) : Interceptor {
12+
13+
override fun intercept(chain: Interceptor.Chain): Response {
14+
if (!debugLoggingEnabled) {
15+
return chain.proceed(chain.request())
16+
}
17+
val url = chain.request().url
18+
val response = chain.proceed(chain.request())
19+
val wasHit = with(response) { cacheResponse != null && networkResponse == null }
20+
val hitOrMiss = if (wasHit) "hit" else "miss"
21+
logger.log(Level.INFO, "Cache $hitOrMiss: $url")
22+
return response
23+
}
24+
}

0 commit comments

Comments
 (0)