-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement caching for proxy responses (#778)
Signed-off-by: Paolo Di Tommaso <[email protected]>
- Loading branch information
1 parent
67f91c8
commit 00b6add
Showing
16 changed files
with
417 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,32 +16,22 @@ | |
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.seqera.wave.tower.client | ||
package io.seqera.wave.proxy | ||
|
||
import spock.lang.Specification | ||
import groovy.transform.ToString | ||
import io.seqera.wave.encoder.MoshiExchange | ||
|
||
/** | ||
* | ||
* Model a response object to be forwarded to the client | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
class TowerClientTest extends Specification { | ||
|
||
def 'should create consistent hash' () { | ||
given: | ||
def client = new TowerClient() | ||
|
||
expect: | ||
client.makeKey('a') == '92cf27ac76c18d8e' | ||
and: | ||
client.makeKey('a') == client.makeKey('a') | ||
and: | ||
client.makeKey('a','b','c') == client.makeKey('a','b','c') | ||
and: | ||
client.makeKey('a','b',null) == client.makeKey('a','b',null) | ||
and: | ||
client.makeKey(new URI('http://foo.com')) == client.makeKey('http://foo.com') | ||
and: | ||
client.makeKey(100l) == client.makeKey('100') | ||
} | ||
|
||
@ToString(includeNames = true, includePackage = false) | ||
class DelegateResponse implements MoshiExchange { | ||
int statusCode | ||
Map<String,List<String>> headers | ||
byte[] body | ||
String location | ||
boolean isRedirect() { location } | ||
boolean isCacheable() { location!=null || (body!=null && statusCode>=200 && statusCode<400) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.proxy | ||
|
||
import java.time.Duration | ||
|
||
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory | ||
import groovy.transform.CompileStatic | ||
import io.micronaut.context.annotation.Value | ||
import io.micronaut.core.annotation.Nullable | ||
import io.seqera.wave.encoder.MoshiEncodeStrategy | ||
import io.seqera.wave.encoder.MoshiExchange | ||
import io.seqera.wave.store.cache.AbstractTieredCache | ||
import io.seqera.wave.store.cache.L2TieredCache | ||
import jakarta.inject.Singleton | ||
/** | ||
* Implements a tiered cache for proxied http responses | ||
* | ||
* @author Paolo Di Tommaso <[email protected]> | ||
*/ | ||
@Singleton | ||
@CompileStatic | ||
class ProxyCache extends AbstractTieredCache<DelegateResponse> { | ||
ProxyCache(@Nullable L2TieredCache l2, | ||
@Value('${wave.proxy-cache.duration:1h}') Duration duration, | ||
@Value('${wave.proxy-cache.max-size:10000}') long maxSize) { | ||
super(l2, encoder(), duration, maxSize) | ||
} | ||
|
||
static MoshiEncodeStrategy encoder() { | ||
// json adapter factory | ||
final factory = PolymorphicJsonAdapterFactory.of(MoshiExchange.class, "@type") | ||
.withSubtype(Entry.class, Entry.name) | ||
.withSubtype(DelegateResponse.class, DelegateResponse.simpleName) | ||
// the encoding strategy | ||
return new MoshiEncodeStrategy<AbstractTieredCache.Entry>(factory) {} | ||
} | ||
|
||
String getName() { | ||
'proxy-cache' | ||
} | ||
|
||
String getPrefix() { | ||
'proxy-cache/v1' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.