Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 6e7634d

Browse files
authored
Merge pull request #105 from scalecube/gateway-session-id-as-primitive
Make sessionId as long
2 parents 2d91fc7 + fcc83fa commit 6e7634d

File tree

8 files changed

+17
-24
lines changed

8 files changed

+17
-24
lines changed

services-gateway-netty/src/main/java/io/scalecube/services/gateway/GatewaySession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ public interface GatewaySession {
77
*
88
* @return session id
99
*/
10-
String sessionId();
10+
long sessionId();
1111
}

services-gateway-netty/src/main/java/io/scalecube/services/gateway/rsocket/RSocketGatewaySession.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import io.scalecube.services.gateway.GatewayMetrics;
1010
import io.scalecube.services.gateway.GatewaySession;
1111
import io.scalecube.services.gateway.ServiceMessageCodec;
12-
import java.util.StringJoiner;
1312
import java.util.concurrent.atomic.AtomicLong;
1413
import java.util.function.BiFunction;
1514
import reactor.core.publisher.Flux;
@@ -27,7 +26,7 @@ public final class RSocketGatewaySession extends AbstractRSocket implements Gate
2726
private final ServiceCall serviceCall;
2827
private final GatewayMetrics metrics;
2928
private final ServiceMessageCodec messageCodec;
30-
private final String sessionId;
29+
private final long sessionId;
3130
private final BiFunction<GatewaySession, ServiceMessage, ServiceMessage> messageMapper;
3231

3332
/**
@@ -46,11 +45,11 @@ public RSocketGatewaySession(
4645
this.metrics = metrics;
4746
this.messageCodec = messageCodec;
4847
this.messageMapper = messageMapper;
49-
this.sessionId = Long.toHexString(SESSION_ID_GENERATOR.incrementAndGet());
48+
this.sessionId = SESSION_ID_GENERATOR.incrementAndGet();
5049
}
5150

5251
@Override
53-
public String sessionId() {
52+
public long sessionId() {
5453
return this.sessionId;
5554
}
5655

@@ -105,8 +104,6 @@ private Payload toPayload(ServiceMessage message) {
105104

106105
@Override
107106
public String toString() {
108-
return new StringJoiner(", ", RSocketGatewaySession.class.getSimpleName() + "[", "]")
109-
.add(sessionId)
110-
.toString();
107+
return "RSocketGatewaySession[" + sessionId + ']';
111108
}
112109
}

services-gateway-netty/src/main/java/io/scalecube/services/gateway/ws/WebsocketGatewaySession.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io.scalecube.services.gateway.GatewaySessionHandler;
88
import java.util.Map;
99
import java.util.Optional;
10-
import java.util.StringJoiner;
1110
import java.util.concurrent.atomic.AtomicLong;
1211
import org.jctools.maps.NonBlockingHashMapLong;
1312
import org.slf4j.Logger;
@@ -35,7 +34,7 @@ public final class WebsocketGatewaySession implements GatewaySession {
3534
private final WebsocketOutbound outbound;
3635
private final GatewayMessageCodec codec;
3736

38-
private final String sessionId;
37+
private final long sessionId;
3938
private final String contentType;
4039

4140
/**
@@ -54,7 +53,7 @@ public WebsocketGatewaySession(
5453
WebsocketOutbound outbound,
5554
GatewaySessionHandler<GatewayMessage> gatewayHandler) {
5655
this.codec = codec;
57-
this.sessionId = Long.toHexString(SESSION_ID_GENERATOR.incrementAndGet());
56+
this.sessionId = SESSION_ID_GENERATOR.incrementAndGet();
5857

5958
String contentType = httpRequest.requestHeaders().get(HttpHeaderNames.CONTENT_TYPE);
6059
this.contentType = Optional.ofNullable(contentType).orElse(DEFAULT_CONTENT_TYPE);
@@ -65,7 +64,7 @@ public WebsocketGatewaySession(
6564
}
6665

6766
@Override
68-
public String sessionId() {
67+
public long sessionId() {
6968
return sessionId;
7069
}
7170

@@ -195,8 +194,6 @@ private void clearSubscriptions() {
195194

196195
@Override
197196
public String toString() {
198-
return new StringJoiner(", ", WebsocketGatewaySession.class.getSimpleName() + "[", "]")
199-
.add(sessionId)
200-
.toString();
197+
return "WebsocketGatewaySession[" + sessionId + ']';
201198
}
202199
}

services-gateway-tests/src/main/java/io/scalecube/services/testservice/AuthRegistry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AuthRegistry {
1313
/** Preconfigured userName-s that are allowed to be authenticated. */
1414
private final Set<String> allowedUsers;
1515

