SP-152 unsubscribe support
This commit is contained in:
1 parent
f2d284cadb
commit
03bbf2f4a8
6 files changed
+43
-12
No files matched your search
@@ -33,6 +33,8 @@ message GatewaySubscribe {
|
||||
}
|
||||
message GatewayUnsubscribe {
|
||||
uint32 subscription_id = 1;
|
||||
string service_type = 4;
|
||||
string service_qualifier = 5;
|
||||
}
|
||||
|
||||
message StreamGWUpdate {
|
||||
|
||||
@@ -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}}>
|
||||
|
||||
@@ -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<std::string, api::ApiClient*> _apiclient;
|
||||
std::map<int32_t, api::TableSubscription> _subscriptions;
|
||||
std::map<int32_t, SubscriptionContext*> _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({});
|
||||
}
|
||||
|
||||
@@ -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<t>
|
||||
let result : Update<any> = { 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
});
|
||||
|
||||
@@ -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<Change<'a>> with get
|
||||
|
||||
[<Erase>]
|
||||
type Subscription = Subscription of int
|
||||
Reference in new issue
Block a user