Skip to content

Commit

Permalink
Cleanup gateway (#852)
Browse files Browse the repository at this point in the history
* Made ServiceCall and ClientTransport extend AutoCloseable.

* Updated RSocketClientTransport to properly close rsocket channels - removed  ThreadLocal map. 

* Changed approach with "gateway client transport " - compacted to have less classes as possible. 

* Added HttpGatewayClientTransport which implements ClientChannel, ClientTransport.

* Added WebsocketGatewayClientTransportwhich  implements ClientChannel, ClientTransport.

* Re-packaged gateway client classes.
  • Loading branch information
artem-v authored Oct 1, 2024
1 parent 7b6fda5 commit 17f5213
Show file tree
Hide file tree
Showing 65 changed files with 1,653 additions and 2,393 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ServiceCall {
public class ServiceCall implements AutoCloseable {

private ClientTransport transport;
private ServiceRegistry serviceRegistry;
Expand Down Expand Up @@ -400,4 +400,15 @@ private ServiceMessage throwIfError(ServiceMessage message) {
}
return message;
}

@Override
public void close() {
if (transport != null) {
try {
transport.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import io.scalecube.services.ServiceReference;

public interface ClientTransport {
public interface ClientTransport extends AutoCloseable {

/**
* Creates {@link ClientChannel} ready for communication with remote service endpoint.
* Creates {@link ClientChannel} for communication with remote service endpoint.
*
* @param serviceReference target serviceReference
* @return {@code ClientChannel} instance
Expand Down
6 changes: 4 additions & 2 deletions services-gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -14,7 +16,7 @@
<dependencies>
<dependency>
<groupId>io.scalecube</groupId>
<artifactId>scalecube-services</artifactId>
<artifactId>scalecube-services-api</artifactId>
<version>${project.parent.version}</version>
</dependency>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.scalecube.services.gateway.client;

import io.netty.buffer.ByteBuf;
import io.scalecube.services.api.ServiceMessage;
import io.scalecube.services.exceptions.MessageCodecException;
import java.lang.reflect.Type;

public interface GatewayClientCodec {

/**
* Data decoder function.
*
* @param message client message.
* @param dataType data type class.
* @return client message object.
* @throws MessageCodecException in case if data decoding fails.
*/
default ServiceMessage decodeData(ServiceMessage message, Type dataType)
throws MessageCodecException {
return ServiceMessageCodec.decodeData(message, dataType);
}

/**
* Encodes {@link ServiceMessage}.
*
* @param message message to encode
* @return encoded message
*/
ByteBuf encode(ServiceMessage message);

/**
* Decodes {@link ServiceMessage} object from {@link ByteBuf}.
*
* @param byteBuf message to decode
* @return decoded message represented by {@link ServiceMessage}
*/
ServiceMessage decode(ByteBuf byteBuf);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.scalecube.services.gateway.transport;
package io.scalecube.services.gateway.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.scalecube.services.gateway.transport;
package io.scalecube.services.gateway.client;

import io.scalecube.services.Address;
import io.scalecube.services.ServiceEndpoint;
Expand All @@ -12,18 +12,21 @@
import java.util.Optional;
import java.util.UUID;

/** Syntethic router for returning preconstructed static service reference with given address. */
public class StaticAddressRouter implements Router {
/**
* Syntethic router for returning pre-constructed {@link ServiceReference} instance with given
* address.
*/
public final class StaticAddressRouter implements Router {

private final ServiceReference staticServiceReference;
private final ServiceReference serviceReference;

/**
* Constructor.
*
* @param address address
*/
public StaticAddressRouter(Address address) {
this.staticServiceReference =
serviceReference =
new ServiceReference(
new ServiceMethodDefinition(UUID.randomUUID().toString()),
new ServiceRegistration(
Expand All @@ -33,6 +36,6 @@ public StaticAddressRouter(Address address) {

@Override
public Optional<ServiceReference> route(ServiceRegistry serviceRegistry, ServiceMessage request) {
return Optional.of(staticServiceReference);
return Optional.of(serviceReference);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package io.scalecube.services.gateway.transport.http;
package io.scalecube.services.gateway.client.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufOutputStream;
import io.scalecube.services.api.ServiceMessage;
import io.scalecube.services.exceptions.MessageCodecException;
import io.scalecube.services.gateway.ReferenceCountUtil;
import io.scalecube.services.gateway.transport.GatewayClientCodec;
import io.scalecube.services.gateway.client.GatewayClientCodec;
import io.scalecube.services.transport.api.DataCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class HttpGatewayClientCodec implements GatewayClientCodec<ByteBuf> {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpGatewayClientCodec.class);
public final class HttpGatewayClientCodec implements GatewayClientCodec {

private final DataCodec dataCodec;

Expand All @@ -38,7 +34,6 @@ public ByteBuf encode(ServiceMessage message) {
dataCodec.encode(new ByteBufOutputStream(content), message.data());
} catch (Throwable t) {
ReferenceCountUtil.safestRelease(content);
LOGGER.error("Failed to encode data on: {}, cause: {}", message, t);
throw new MessageCodecException(
"Failed to encode data on message q=" + message.qualifier(), t);
}
Expand Down
Loading

0 comments on commit 17f5213

Please sign in to comment.