Skip to content

Commit

Permalink
Merge pull request #19723 from kalaiyarasiganeshalingam/retry
Browse files Browse the repository at this point in the history
Implement retry for webSocket endpoint
  • Loading branch information
daneshk authored Feb 3, 2020
2 parents cfde288 + de7e116 commit f4b0e32
Show file tree
Hide file tree
Showing 29 changed files with 1,051 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WsConnectionClosureError",
"kind": "Event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketConnector",
"kind": "Interface",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WsConnectionClosureError",
"kind": "Event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation":{
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketConnector",
"kind": "Interface",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "221",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketConnector",
"kind": "Interface",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WsConnectionClosureError",
"kind": "Event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WsConnectionClosureError",
"kind": "Event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketConnector",
"kind": "Interface",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,17 @@
"insertText": "WebSocketClientConfiguration",
"insertTextFormat": "Snippet"
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Retry configurations for WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet"
},
{
"label": "WsConnectionClosureError",
"kind": "Event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,32 @@
}
]
},
{
"label": "WebSocketRetryConfig",
"kind": "Struct",
"detail": "Record",
"documentation": {
"left": "Configurations for reconnecting to the WebSocket.\n"
},
"sortText": "180",
"insertText": "WebSocketRetryConfig",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 0
}
},
"newText": "import ballerina/http;\n"
}
]
},
{
"label": "AuthnFilter",
"kind": "Interface",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ public type WebSocketClient client object {
# `WebSocketClient`needs to be called once to start receiving messages.
# + secureSocket - SSL/TLS related options
# + maxFrameSize - The maximum payload size of a WebSocket frame in bytes.
# If this is not set or is negative or zero the default frame size of 65536 will be used.
# + webSocketCompressionEnabled - Enable support for compression in WebSocket
# If this is not set, is negative, or is zero, the default frame size of 65536 will be used.
# + webSocketCompressionEnabled - Enable support for compression in the WebSocket.
# + handShakeTimeoutInSeconds - Time (in seconds) that a connection waits to get the response of
# the webSocket handshake. If the timeout exceeds, then the connection is terminated with
# an error.If the value < 0, then the value sets to the default value(300).
# + retryConfig - Retry related configurations.
public type WebSocketClientConfiguration record {|
service? callbackService = ();
string[] subProtocols = [];
Expand All @@ -188,6 +192,22 @@ public type WebSocketClientConfiguration record {|
ClientSecureSocket? secureSocket = ();
int maxFrameSize = 0;
boolean webSocketCompressionEnabled = true;
int handShakeTimeoutInSeconds = 300;
WebSocketRetryConfig retryConfig?;
|};

# Retry configurations for WebSocket.
#
# + maxCount - The maximum number of retry attempts. If the count is zero, the client will retry indefinitely.
# + intervalInMillis - The number of milliseconds to delay before attempting to reconnect.
# + backOffFactor - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems
# persist.
# + maxWaitIntervalInMillis - Maximum time of the retry interval in milliseconds.
public type WebSocketRetryConfig record {|
int maxCount = 0;
int intervalInMillis = 1000;
float backOffFactor = 1.0;
int maxWaitIntervalInMillis = 30000;
|};

function externWSInitEndpoint(WebSocketClient wsClient) = @java:Method {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package org.ballerinalang.net.http.websocket;


import static org.ballerinalang.jvm.util.BLangConstants.BALLERINA_PACKAGE_PREFIX;
import org.ballerinalang.jvm.types.BPackage;
import org.ballerinalang.jvm.util.BLangConstants;

/**
* Constants of WebSocket.
Expand All @@ -28,7 +28,7 @@ public class WebSocketConstants {

public static final String BALLERINA_ORG = "ballerina";
public static final String PACKAGE_HTTP = "http";
public static final String FULL_PACKAGE_HTTP = BALLERINA_PACKAGE_PREFIX + PACKAGE_HTTP;
public static final String FULL_PACKAGE_HTTP = BLangConstants.BALLERINA_PACKAGE_PREFIX + PACKAGE_HTTP;
public static final String SEPARATOR = ":";
public static final String LISTENER = "Listener";
public static final String WEBSOCKET_CONNECTOR = "WebSocketConnector";
Expand All @@ -38,9 +38,11 @@ public class WebSocketConstants {
public static final String WEBSOCKET_CLIENT_SERVICE = "WebSocketClientService";
public static final String WSS_SCHEME = "wss";
public static final String WEBSOCKET_CALLER_NAME = PACKAGE_HTTP + SEPARATOR + WEBSOCKET_CALLER;
public static final String FULL_WEBSOCKET_CALLER_NAME = BALLERINA_PACKAGE_PREFIX + WEBSOCKET_CALLER_NAME;
public static final String FULL_WEBSOCKET_CALLER_NAME = BLangConstants.BALLERINA_PACKAGE_PREFIX +
WEBSOCKET_CALLER_NAME;
public static final String WEBSOCKET_CLIENT_NAME = PACKAGE_HTTP + SEPARATOR + WEBSOCKET_CLIENT;
public static final String FULL_WEBSOCKET_CLIENT_NAME = BALLERINA_PACKAGE_PREFIX + WEBSOCKET_CLIENT_NAME;
public static final String FULL_WEBSOCKET_CLIENT_NAME = BLangConstants.BALLERINA_PACKAGE_PREFIX +
WEBSOCKET_CLIENT_NAME;


public static final String WEBSOCKET_ANNOTATION_CONFIGURATION = "WebSocketServiceConfig";
Expand All @@ -49,6 +51,7 @@ public class WebSocketConstants {
public static final String ANNOTATION_ATTR_IDLE_TIMEOUT = "idleTimeoutInSeconds";
public static final String ANNOTATION_ATTR_MAX_FRAME_SIZE = "maxFrameSize";


public static final String RESOURCE_NAME_ON_OPEN = "onOpen";
public static final String RESOURCE_NAME_ON_TEXT = "onText";
public static final String RESOURCE_NAME_ON_BINARY = "onBinary";
Expand Down Expand Up @@ -76,6 +79,13 @@ public class WebSocketConstants {
public static final String CLIENT_READY_ON_CONNECT = "readyOnConnect";
public static final String WEBSOCKET_UPGRADE_SERVICE_CONFIG = "upgradeService";

public static final String RETRY_CONFIG = "retryConfig";
public static final String COUNT_DOWN_LATCH = "countDownLatch";
public static final String CLIENT_LISTENER = "clientListener";
public static final String CLIENT_CONNECTOR = "clientConnector";
public static final String LOG_MESSAGE = "{} {}";
public static final String ERROR_MESSAGE = "Error occurred: ";

public static final String COMPRESSION_ENABLED_CONFIG = "webSocketCompressionEnabled";

// WebSocketListener field names
Expand All @@ -98,6 +108,8 @@ public class WebSocketConstants {
public static final int STATUS_CODE_FOR_NO_STATUS_CODE_PRESENT = 1005;

public static final int DEFAULT_MAX_FRAME_SIZE = 65536;
public static final BPackage PROTOCOL_HTTP_PKG_ID = new BPackage(BLangConstants.BALLERINA_BUILTIN_PKG_PREFIX,
"http");

// Warning suppression
public static final String UNCHECKED = "unchecked";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

package org.ballerinalang.net.http.websocket;

import org.ballerinalang.jvm.BallerinaValues;
import org.ballerinalang.jvm.values.ErrorValue;
import org.ballerinalang.jvm.values.MapValue;
import org.ballerinalang.net.http.HttpConstants;

import static org.ballerinalang.net.http.websocket.WebSocketConstants.ErrorCode;

Expand All @@ -39,16 +42,26 @@ public WebSocketException(String message) {
}

public WebSocketException(ErrorCode errorCode, String message) {
super(errorCode.errorCode(), WebSocketUtil.createDetailRecord(message));
super(errorCode.errorCode(), createDetailRecord(message));
this.message = message;
}

public WebSocketException(ErrorCode errorCode, String message, ErrorValue cause) {
super(errorCode.errorCode(), WebSocketUtil.createDetailRecord(message, cause));
super(errorCode.errorCode(), createDetailRecord(message, cause));
this.message = message;
}

public String detailMessage() {
return message;
}

private static MapValue<String, Object> createDetailRecord(String errMsg) {
return createDetailRecord(errMsg, null);
}

private static MapValue<String, Object> createDetailRecord(String errMsg, ErrorValue cause) {
MapValue<String, Object> detail = BallerinaValues.createRecordValue(HttpConstants.PROTOCOL_HTTP_PKG_ID,
WebSocketConstants.WEBSOCKET_ERROR_DETAILS);
return BallerinaValues.createRecord(detail, errMsg, cause);
}
}
Loading

0 comments on commit f4b0e32

Please sign in to comment.