centrifugal / centrifugal/centrifuge-java

Builder for Options

Open
#60 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
76
Forks
38
Avg merge
4h 18m
Merged PRs (30d)
10

Description

One more thing to improve is using builder pattern for options:

Code

```java
package io.github.centrifugal.centrifuge;

import java.net.Proxy;
import java.util.Map;

/**
* Configuration for a {@link Client} instance.
*/
public class Options {
private final String token;
private final ConnectionTokenGetter tokenGetter;
private final String name;
private final String version;
private final byte[] data;
private final Map headers;
private final int timeout;
private final int minReconnectDelay;
private final int maxReconnectDelay;
private final int maxServerPingDelay;
private final Proxy proxy;
private final String proxyLogin;
private final String proxyPassword;
private final Dns dns;

public String getToken() {
return token;
}

public ConnectionTokenGetter getTokenGetter() {
return tokenGetter;
}

public String getName() {
return name;
}

public String getVersion() {
return version;
}

public byte[] getData() {
return data;
}

public Map getHeaders() {
return headers;
}

public int getTimeout() {
return timeout;
}

public int getMinReconnectDelay() {
return minReconnectDelay;
}

public int getMaxReconnectDelay() {
return maxReconnectDelay;
}

public int getMaxServerPingDelay() {
return maxServerPingDelay;
}

public Proxy getProxy() {
return proxy;
}

public String getProxyLogin() {
return proxyLogin;
}

public String getProxyPassword() {
return proxyPassword;
}

public Dns getDns() {
return this.dns;
}

private Options(Builder builder) {
this.token = builder.token;
this.tokenGetter = builder.tokenGetter;
this.name = builder.name;
this.version = builder.version;
this.data = builder.data;
this.headers = builder.headers;
this.timeout = builder.timeout;
this.minReconnectDelay = builder.minReconnectDelay;
this.maxReconnectDelay = builder.maxReconnectDelay;
this.maxServerPingDelay = builder.maxServerPingDelay;
this.proxy = builder.proxy;
this.proxyLogin = builder.proxyLogin;
this.proxyPassword = builder.proxyPassword;
this.dns = builder.dns;
}

public static class Builder {
private String token = "";
private ConnectionTokenGetter tokenGetter;
private String name = "java";
private String version = "";
private byte[] data;
private Map headers;
private int timeout = 5000;
private int minReconnectDelay = 500;
private int maxReconnectDelay = 20000;
private int maxServerPingDelay = 10000;
private Proxy proxy;
private String proxyLogin;
private String proxyPassword;
private Dns dns;

/**
* Set connection token. This is a token you have to receive from your application backend.
* If your tokens expire and you want SDK to automatically refresh tokens then set
* ConnectionTokenGetter (see below).
*/
public Builder setToken(String token) {
this.token = token;
return this;
}

/**
* Set a method to extract new connection token upon expiration.
*/
public Builder setTokenGetter(ConnectionTokenGetter tokenGetter) {
this.tokenGetter = tokenGetter;
return this;
}

/**
* Set client name - name of this client. This should not be unique per client – it
* identifies client application name actually, so name should have a limited
* number of possible values. By default this client uses "java" as a name.
*/
public Builder setName(String name) {
this.name = name;
return this;
}

/**
* Set client version - version of application. This may be used for observability
* on the server (for example in analytics).
*/
public Builder setVersion(String version) {
this.version = version;
return this;
}

/**
* Set custom connection data. This data will be delivered to server in Connect command.
* For Centrifugo this may be useful in case of using connect proxy.
*/
public Builder setData(byte[] data) {
this.data = data;
return this;
}

/**
* Set custom headers for WebSocket Upgrade request.
*/
public Builder setHeaders(Map headers) {
this.headers = headers;
return this;
}

/**
* Set custom timeout for requests in milliseconds. By default, 5000 is used.
*/
public Builder setTimeout(int timeout) {
this.timeout = timeout;
return this;
}

/**
* Set minimal time before reconnect attempt in milliseconds. By default, 500 is used.
*/
public Builder setMinReconnectDelay(int minReconnectDelay) {
this.minReconnectDelay = minReconnectDelay;
return this;
}

/**
* Set max time between reconnect attempts in milliseconds. By default, 20000 is used.
*/
public Builder setMaxReconnectDelay(int maxReconnectDelay) {
this.maxReconnectDelay = maxReconnectDelay;
return this;
}

/**
* Set max time of ping delay from server in milliseconds. By default, 10000 is used.
*/
public Builder setMaxServerPingDelay(int maxServerPingDelay) {
this.maxServerPingDelay = maxServerPingDelay;
return this;
}

/**
* Set proxy to use.
*/
public Builder setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}

/**
* Set proxy credentials.
*/
public Builder setProxyCredentials(String login, String password) {
this.proxyLogin = login;
this.proxyPassword = password;
return this;
}

/**
* Set custom DNS resolver.
*/
public Builder setDns(Dns dns) {
this.dns = dns;
return this;
}

public Options build() {
return new Options(this);
}
}
}

```

Possibly we could have both current (for compatibility) and builder approach. Though the main benefit of Builder is immutability - combining both approaches makes the benefit less obvious (as we could just return `this` from current setters).

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by locating the Options class and its current construction and setter API, then compare it with the proposed Builder shown in the issue. Clarify whether the existing API must remain compatible; the work is done when the agreed options-construction design is implemented and its compatibility expectations are covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.