16-
private ConcurrentMap<String, String> loggedInUsers = new ConcurrentHashMap<>();
16+
private ConcurrentMap<Long, String> loggedInUsers = new ConcurrentHashMap<>();
1717

1818
public AuthRegistry(Set<String> allowedUsers) {
1919
this.allowedUsers = allowedUsers;
@@ -25,7 +25,7 @@ public AuthRegistry(Set<String> allowedUsers) {
2525
* @param sessionId session id to get auth info for
2626
* @return auth info for given session if exists
2727
*/
28-
public Optional<String> getAuth(String sessionId) {
28+
public Optional<String> getAuth(long sessionId) {
2929
return Optional.ofNullable(loggedInUsers.get(sessionId));
3030
}
3131

@@ -36,7 +36,7 @@ public Optional<String> getAuth(String sessionId) {
3636
* @param auth auth info for given session id
3737
* @return auth info added for session id or empty if auth info is invalid
3838
*/
39-
public Optional<String> addAuth(String sessionId, String auth) {
39+
public Optional<String> addAuth(long sessionId, String auth) {
4040
if (allowedUsers.contains(auth)) {
4141
loggedInUsers.putIfAbsent(sessionId, auth);
4242
return Optional.of(auth);
@@ -52,7 +52,7 @@ public Optional<String> addAuth(String sessionId, String auth) {
5252
* @param sessionId session id to be removed from registry
5353
* @return true if session had auth info, false - otherwise
5454
*/
55-
public String removeAuth(String sessionId) {
55+
public String removeAuth(long sessionId) {
5656
return loggedInUsers.remove(sessionId);
5757
}
5858
}

services-gateway-tests/src/main/java/io/scalecube/services/testservice/SecuredAuthenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Mono<String> authenticate(ServiceMessage message) {
2020
return sessionId == null
2121
? Mono.error(new BadRequestException("No session id provided in request"))
2222
: authRegistry
23-
.getAuth(sessionId)
23+
.getAuth(Long.parseLong(sessionId))
2424
.map(Mono::just)
2525
.orElse(Mono.error(new ForbiddenException("Session is not authenticated")));
2626
}

services-gateway-tests/src/main/java/io/scalecube/services/testservice/SecuredRsGwGatewaySessionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public SecuredRsGwGatewaySessionHandler(AuthRegistry authRegistry) {
1818

1919
@Override
2020
public ServiceMessage mapMessage(GatewaySession session, ServiceMessage req, Context context) {
21-
return ServiceMessage.from(req).header(AuthRegistry.SESSION_ID, session).build();
21+
return ServiceMessage.from(req).header(AuthRegistry.SESSION_ID, session.sessionId()).build();
2222
}
2323

2424
@Override

services-gateway-tests/src/main/java/io/scalecube/services/testservice/SecuredServiceImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.scalecube.services.exceptions.ForbiddenException;
77
import java.util.Optional;
88
import java.util.stream.IntStream;
9-
109
import org.slf4j.Logger;
1110
import org.slf4j.LoggerFactory;
1211
import reactor.core.publisher.Flux;
@@ -29,7 +28,7 @@ public Mono<String> createSession(ServiceMessage request) {
2928
});
3029
}
3130
String req = request.data();
32-
Optional<String> authResult = authRegistry.addAuth(sessionId, req);
31+
Optional<String> authResult = authRegistry.addAuth(Long.parseLong(sessionId), req);
3332
if (authResult.isPresent()) {
3433
return Mono.just(req);
3534
} else {

services-gateway-tests/src/main/java/io/scalecube/services/testservice/SecuredWsGwGatewaySessionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public SecuredWsGwGatewaySessionManager(AuthRegistry authRegistry) {
1818

1919
@Override
2020
public GatewayMessage mapMessage(GatewaySession session, GatewayMessage req, Context context) {
21-
return GatewayMessage.from(req).header(AuthRegistry.SESSION_ID, session).build();
21+
return GatewayMessage.from(req).header(AuthRegistry.SESSION_ID, session.sessionId()).build();
2222
}
2323

2424
@Override

0 commit comments

Comments
 (0)