Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Contributor

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 becomes ZERO.

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, ZERO is a sentinel, so this only works if every consumer knows it means "default". If you'd prefer something less implicit, I see two options:

  • Keep the raw value (LDValue), as rust-server-sdk does with Option<serde_json::Value>, and let the server SDK interpret it.
  • Apply the (0, 1h] check here and return null for absent, a valid Duration for in range, and a single documented "use default" value otherwise.

Do you want me to switch to one of those?

Written by Devin

}
}

/**
* 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;
}

/**
Expand All @@ -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.
Expand All @@ -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) {
Expand All @@ -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) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I special-cased JSON null to match two things. Gson's @JsonAdapter is null-safe by default, so Gson deserialization already reads null as absent. And rust-server-sdk treats null as absent, because serde reads a JSON null into Option as None.

The spec only talks about the field's "presence", though. If we count "protocolFallbackTTL": null as present, it becomes a directive with the default TTL, parse() just calls the adapter, and the special case goes away. Rust and the Gson path would then differ from this on null, but only Gson round-trip tests use the Gson path.

Should I drop the special case?

Written by Devin

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);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.launchdarkly.sdk.internal.fdv2.payloads.ServerIntent;
import com.launchdarkly.sdk.json.SerializationException;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -136,9 +137,15 @@ public String getReason() {

public static final class FDv2ActionGoodbye implements IFDv2ProtocolAction {
private final String reason;
private final Duration protocolFallbackTTL;

public FDv2ActionGoodbye(String reason) {
this(reason, null);
}

public FDv2ActionGoodbye(String reason, Duration protocolFallbackTTL) {
this.reason = reason;
this.protocolFallbackTTL = protocolFallbackTTL;
}

@Override
Expand All @@ -149,6 +156,15 @@ public FDv2ProtocolActionType getAction() {
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;
}
}

public static final class FDv2ActionInternalError implements IFDv2ProtocolAction {
Expand Down Expand Up @@ -264,7 +280,7 @@ private IFDv2ProtocolAction error(Error error) {
}

private IFDv2ProtocolAction goodbye(Goodbye intent) {
return new FDv2ActionGoodbye(intent.getReason());
return new FDv2ActionGoodbye(intent.getReason(), intent.getProtocolFallbackTTL());
}

/**
Expand Down Expand Up @@ -341,4 +357,3 @@ public void reset() {
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.Test;

import java.io.StringReader;
import java.time.Duration;
import java.util.List;

import static com.launchdarkly.sdk.internal.GsonHelpers.gsonInstance;
Expand All @@ -27,6 +28,29 @@
@SuppressWarnings("javadoc")
public class FDv2PayloadsTest extends BaseInternalTest {

@Test
public void goodbye_ParsesProtocolFallbackTTL() throws Exception {
String[] values = {"60", "1.25", "0", "-1", "3601", "\"invalid\"", "true", "{}", "[]", "null"};
Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO,
Duration.ofSeconds(-1), Duration.ofSeconds(3601), Duration.ZERO, Duration.ZERO,
Duration.ZERO, Duration.ZERO, null};
for (int i = 0; i < values.length; i++) {
String json = "{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}";
Goodbye goodbye = Goodbye.parse(new JsonReader(new StringReader(json)));
assertEquals(values[i], expected[i], goodbye.getProtocolFallbackTTL());
assertEquals("fallback", goodbye.getReason());
Goodbye reserialized = Goodbye.parse(new JsonReader(new StringReader(gsonInstance().toJson(goodbye))));
assertEquals(expected[i], reserialized.getProtocolFallbackTTL());
}
}

@Test
public void goodbye_WithoutProtocolFallbackTTL() throws Exception {
assertNull(Goodbye.parse(new JsonReader(new StringReader("{}"))).getProtocolFallbackTTL());
assertNull(new Goodbye("reason").getProtocolFallbackTTL());
assertEquals(Duration.ofSeconds(5), new Goodbye("reason", Duration.ofSeconds(5)).getProtocolFallbackTTL());
}

@Test
public void serverIntent_CanDeserializeAndReserialize() throws Exception {
String json = "{\n" +
Expand Down Expand Up @@ -874,4 +898,3 @@ public void deserializeEventsArray_CanDeserializeValidEventsArray() throws Excep
assertEquals(100, transferred.getVersion());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.junit.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -340,6 +341,24 @@ public void goodbye_ReturnsGoodbyeActionWithReason() {
assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye);
FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action;
assertEquals("Server is shutting down", goodbyeAction.getReason());
assertNull(goodbyeAction.getProtocolFallbackTTL());
assertNull(new FDv2ProtocolHandler.FDv2ActionGoodbye("reason").getProtocolFallbackTTL());
}

@Test
public void goodbye_PassesThroughProtocolFallbackTTL() {
String[] values = {"60", "1.25", "\"invalid\"", "null"};
Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO, null};
for (int i = 0; i < values.length; i++) {
JsonElement data = gsonInstance().fromJson(
"{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}", JsonElement.class);
FDv2ProtocolHandler.IFDv2ProtocolAction action = new FDv2ProtocolHandler().handleEvent(
new FDv2Event(FDv2EventTypes.GOODBYE, data));
assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye);
FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action;
assertEquals("fallback", goodbyeAction.getReason());
assertEquals(values[i], expected[i], goodbyeAction.getProtocolFallbackTTL());
}
}

// Requirement 3.3.9: Heartbeat Handling
Expand Down Expand Up @@ -1063,4 +1082,3 @@ public void reset_WithMixedOperations_ClearsAllChanges() {
assertEquals("f-new", changesetAction.getChangeset().getChanges().get(0).getKey());
}
}

Loading