solclient send request resulting success and fail at the same time - callback

I'm starting a new project with Solace as the load balancer. As I follow the guideline on the official doc to build a service that can send requests to Solace, I encounter a weird issue where my request is successful and fails simultaneously. Here's my code
function initSolace(pass: string) {
var factoryProps = new solace.SolclientFactoryProperties();
factoryProps.profile = solace.SolclientFactoryProfiles.version10;
solace.SolclientFactory.init(factoryProps);
session = solace.SolclientFactory.createSession({
"url": "ws://localhost:8008",
"userName": "tech_core",
"vpnName": "testing",
"password": pass
}, new solace.MessageRxCBInfo(messageRxCb));
session.on(solace.SessionEventCode.UP_NOTICE, function (sessionEvent: any) {
requestData(10000).subscribe();
});
session.connect();
}
async function messageRxCb(session: any, message: any) {
message = Parser.decodeBinaryAttachmentToPb(message, pnlPb.RespPnl);
console.log('result from RxCb', message); // I got the correct response here
}
function requestData(timeout = 10000) {
return new Observable(subscriber => {
const parsedPayload = Parser.createPb({displayCurrency: 'USD'}, pnlPb.ReqPnl);
const msg = Parser.encodePbToBinaryAttachment(parsedPayload, pnlPb.ReqPnl);
const request = solace.SolclientFactory.createMessage();
request.setDestination(solace.SolclientFactory.createTopicDestination('my/testing/topic'));
request.setDeliveryMode(solace.MessageDeliveryModeType.DIRECT);
request.setDeliverToOne(true);
request.setBinaryAttachment(msg);
session.sendRequest(request, timeout,
(ses: any, message: any) => {
console.log('SUCCESS', message);
subscriber.next(message);
},
(ses: any, event: any) => {
console.error('FAIL', event); // I got a timeout error here
subscriber.error(event);
},
'correlation test'
)
});
}
As I run the code, I gets the timeout error from the requestData function AND the correct data from the messageRxCb function as well.
How does this happening? Did I miss any config here?

The Request-Reply pattern is a closely coupled communication pattern.
Every request that is posted by the requestor requires a response from the replier within the timeout specified.
I see from your code sample that you have configured a timeout of 10000ms. What it means is that every request that is posted should receive an incoming reply within 10000ms. If this response is not received, then it would result in the timeout error that you see in the console.
The reason why you see that the request is successfull and the error is because while the request has been successfully posted, a reply was not received within the specified timeout.
Do you already have a replier setup for this interaction? If not then I would suggesting setting up a simple boilerplate replier listening on this and testing the flow again.
Additionally, it would be good coding practise to handle the timeout error in a functionally appropriate manner.
Regards
Hari

Related

stream two event messages in one from gRPC Server

I have a scenario where I am trying to send multiple messages in one and trying to stream the one message from the gRPC server to the gRPC client side.
My proto files on server side look like this:
Service Greeter{
rpc AccumulateEvents (EventRequest) returns (stream EventsMessage);
}
message EventsMessage{
FirstEvent firstEvents =1;
SecondEvents secondEvent = 2;
}
message EventRequest{
//sending empty request
}
The service method is as follows:
public override async Task AccumulateEvents(EventRequest eventRequest, IServerStreamWriter < EventsMessage > responseStream, ServerCallContext context) {
IDisposable disposable4 = service.SubscribeToEvents(OnEvents);
service.execute();
await responseStream.WriteAsync(new EventsMessage {
FirstEvent = firstEvent, SecondEvents = secondEvents
});
}
When I am trying to fetch and parse the stream from the client side,i am getting null for secondEvent part of the message EventsMessage. Only firstEvents was returned from the server to the client. I tried debugging and could see secondEvent getting populated but then it became null when the streaming started from the server.
Also, secondEvent is a repeated field. I am not sure if that is the reason of it becoming null.
Please let me know what i might be missing here.

Vertx: How to verify auth before handle body request?

