-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New gateway 'service' - part 1 #663
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,24 @@ | |
import ai.langstream.api.gateway.GatewayRequestContext; | ||
import ai.langstream.api.model.Gateway; | ||
import ai.langstream.api.runner.code.Header; | ||
import ai.langstream.api.runner.code.Record; | ||
import ai.langstream.apigateway.api.ProduceRequest; | ||
import ai.langstream.apigateway.api.ProduceResponse; | ||
import ai.langstream.apigateway.gateways.ConsumeGateway; | ||
import ai.langstream.apigateway.gateways.GatewayRequestHandler; | ||
import ai.langstream.apigateway.gateways.ProduceGateway; | ||
import ai.langstream.apigateway.runner.TopicConnectionsRuntimeProviderBean; | ||
import ai.langstream.apigateway.websocket.AuthenticatedGatewayRequestContext; | ||
import ai.langstream.apigateway.websocket.api.ProduceRequest; | ||
import ai.langstream.apigateway.websocket.api.ProduceResponse; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.validation.constraints.NotBlank; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
@@ -49,6 +57,7 @@ public class GatewayResource { | |
|
||
private final TopicConnectionsRuntimeProviderBean topicConnectionsRuntimeRegistryProvider; | ||
private final GatewayRequestHandler gatewayRequestHandler; | ||
private final ExecutorService consumeThreadPool = Executors.newCachedThreadPool(); | ||
|
||
@PostMapping( | ||
value = "/produce/{tenant}/{application}/{gateway}", | ||
|
@@ -61,12 +70,8 @@ ProduceResponse produce( | |
@RequestBody ProduceRequest produceRequest) | ||
throws ProduceGateway.ProduceException { | ||
|
||
final Map<String, String> queryString = | ||
request.getParameterMap().keySet().stream() | ||
.collect(Collectors.toMap(k -> k, k -> request.getParameter(k))); | ||
final Map<String, String> headers = new HashMap<>(); | ||
request.getHeaderNames() | ||
.forEachRemaining(name -> headers.put(name, request.getHeader(name))); | ||
final Map<String, String> queryString = computeQueryString(request); | ||
final Map<String, String> headers = computeHeaders(request); | ||
final GatewayRequestContext context = | ||
gatewayRequestHandler.validateRequest( | ||
tenant, | ||
|
@@ -83,19 +88,102 @@ ProduceResponse produce( | |
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, e.getMessage()); | ||
} | ||
|
||
final ProduceGateway produceGateway = | ||
try (final ProduceGateway produceGateway = | ||
new ProduceGateway( | ||
topicConnectionsRuntimeRegistryProvider | ||
.getTopicConnectionsRuntimeRegistry()); | ||
try { | ||
.getTopicConnectionsRuntimeRegistry()); ) { | ||
final List<Header> commonHeaders = | ||
ProduceGateway.getProducerCommonHeaders( | ||
context.gateway().getProduceOptions(), authContext); | ||
produceGateway.start(context.gateway().getTopic(), commonHeaders, authContext); | ||
produceGateway.produceMessage(produceRequest); | ||
return ProduceResponse.OK; | ||
} finally { | ||
produceGateway.close(); | ||
} | ||
} | ||
|
||
private Map<String, String> computeHeaders(WebRequest request) { | ||
final Map<String, String> headers = new HashMap<>(); | ||
request.getHeaderNames() | ||
.forEachRemaining(name -> headers.put(name, request.getHeader(name))); | ||
return headers; | ||
} | ||
|
||
@PostMapping( | ||
value = "/service/{tenant}/{application}/{gateway}", | ||
consumes = MediaType.APPLICATION_JSON_VALUE) | ||
void service( | ||
WebRequest request, | ||
HttpServletResponse response, | ||
@NotBlank @PathVariable("tenant") String tenant, | ||
@NotBlank @PathVariable("application") String application, | ||
@NotBlank @PathVariable("gateway") String gateway, | ||
@RequestBody ProduceRequest produceRequest) | ||
throws ProduceGateway.ProduceException { | ||
|
||
final Map<String, String> queryString = computeQueryString(request); | ||
final Map<String, String> headers = computeHeaders(request); | ||
final GatewayRequestContext context = | ||
gatewayRequestHandler.validateRequest( | ||
tenant, | ||
application, | ||
gateway, | ||
Gateway.GatewayType.service, | ||
queryString, | ||
headers, | ||
new ProduceGateway.ProduceGatewayRequestValidator()); | ||
final AuthenticatedGatewayRequestContext authContext; | ||
try { | ||
authContext = gatewayRequestHandler.authenticate(context); | ||
} catch (GatewayRequestHandler.AuthFailedException e) { | ||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, e.getMessage()); | ||
} | ||
|
||
try (final ConsumeGateway consumeGateway = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have to follow up with a cache for this |
||
new ConsumeGateway( | ||
topicConnectionsRuntimeRegistryProvider | ||
.getTopicConnectionsRuntimeRegistry()); | ||
final ProduceGateway produceGateway = | ||
new ProduceGateway( | ||
topicConnectionsRuntimeRegistryProvider | ||
.getTopicConnectionsRuntimeRegistry()); ) { | ||
|
||
final Gateway.ServiceOptions serviceOptions = authContext.gateway().getServiceOptions(); | ||
try { | ||
final List<Function<Record, Boolean>> messageFilters = | ||
ConsumeGateway.createMessageFilters( | ||
serviceOptions.getHeaders(), | ||
authContext.userParameters(), | ||
authContext.principalValues()); | ||
consumeGateway.setup(serviceOptions.getInputTopic(), messageFilters, authContext); | ||
final AtomicBoolean stop = new AtomicBoolean(false); | ||
consumeGateway.startReadingAsync( | ||
consumeThreadPool, | ||
() -> stop.get(), | ||
record -> { | ||
stop.set(true); | ||
try { | ||
response.getWriter().print(record); | ||
response.getWriter().flush(); | ||
response.getWriter().close(); | ||
} catch (IOException ioException) { | ||
throw new RuntimeException(ioException); | ||
} | ||
}); | ||
} catch (Exception ex) { | ||
log.error("Error while setting up consume gateway", ex); | ||
throw new RuntimeException(ex); | ||
} | ||
final List<Header> commonHeaders = | ||
ProduceGateway.getProducerCommonHeaders(serviceOptions, authContext); | ||
produceGateway.start(serviceOptions.getOutputTopic(), commonHeaders, authContext); | ||
produceGateway.produceMessage(produceRequest); | ||
} | ||
} | ||
|
||
private Map<String, String> computeQueryString(WebRequest request) { | ||
final Map<String, String> queryString = | ||
request.getParameterMap().keySet().stream() | ||
.collect(Collectors.toMap(k -> k, k -> request.getParameter(k))); | ||
return queryString; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where do you set the timeout ?
I guess that we should give the client the ability to set the timeout for waiting for the message to arrive