Skip to content

Commit

Permalink
Improve Proxy cache configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Jan 20, 2025
1 parent 0ab8716 commit 163e605
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Wave, containers provisioning service
* Copyright (c) 2023-2024, Seqera Labs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package io.seqera.wave.configuration

import java.time.Duration

import groovy.transform.CompileStatic
import groovy.transform.ToString
import io.micronaut.context.annotation.Value
import jakarta.inject.Singleton

/**
* Model {@link io.seqera.wave.proxy.ProxyCache} configuration settings
*
* @author Paolo Di Tommaso <[email protected]>
*/
@Singleton
@CompileStatic
@ToString(includeNames = true, includePackage = false)
class ProxyCacheConfig {

@Value('${wave.proxy-cache.duration:4m}')
private Duration duration

@Value('${wave.proxy-cache.max-size:10000}')
private int maxSize

@Value('${wave.proxy-cache.enabled:true}')
private boolean enabled

Duration getDuration() {
return duration
}

int getMaxSize() {
return maxSize
}

boolean getEnabled() {
return enabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ class RegistryProxyService {
}

DelegateResponse handleRequest(RoutePath route, Map<String,List<String>> headers) {
if( !cache.enabled ) {
return handleRequest0(route, headers)
}
final key = requestKey(route, headers)
if( !key ) {
log.debug "Bypass cache for requrst route=${route}; headers=${headers}"
Expand Down
22 changes: 13 additions & 9 deletions src/main/groovy/io/seqera/wave/proxy/ProxyCache.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import java.time.Duration

import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import groovy.transform.CompileStatic
import io.micronaut.context.annotation.Value
import groovy.util.logging.Slf4j
import io.micronaut.core.annotation.Nullable
import io.seqera.wave.configuration.ProxyCacheConfig
import io.seqera.wave.encoder.MoshiEncodeStrategy
import io.seqera.wave.encoder.MoshiExchange
import io.seqera.wave.store.cache.AbstractTieredCache
Expand All @@ -34,18 +35,17 @@ import jakarta.inject.Singleton
*
* @author Paolo Di Tommaso <[email protected]>
*/
@Slf4j
@Singleton
@CompileStatic
class ProxyCache extends AbstractTieredCache<DelegateResponse> {

@Value('${wave.proxy-cache.duration:30m}')
private Duration duration
private ProxyCacheConfig config

@Value('${wave.proxy-cache.max-size:10000}')
private int maxSize

ProxyCache(@Nullable L2TieredCache l2) {
ProxyCache(@Nullable L2TieredCache l2, ProxyCacheConfig config) {
super(l2, encoder())
this.config = config
log.info "+ Creating Proxy-cache - config=${config}"
}

static MoshiEncodeStrategy encoder() {
Expand All @@ -69,11 +69,15 @@ class ProxyCache extends AbstractTieredCache<DelegateResponse> {

@Override
int getMaxSize() {
return maxSize
return config.maxSize
}

Duration getDuration() {
return duration
return config.duration
}

boolean getEnabled() {
return config.enabled
}

}
3 changes: 2 additions & 1 deletion src/test/groovy/io/seqera/wave/proxy/ProxyCacheTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import spock.lang.Specification
import java.time.Duration

import io.micronaut.context.ApplicationContext
import io.seqera.wave.configuration.ProxyCacheConfig
import io.seqera.wave.store.cache.RedisL2TieredCache
import io.seqera.wave.test.RedisTestContainer
/**
Expand Down Expand Up @@ -51,7 +52,7 @@ class ProxyCacheTest extends Specification implements RedisTestContainer {
given:
def TTL = Duration.ofMillis(150)
def store = applicationContext.getBean(RedisL2TieredCache)
def cache = new ProxyCache(store)
def cache = new ProxyCache(store, Mock(ProxyCacheConfig))
and:
def k = UUID.randomUUID().toString()
def resp = new DelegateResponse(
Expand Down

0 comments on commit 163e605

Please sign in to comment.