How to verify auth before handle body request?
I'm using vertx:
vertxVersion = '3.8.3'
implementation "io.vertx:vertx-core:$rootProject.vertxVersion"
implementation "io.vertx:vertx-web:$rootProject.vertxVersion"
implementation "io.vertx:vertx-lang-kotlin:$rootProject.vertxVersion"
implementation "io.vertx:vertx-lang-kotlin-coroutines:$rootProject.vertxVersion"
implementation "io.vertx:vertx-mongo-client:$rootProject.vertxVersion"
implementation "io.vertx:vertx-auth-mongo:$rootProject.vertxVersion"
implementation "io.vertx:vertx-auth-jwt:$rootProject.vertxVersion"
I want to verify auth before handle body request. But I got error java.lang.IllegalStateException: Request has already been read
Reproduce by use delay on suspend function:
router.handler { context ->
launch {
context.request().setExpectMultipart(true)
delay(100) //This line is sample for a verify auth process
context.next()
}
}
.handler {context ->
println("2")
context.request()
.handler {
b -> println("buff ${b.length()}")
}
.endHandler {
println("end handle")
context.success("ok")
}
}.baseHandle(
fn
).failureHandler {
println("fail: ${it.failure()}")
it.error()
}
When run delay(100) (this's sample for a verify process), I got the error above. If I comment delay(100), It's will be working fine.
This happens because by the time you auhenticated the request, the content has kept arriving and has been dropped.
You need to invoke context.request().pause() in you first handler and then context.request().resume() when you're ready.
In most cases though it's easier to let the BodyHandler manage payload for you.
Finally, I did solve it.
My router is working with the flows:
router.post("/api/upload/file")
.baseHandle { checkAuthorization(it) }
.handleFileUpload { updateFileOnItem(it) }
And Following step:
fun checkAuthorization(context: RoutingContext) {
val request = context.request()
val tkCookie = request.getCookie("user")
...do something to verify user permission
request.pause()
context.next()
context.request().resume()
}
Next:
fun updateFileOnItem(context: RoutingContext) {
val file = context.fileUploads()
...do something
}
It's working with me. Hope it can be help you. Thanks!

Play framework Scala run job in background

Is there any way I can trigger a job from the controller (to not to wait for its completion) and display the message to the user that job will be running in the background?
I have one controller method which takes quite long time to run. So I want to make that run offline and display the message to the user that it will be running in the background.
I tried Action.async as shown below. But the processing of the Future object is still taking more time and getting timed out.
def submit(id: Int) = Action.async(parse.multipartFormData) { implicit request =>
val result = Future {
//process the data
}
result map {
res =>
Redirect(routes.testController.list()).flashing(("success", s"Job(s) will be ruuning in background."))
}
}
You can also return a result without waiting for the result of the future in a "fire and forget" way
def submit(id: Int) = Action(parse.multipartFormData) { implicit request =>
Future {
//process the data
}
Redirect(routes.testController.list()).flashing(("success", s"Job(s) will be running in background."))
}
The docs state:
By giving a Future[Result] instead of a normal Result, we are able to quickly generate the result without blocking. Play will then serve the result as soon as the promise is redeemed.
The web client will be blocked while waiting for the response, but nothing will be blocked on the server, and server resources can be used to serve other clients.
You can configure your client code to use ajax request and display a Waiting for data message for some part of the page without blocking the rest of the web page from loading.
I also tried the "Futures.timeout" option. It seems to work fine. But I'm not sure its correct way to do it or not.
result.withTimeout(20.seconds)(futures).map { res =>
Redirect(routes.testController.list()).flashing(("success", s"Job(s) will be updated in background."))
}.recover {
case e: scala.concurrent.TimeoutException =>
Redirect(routes.testController.list()).flashing(("success", s"Job(s) will be updated in background."))
}

Redux-saga and socket subscription causes Uncaught TypeError: Converting circular structure to JSON

I am having trouble subscribing to a socketcluster (http://socketcluster.io/) channel when using a redux-saga generator in my chat app. The socketcluster backend is setup in a way where any messages are saved in the database then published into the receiving user's personal channel, which is named after the user's id. For example, User A has an id '123abc' and would subscribe to the channel named '123abc' for their realtime messages.
The code below does receive new messages that are published to a channel but it throws a "TypeError: Converting circular structure to JSON" onload and breaks all of my other redux-saga generators in the app. I've done digging in Chrome Devtools and my theory is that it has something to do with queue created in the createChannel function. Also, I've tried returning a deferred promise in the subscribeToChannel function but that also caused a Circular Conversion Error, I can post that code on request.
I referred to this answer at first: https://stackoverflow.com/a/35288877/5068616 and it helped me get the below code in place but I cannot find any similar issues on the internet. Also something to note, I am utilizing redux-socket-cluster (https://github.com/mattkrick/redux-socket-cluster) to sync up the socket and state, but I don't think it is the root of the problem
sagas.js
export default function* root() {
yield [
fork(startSubscription),
]
}
function* startSubscription(getState) {
while (true) {
const {
userId
} = yield take(actions.SUBSCRIBE_TO_MY_CHANNEL);
yield call(monitorChangeEvents, subscribeToChannel(userId))
}
}
function* monitorChangeEvents(channel) {
while (true) {
const info = yield call(channel.take) // Blocks until the promise resolves
console.log(info)
}
}
function subscribeToChannel(channelName) {
const channel = createChannel();
const socket = socketCluster.connect(socketConfig);
const c = socket.subscribe(channelName);
c.watch(event => {
channel.put(event)
})
return channel;
}
function createChannel() {
const messageQueue = []
const resolveQueue = []
function put(msg) {
// anyone waiting for a message ?
if (resolveQueue.length) {
// deliver the message to the oldest one waiting (First In First Out)
const nextResolve = resolveQueue.shift()
nextResolve(msg)
} else {
// no one is waiting ? queue the event
messageQueue.push(msg)
}
}
// returns a Promise resolved with the next message
function take() {
// do we have queued messages ?
if (messageQueue.length) {
// deliver the oldest queued message
return Promise.resolve(messageQueue.shift())
} else {
// no queued messages ? queue the taker until a message arrives
return new Promise((resolve) => resolveQueue.push(resolve))
}
}
return {
take,
put
}
}
Thanks for the help!

socket io emit failed callback

Is there any way to know socket io emit failed and success, something like ajax callback methods: onSuccess, onError?
For socket io emit i only find:
socket.emit('publish', {message:'test message'},function (data) {
alert("")})
This callback only be called when the server send an ack response.But it can not apply for this situation:
At the moment of emit message to server, there is bad network or lost connection, that means server not receive this message, so the client callback function is not called.
What I want is:
When I call the socket io emit, if it fails, I want to retry 3 times.
I know this is an old post, but just in case anyone is still having trouble with this.
var socket = new io.connect('http://localhost:3000', {
'reconnection': true,
'reconnectionDelay': 1000,
'reconnectionDelayMax' : 5000,
'reconnectionAttempts': 3
});
socket.on('connect_error', function() {
console.log('Connection failed');
});
socket.on('reconnect_failed', function() {
// fired only after the 3 attemps in this example fail
console.log('Reconnection failed');
});
More info here -> https://socket.io/docs/client-api/#manager-reconnectionAttempts-value