const cleanupConnection = async (session) => { try { if (session.channel) { const state = session.channel.getConnectivityState(false); console.log(`Client : ${state}`); if (state !== 4) { console.log("Closing call and client."); session.client.close(); session.call.end(); session.client = null; session.call = null; } } else { try { if (session.client) { session.client.close(); if (session.call) { session.call.end(); } session.call = null; session.client = null; session.channel = null; } } catch (err) { session.call = null; session.client = null; session.channel = null; } } console.log("gRPC connection ended."); } catch (err) { if (session.call) { session.call.end(); } session.call = null; console.log("Error ending gRPC connection: ", err); } finally { if (session.call) { session.call.end(); } session.call = null; session.client = null; session.channel = null; } }; module.exports = { cleanupConnection };