Skip to content

Commit

Permalink
refactor: de-lombok EndpointStats
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Nov 20, 2024
1 parent b7dc216 commit 0a1306c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
18 changes: 12 additions & 6 deletions carapace-server/src/main/java/org/carapaceproxy/EndpointStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,32 @@

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import lombok.Getter;
import lombok.ToString;
import org.carapaceproxy.core.EndpointKey;

/**
* Stats about an endpoint
*
* @author enrico.olivelli
*/
@ToString
public class EndpointStats {

private final EndpointKey key;
@Getter
private final AtomicInteger totalRequests = new AtomicInteger();
@Getter
private final AtomicLong lastActivity = new AtomicLong();

public EndpointStats(EndpointKey key) {
public EndpointStats(final EndpointKey key) {
this.key = key;
}

public String toString() {
return "EndpointStats(key=" + this.key + ", totalRequests=" + this.totalRequests + ", lastActivity=" + this.lastActivity + ")";
}

public AtomicInteger getTotalRequests() {
return this.totalRequests;
}

public AtomicLong getLastActivity() {
return this.lastActivity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,41 @@
*/
package org.carapaceproxy.core;

import org.carapaceproxy.utils.StringUtils;

/**
* Identifier of an endpoint
*
* @author enrico.olivelli
*/
public record EndpointKey(String host, int port) {

/**
* The minimum port value according to <a href="https://tools.ietf.org/html/rfc6335">RFC 6335</a>.
*/
public static final int MIN_PORT = 0;
/**
* The maximum port value according to <a href="https://tools.ietf.org/html/rfc6335">RFC 6335</a>.
*/
public static final int MAX_PORT = 0xffff;

public EndpointKey {
if (StringUtils.isBlank(host)) {
throw new IllegalArgumentException("Hostname cannot be blank");
}
if (port > MAX_PORT || port < MIN_PORT) {
throw new IllegalArgumentException("Invalid port: " + port);
}
}

public static EndpointKey make(String host, int port) {
return new EndpointKey(host, port);
}

public static EndpointKey make(String hostAndPort) {
int pos = hostAndPort.indexOf(':');
if (pos <= 0) {
return new EndpointKey(hostAndPort, 0);
return new EndpointKey(hostAndPort, MIN_PORT);
}
String host = hostAndPort.substring(0, pos);
int port = Integer.parseInt(hostAndPort.substring(pos + 1));
Expand Down

0 comments on commit 0a1306c

Please sign in to comment.