Skip to content

Commit

Permalink
Enable retry of websocket connection on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh9 committed Sep 26, 2018
1 parent bd5769c commit 67f0990
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ The following system properties can be used to configure the library:
|`riko.ws.server.url`|The URL to use for websocket|`ws-api.upstox.com`|
|`riko.ws.server.port`|The port to use for websocket|`80` or `443` based on the scheme specified|
|`riko.ws.server.scheme`|The scheme to use|`https`|
|`riko.ws.reconnect`|Retry websocket connection on failure|`true`|


#### Including Riko in your project
Expand All @@ -88,13 +89,13 @@ The following system properties can be used to configure the library:
<dependency>
<groupId>com.github.rishabh9</groupId>
<artifactId>riko</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>

###### Gradle

dependencies {
implementation 'com.github.rishabh9:riko:1.0.0'
implementation 'com.github.rishabh9:riko:1.0.1'
}

#### Progaurd
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencyManagement {
// GroupId
group = 'com.github.rishabh9'
// Version
version = '1.0.0'
version = '1.0.1'
archivesBaseName = 'riko'

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.github.rishabh9.riko.upstox.common.constants.PropertyKeys.*;

public class ServiceGenerator {

private static final Logger log = LogManager.getLogger(ServiceGenerator.class);
Expand All @@ -59,9 +61,9 @@ private ServiceGenerator() {
.registerTypeAdapter(NumberString.class, new NumberStringDeserializer())
.registerTypeAdapterFactory(new AlwaysListTypeAdapterFactory())
.create();
final String readTimeout = System.getProperty("riko.read.timeout");
final String writeTimeout = System.getProperty("riko.write.timeout");
final String connectTimeout = System.getProperty("riko.connect.timeout");
final String readTimeout = System.getProperty(RIKO_READ_TIMEOUT);
final String writeTimeout = System.getProperty(RIKO_WRITE_TIMEOUT);
final String connectTimeout = System.getProperty(RIKO_CONNECT_TIMEOUT);
this.httpClient = new OkHttpClient.Builder();
if (!Strings.isNullOrEmpty(readTimeout)) {
this.httpClient.readTimeout(
Expand All @@ -76,9 +78,9 @@ private ServiceGenerator() {
Long.parseLong(writeTimeout), TimeUnit.SECONDS);
}
final HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
.scheme(System.getProperty("riko.server.scheme", "https"))
.host(System.getProperty("riko.server.url", "api.upstox.com"));
final String port = System.getProperty("riko.server.port");
.scheme(System.getProperty(RIKO_SERVER_SCHEME, RIKO_SERVER_SCHEME_DEFAULT))
.host(System.getProperty(RIKO_SERVER_URL, RIKO_SERVER_URL_DEFAULT));
final String port = System.getProperty(RIKO_SERVER_PORT);
if (!Strings.isNullOrEmpty(port)) {
urlBuilder.port(Integer.parseInt(port));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* MIT License
*
* Copyright (c) 2018 Rishabh Joshi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.rishabh9.riko.upstox.common.constants;

public class PropertyKeys {
public static final String RIKO_READ_TIMEOUT = "riko.read.timeout";
public static final int RIKO_READ_TIMEOUT_DEFAULT = 10;

public static final String RIKO_WRITE_TIMEOUT = "riko.write.timeout";
public static final int RIKO_WRITE_TIMEOUT_DEFAULT = 10;

public static final String RIKO_CONNECT_TIMEOUT = "riko.connect.timeout";
public static final int RIKO_CONNECT_TIMEOUT_DEFAULT = 10;

public static final String RIKO_SERVER_SCHEME = "riko.server.scheme";
public static final String RIKO_SERVER_SCHEME_DEFAULT = "https";

public static final String RIKO_SERVER_URL = "riko.server.url";
public static final String RIKO_SERVER_URL_DEFAULT = "api.upstox.com";

public static final String RIKO_SERVER_PORT = "riko.server.port";
public static final int RIKO_SERVER_PORT_DEFAULT = 443;

public static final String RIKO_WS_SERVER_SCHEME = "riko.ws.server.scheme";
public static final String RIKO_WS_SERVER_SCHEME_DEFAULT = "https";

public static final String RIKO_WS_SERVER_URL = "riko.ws.server.url";
public static final String RIKO_WS_SERVER_URL_DEFAULT = "ws-api.upstox.com";

public static final String RIKO_WS_SERVER_PORT = "riko.ws.server.port";
public static final int RIKO_WS_SERVER_PORT_DEFAULT = 443;

public static final String RIKO_WS_RECONNECT = "riko.ws.reconnect";
public static final String RIKO_WS_RECONNECT_DEFAULT = "true";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static com.github.rishabh9.riko.upstox.common.constants.PropertyKeys.*;

public class WebSocketService extends Service {

private static final Logger log = LogManager.getLogger(WebSocketService.class);
Expand Down Expand Up @@ -112,6 +114,7 @@ private WrappedWebSocket makeConnection(final WebsocketParameters parameters,
.readTimeout(parameters.getPythonPingTimeout() * 3, TimeUnit.SECONDS)
.writeTimeout(parameters.getPythonPingTimeout() * 3, TimeUnit.SECONDS)
.pingInterval(parameters.getPythonPingInterval(), TimeUnit.SECONDS)
.retryOnConnectionFailure(Boolean.parseBoolean(System.getProperty(RIKO_WS_RECONNECT, RIKO_WS_RECONNECT_DEFAULT)))
.build();

final Request request = prepareRequest();
Expand All @@ -126,9 +129,9 @@ private Request prepareRequest() {
log.debug("Preparing request");

final HttpUrl.Builder urlBuilder = new HttpUrl.Builder()
.scheme(System.getProperty("riko.ws.server.scheme", "https"))
.host(System.getProperty("riko.ws.server.url", "ws-api.upstox.com"));
final String port = System.getProperty("riko.ws.server.port");
.scheme(System.getProperty(RIKO_WS_SERVER_SCHEME, RIKO_WS_SERVER_SCHEME_DEFAULT))
.host(System.getProperty(RIKO_WS_SERVER_URL, RIKO_WS_SERVER_URL_DEFAULT));
final String port = System.getProperty(RIKO_WS_SERVER_PORT);
if (!Strings.isNullOrEmpty(port)) {
urlBuilder.port(Integer.parseInt(port));
}
Expand Down

0 comments on commit 67f0990

Please sign in to comment.