Repository navigation
feat: Parse protocolFallbackTTL from FDv2 goodbye #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b0b56f7
8801336
4024547
6502a0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,58 @@ | ||
| package com.launchdarkly.sdk.internal.fdv2.payloads; | ||
|
|
||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.annotations.JsonAdapter; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonToken; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import com.launchdarkly.sdk.json.SerializationException; | ||
|
|
||
| import java.io.IOException; | ||
| import java.math.BigDecimal; | ||
| import java.time.Duration; | ||
|
|
||
| /** | ||
| * Represents the goodbye event, which indicates that the server is about to disconnect. | ||
| */ | ||
| public final class Goodbye { | ||
| private final String reason; | ||
| @JsonAdapter(FallbackTTLAdapter.class) | ||
| private final Duration protocolFallbackTTL; | ||
|
|
||
| private static final class FallbackTTLAdapter extends TypeAdapter<Duration> { | ||
| @Override | ||
| public void write(JsonWriter writer, Duration value) throws IOException { | ||
| writer.value(BigDecimal.valueOf(value.toMillis(), 3)); | ||
| } | ||
|
|
||
| @Override | ||
| public Duration read(JsonReader reader) throws IOException { | ||
| if (reader.peek() == JsonToken.NUMBER) { | ||
| return Duration.ofMillis((long) (reader.nextDouble() * 1000)); | ||
| } | ||
| reader.skipValue(); | ||
| return Duration.ZERO; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new Goodbye. | ||
| * | ||
| * @param reason reason for the disconnection | ||
| */ | ||
| public Goodbye(String reason) { | ||
| this(reason, null); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new Goodbye. | ||
| * | ||
| * @param reason reason for the disconnection | ||
| * @param protocolFallbackTTL fallback TTL, or null if absent; ZERO requests the default TTL | ||
| */ | ||
| public Goodbye(String reason, Duration protocolFallbackTTL) { | ||
| this.reason = reason; | ||
| this.protocolFallbackTTL = protocolFallbackTTL; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -29,6 +63,15 @@ public Goodbye(String reason) { | |
| public String getReason() { | ||
| return reason; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the fallback TTL, or null if absent. ZERO requests the default TTL. | ||
| * | ||
| * @return the fallback TTL | ||
| */ | ||
| public Duration getProtocolFallbackTTL() { | ||
| return protocolFallbackTTL; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a Goodbye from a JsonReader. | ||
|
|
@@ -39,6 +82,7 @@ public String getReason() { | |
| */ | ||
| public static Goodbye parse(JsonReader reader) throws SerializationException { | ||
| String reason = null; | ||
| Duration protocolFallbackTTL = null; | ||
|
|
||
| try { | ||
| if (reader.peek() != JsonToken.BEGIN_OBJECT) { | ||
|
|
@@ -52,17 +96,24 @@ public static Goodbye parse(JsonReader reader) throws SerializationException { | |
| case "reason": | ||
| reason = reader.nextString(); | ||
| break; | ||
| case "protocolFallbackTTL": | ||
| if (reader.peek() == JsonToken.NULL) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's kinda strange to special-case null here, when the adapter already handles other non-number cases differently.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I special-cased JSON The spec only talks about the field's "presence", though. If we count Should I drop the special case? |
||
| protocolFallbackTTL = null; | ||
| reader.skipValue(); | ||
| } else { | ||
| protocolFallbackTTL = new FallbackTTLAdapter().read(reader); | ||
| } | ||
| break; | ||
| default: | ||
| reader.skipValue(); | ||
| break; | ||
| } | ||
| } | ||
| reader.endObject(); | ||
|
|
||
| return new Goodbye(reason); | ||
| return new Goodbye(reason, protocolFallbackTTL); | ||
| } catch (IOException | RuntimeException e) { | ||
| throw new SerializationException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, is it actually safe to treat missing or incorrect-type values as 0? Are they always treated the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A missing field isn't mapped to 0. It stays
null, which means "no directive". Only a field that is present but not a number becomesZERO.For a field that is present, the spec handles wrong-type and 0 the same way: 1.6.1.4 says a malformed TTL uses the default, and 1.6.1.5 says 0, negative, and >1h also use the default. Both get the jittered 1h default.
Still,
ZEROis a sentinel, so this only works if every consumer knows it means "default". If you'd prefer something less implicit, I see two options:LDValue), as rust-server-sdk does withOption<serde_json::Value>, and let the server SDK interpret it.(0, 1h]check here and returnnullfor absent, a validDurationfor in range, and a single documented "use default" value otherwise.Do you want me to switch to one of those?
Written by Devin