File size: 1,184 Bytes
d7dfeff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 };