From 03bbf2f4a809cd3e8118bfc19224840953530a56 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Mon, 29 Jul 2019 12:37:28 -0400 Subject: [PATCH] SP-152 unsubscribe support --- src/lib/api/apigateway.proto | 2 ++ .../service_compiler/templates/fs/fable.fs | 3 +- src/server/sparkd/http/ApiGateway.cpp | 36 ++++++++++++++----- src/web/api/src/DynamicService.ts | 7 +++- src/web/api/src/ServiceContext.ts | 4 +-- src/web/ui/src/Util/StreamUtils.fs | 3 ++ 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/lib/api/apigateway.proto b/src/lib/api/apigateway.proto index a88214007..1463f8354 100644 --- a/src/lib/api/apigateway.proto +++ b/src/lib/api/apigateway.proto @@ -33,6 +33,8 @@ message GatewaySubscribe { } message GatewayUnsubscribe { uint32 subscription_id = 1; + string service_type = 4; + string service_qualifier = 5; } message StreamGWUpdate { diff --git a/src/lib/api/service_compiler/templates/fs/fable.fs b/src/lib/api/service_compiler/templates/fs/fable.fs index f91357c81..226b85674 100644 --- a/src/lib/api/service_compiler/templates/fs/fable.fs +++ b/src/lib/api/service_compiler/templates/fs/fable.fs @@ -69,7 +69,8 @@ type I{{name}} = {{#each method}} {{#if server_streaming}} // {{comment}} - abstract member subscribe_{{name}} : qualifier:string -> callback:(StreamUtils.IUpdate<{{output_type_full}}>->unit) -> unit + abstract member subscribe_{{name}} : qualifier:string -> callback:(StreamUtils.IUpdate<{{output_type_full}}>->unit) -> StreamUtils.Subscription + abstract member unsubscribe_{{name}} : subscription:StreamUtils.Subscription -> unit {{else}} // {{comment}} abstract member {{name}}: {{input_type_full}} -> Fable.Import.JS.Promise<{{output_type_full}}> diff --git a/src/server/sparkd/http/ApiGateway.cpp b/src/server/sparkd/http/ApiGateway.cpp index 394e5fecb..42e88cd32 100644 --- a/src/server/sparkd/http/ApiGateway.cpp +++ b/src/server/sparkd/http/ApiGateway.cpp @@ -19,6 +19,7 @@ namespace http { struct SubscriptionContext { int id; Responder send; + api::TableSubscription table; }; struct ApiGatewayImpl { @@ -27,7 +28,7 @@ struct ApiGatewayImpl { api::user::AuthService _logon; std::unordered_map _apiclient; - std::map _subscriptions; + std::map _subscriptions; ApiGatewayImpl(const api::ServiceContext& ctx) : @@ -72,13 +73,16 @@ struct ApiGatewayImpl { } // Create or return the client proxy - api::ApiClient* get_api_client(const std::string& service_type, const std::string& qualifier) { + api::ApiClient* get_api_client(const std::string& service_type, const std::string& qualifier, bool create=true) { auto key = service_type + qualifier; auto iter = _apiclient.find(key); if (iter == _apiclient.end()) { - auto* pclient = api::api_client_create(&_sc.bus(), service_type.c_str(), qualifier.c_str()); - _apiclient.insert(std::make_pair(key, pclient)); - return pclient; + if (create) { + auto* pclient = api::api_client_create(&_sc.bus(), service_type.c_str(), qualifier.c_str()); + _apiclient.insert(std::make_pair(key, pclient)); + return pclient; + } + else return nullptr; } else { return iter->second; @@ -131,7 +135,7 @@ struct ApiGatewayImpl { // When a tableupate fires attach the id to the reply and send it over the channel // Responder* preply = new Responder(move(reply)); auto id = request.request_id(); - SubscriptionContext* pcontext = new SubscriptionContext { id, move(reply) }; + SubscriptionContext* pcontext = new SubscriptionContext { id, move(reply), nullptr }; auto table = api_client_subscribe(pclient, request.method_name().c_str(), request.stream_qualifier().c_str(), [](api::EventType type, const api::StreamUpdate* changes, int count, void* extra) { @@ -154,12 +158,28 @@ struct ApiGatewayImpl { } pcontext->send(update); }, pcontext); + pcontext->table = table; - _subscriptions[id] = table; + _subscriptions[id] = pcontext; // reply with empty response so we don't time out reply({}); } - void handle_unsubscribe(const GatewayUnsubscribe&, const Responder& reply) { + void handle_unsubscribe(const GatewayUnsubscribe& request, const Responder& reply) { + auto id = request.subscription_id(); + api::ApiClient* pclient = get_api_client(request.service_type(), request.service_qualifier(), false); + bts_assert_d(pclient); + auto iter = _subscriptions.find(id); + if (iter != _subscriptions.end()) { + SubscriptionContext* pcontext = iter->second; + _subscriptions.erase(iter); + if(pcontext && pclient) { + api_client_unsubscribe(pclient, pcontext->table); + } + // Note: not safe to delete context yet + api::api_post(_sc.bus(), [pcontext]() { + delete pcontext; + }, 5000); + } // empty response makes quickbus happy reply({}); } diff --git a/src/web/api/src/DynamicService.ts b/src/web/api/src/DynamicService.ts index 2d1489103..a8208b305 100644 --- a/src/web/api/src/DynamicService.ts +++ b/src/web/api/src/DynamicService.ts @@ -41,7 +41,6 @@ function gen_subscribe(service:IService, method_id : number, methodName : string console.log(`--subscribe-- ${service.name}:${method_id}`) return service.context.subscribe(service.name, service.qualifier, method_id, methodName, qualifier, (updates) => { // iterate and decode each update - // create an Update let result : Update = { all: {}, added: {}, changed: {}, removed: {} }; updates.forEach((value:RawUpdate) => { const key = value.key; @@ -76,6 +75,11 @@ function gen_subscribe(service:IService, method_id : number, methodName : string }); } } +function gen_unsubscribe(service:IService) { + return (id:number) => { + service.context.unsubscribe(id, service.name, service.qualifier); + }; +} export class DynamicService { constructor( @@ -91,6 +95,7 @@ export class DynamicService { for (var [streamName,id,typeName] of streams) { const fnName = "subscribe_" + streamName; this[fnName] = gen_subscribe(svc, id, streamName, typeName) + this["un"+fnName] = gen_unsubscribe(svc); } } } diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index 98cbb16ef..cd448ec86 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -206,8 +206,8 @@ export class ServiceContext { return id; } - unsubscribe(subscriptionId : number) { - let req = GatewayUnsubscribe.crate({ subscriptionId}); + unsubscribe(subscriptionId : number, serviceType : string, serviceQualifier : string) { + let req = GatewayUnsubscribe.create({subscriptionId, serviceType, serviceQualifier}); let message = GatewayRequest.create({ unsubscribe: req }); diff --git a/src/web/ui/src/Util/StreamUtils.fs b/src/web/ui/src/Util/StreamUtils.fs index 76d533e65..31ab9841b 100644 --- a/src/web/ui/src/Util/StreamUtils.fs +++ b/src/web/ui/src/Util/StreamUtils.fs @@ -1,5 +1,6 @@ module StreamUtils open JSMap +open Fable.Core type Change<'a> = { newItem : 'a @@ -12,3 +13,5 @@ type IUpdate<'a> = abstract member removed : JSMap<'a> with get abstract member changed : JSMap> with get +[] +type Subscription = Subscription of int \ No newline at end of file