SAP / node-rfc

Asynchronous, non-blocking SAP NW RFC SDK bindings for Node.js
Apache License 2.0
249 stars 73 forks source link

Question: Cancel connection #231

Closed Berdmanfolk closed 2 years ago

Berdmanfolk commented 2 years ago

Hello @bsrdjan! Can you please to clarify how to use Client cancellation. I was compile for last version 2.5.1. but now when I try to call method Cancel() I gett error that method is not found. In devetools of browser I don see this method:

this: Islet
_client: Client
_alive: (...)
_config: (...)
_connectionHandle: (...)
_id: (...)
_pool_id: (...)
[[Prototype]]: Object
close: ƒ close()
connectionInfo: ƒ connectionInfo()
constructor: ƒ Client()
invoke: ƒ invoke()
open: ƒ open()
ping: ƒ ping()
release: ƒ release()
resetServerContext: ƒ resetServerContext()
_alive: (...)
_config: (...)
_connectionHandle: (...)
_id: (...)
_pool_id: (...)
get _alive: ƒ ()
get _config: ƒ ()
get _connectionHandle: ƒ ()
get _id: ƒ ()
get _pool_id: ƒ ()
[[Prototype]]: Object

I was try also to get timeout by ​event is using such code: But timeout is not arise.

    try {
        Rfc = require('./sapnwrfc.node').Client;
        const client = new Rfc(curconnection, {timeout: 3});

        client.open(function (err) {
            if (err) {
                reject(err);
            } else { 
                resolve(client);
            }
        });                                                 
    } catch {

    }   

Can you pleaswe clarify what I do wrong?

bsrdjan commented 2 years ago

A longer RFC call can be simulated by calling RFC_PING_AND_WAIT ABAP function module. It will wait for a number of seconds, provided in input parameter SECONDS, and return.

The timeout can be tested by calling this function module with SECONDS: 5 for example and setting connection timeout to 3 seconds. After 3 second the timeout event will be fired, as shown in direct-callback.mjs source code example. Few more examples are provided in a parent folder

The client.cancel() can be tested in a similar way. Call RFC_PING_AND_WAIT and while waiting for the call to complete, call the cancel() method, exposed at addon, pool or client level. Examples can be found in unit tests, like client.cancel.spec.js

Hope this helps further.

Berdmanfolk commented 2 years ago

I think, I understand - we are talking about when the connection to the sap system has already taken place. And if it is possible to interrupt the connection when the connection to the stand-alone system has not yet been completed, but due to some network problems it takes a long time?

bsrdjan commented 2 years ago

In case of network problems the default timeout is 1 min, defined by SAP NWRFC SDK. After that time the network error is raised with more diagnostic info.

The application can define its own shorter timeout and in that case no info on network error is received, something like:

const RFC = require("node-rfc");

const client = new RFC.Client({ dest: "MME" });

(async () => {
    try {
         // check of opening connection takes longer than 1 sec
        setTimeout(() => {
            if (!client.connectionHandle) {
                // handle open timeout here
            }
        }, 1000);

        await client.open();

       console.log("Connection opened");

    } catch (ex) {
        console.log("error", ex, client.alive, client.connectionHandle);
    }
})();
bsrdjan commented 2 years ago

Please re-open if needed.

Berdmanfolk commented 2 years ago

Oh, It is need

bsrdjan commented 2 years ago

Only opened connection can be cancelled. The cancel() method requires connection handle and the handle is known only after connection opened. How would you like to use cancellation, can you describe the usage scenario?