-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reuse concurrency limiter state when reloading host channels (#2413)
reuse concurrency limiter state when reloading host channels
- Loading branch information
Showing
7 changed files
with
295 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: reuse concurrency limiter state when reloading host channels | ||
links: | ||
- https://github.com/palantir/dialogue/pull/2413 |
51 changes: 51 additions & 0 deletions
51
dialogue-core/src/main/java/com/palantir/dialogue/core/ChannelState.java
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,51 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.dialogue.core; | ||
|
||
import com.palantir.logsafe.Preconditions; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
final class ChannelState { | ||
static final class Key<T> { | ||
private final Class<T> valueClass; | ||
private final Supplier<T> factory; | ||
|
||
private T cast(final Object value) { | ||
return valueClass.cast(value); | ||
} | ||
|
||
private Supplier<T> getFactory() { | ||
return factory; | ||
} | ||
|
||
Key(final Class<T> valueClass, Supplier<T> factory) { | ||
this.valueClass = valueClass; | ||
this.factory = factory; | ||
} | ||
} | ||
|
||
@SuppressWarnings("DangerousIdentityKey") | ||
private final Map<Key<?>, Object> state = new HashMap<>(); | ||
|
||
<T> T getState(Key<T> key) { | ||
return key.cast(Preconditions.checkNotNull( | ||
state.computeIfAbsent(key, keyValue -> keyValue.getFactory().get()), | ||
"state factory cannot produce a null value")); | ||
} | ||
} |
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
103 changes: 103 additions & 0 deletions
103
dialogue-core/src/test/java/com/palantir/dialogue/core/ChannelStateTest.java
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,103 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.dialogue.core; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.immutables.value.Value; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ChannelStateTest { | ||
private static final String STRING_VALUE = "hello"; | ||
private static final OuterType COMPLEX_VALUE = ImmutableOuterType.builder() | ||
.putValues( | ||
"foo", | ||
ImmutableInnerType.builder() | ||
.foo("hello") | ||
.addBar(1) | ||
.addBar(2) | ||
.addBar(3) | ||
.build()) | ||
.putValues( | ||
"bar", | ||
ImmutableInnerType.builder() | ||
.foo("world") | ||
.addBar(4) | ||
.addBar(5) | ||
.addBar(6) | ||
.build()) | ||
.build(); | ||
|
||
private static String createStringValue() { | ||
return STRING_VALUE; | ||
} | ||
|
||
private static OuterType createComplexValue() { | ||
return COMPLEX_VALUE; | ||
} | ||
|
||
@Test | ||
public void invokes_factory_when_retrieving_state() { | ||
ChannelState state = new ChannelState(); | ||
ChannelState.Key<String> key = new ChannelState.Key<>(String.class, ChannelStateTest::createStringValue); | ||
assertThat(state.getState(key)).isEqualTo(STRING_VALUE); | ||
} | ||
|
||
@Test | ||
public void can_store_state_for_multiple_key_types() { | ||
ChannelState state = new ChannelState(); | ||
ChannelState.Key<String> key1 = new ChannelState.Key<>(String.class, ChannelStateTest::createStringValue); | ||
ChannelState.Key<OuterType> key2 = | ||
new ChannelState.Key<>(OuterType.class, ChannelStateTest::createComplexValue); | ||
|
||
assertThat(state.getState(key1)).isEqualTo(STRING_VALUE); | ||
assertThat(state.getState(key2)).isEqualTo(COMPLEX_VALUE); | ||
} | ||
|
||
@Test | ||
public void retrieves_existing_state_without_invoking_factory() { | ||
ChannelState state = new ChannelState(); | ||
ChannelState.Key<UUID> key = new ChannelState.Key<>(UUID.class, UUID::randomUUID); | ||
|
||
UUID stored = state.getState(key); | ||
assertThat(state.getState(key)).isEqualTo(stored); | ||
} | ||
|
||
@Test | ||
public void throws_when_factory_produces_null_value() { | ||
ChannelState state = new ChannelState(); | ||
ChannelState.Key<String> key = new ChannelState.Key<>(String.class, () -> null); | ||
|
||
assertThatThrownBy(() -> state.getState(key)).isInstanceOf(Exception.class); | ||
} | ||
|
||
@Value.Immutable | ||
interface InnerType { | ||
String foo(); | ||
|
||
List<Integer> bar(); | ||
} | ||
|
||
@Value.Immutable | ||
interface OuterType { | ||
Map<String, InnerType> values(); | ||
} | ||
} |
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.