Repository: dusk-network/rusk (language: rust)

Issue:
explorer: Add Keep-Alive to Request Headers in Explorer to Mitigate Connection Issues
# Add Keep-Alive to Request Headers in Explorer to Mitigate Connection Issues

## Description

Currently, the nodes' URL cannot handle a high number of connections efficiently. For the Explorer, each request opens and closes a new connection, which occurs frequently due to the short-polling mechanism. This behavior is putting excessive strain on the nodes and could lead to performance degradation or failures.

## Steps to Reproduce
1. Use the Explorer with short-polling enabled.
2. Observe the behavior of connections being opened and closed for each request.
3. Notice the high number of connection attempts on the node.

## Expected Behavior
The Explorer should reuse connections wherever possible to reduce the load on the nodes.

## Actual Behavior
Connections are opened and closed for every single request, leading to an overload on the nodes.

## Suggested Solution
- Add the `Keep-Alive` header to request headers in the Explorer.
- This would allow HTTP connections to remain open and be reused for subsequent requests, reducing the overhead of establishing new connections repeatedly.

## Additional Context
- Using `Keep-Alive` can significantly improve the efficiency of network communication and alleviate the strain on the nodes.
- Further optimizations could be considered after implementing and testing this first step.

## Impact
This change would improve the scalability and reliability of the Explorer by reducing the load on the nodes caused by frequent connection openings and closings.

## Resources
- [MDN HTTP Keep-Alive Documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive)
- [HTTP Connection Management Best Practices](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Connection)




Discussion / hints:

Fix patch:
diff --git a/explorer/src/lib/services/duskAPI.js b/explorer/src/lib/services/duskAPI.js
index 6df67ad4e6..65b5dc4a00 100644
--- a/explorer/src/lib/services/duskAPI.js
+++ b/explorer/src/lib/services/duskAPI.js
@@ -62,6 +62,7 @@ const gqlGet = (queryInfo) =>
     headers: {
       Accept: "application/json",
       "Accept-Charset": "utf-8",
+      Connection: "Keep-Alive",
       "Content-Type": "application/json",
       ...toHeadersVariables(queryInfo.variables),
     },
@@ -80,6 +81,7 @@ const apiGet = (endpoint, params) =>
     headers: {
       Accept: "application/json",
       "Accept-Charset": "utf-8",
+      Connection: "Keep-Alive",
     },
     method: "GET",
   })
@@ -95,6 +97,7 @@ const nodePost = (endpoint) =>
     headers: {
       Accept: "application/json",
       "Accept-Charset": "utf-8",
+      Connection: "Keep-Alive",
     },
     method: "POST",
   })


Test patch:
diff --git a/explorer/src/lib/services/__tests__/duskAPI.spec.js b/explorer/src/lib/services/__tests__/duskAPI.spec.js
index 5843f16815..5016dd3a9d 100644
--- a/explorer/src/lib/services/__tests__/duskAPI.spec.js
+++ b/explorer/src/lib/services/__tests__/duskAPI.spec.js
@@ -21,6 +21,7 @@ describe("duskAPI", () => {
     headers: {
       Accept: "application/json",
       "Accept-Charset": "utf-8",
+      Connection: "Keep-Alive",
     },
     method: "GET",
   };
@@ -75,6 +76,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-id": ""some-id"",
         },
@@ -88,6 +90,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-height": "495869",
         },
@@ -131,6 +134,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-height": "11",
         },
@@ -144,6 +148,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-height": "11",
         },
@@ -165,6 +170,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-id": ""some-id"",
         },
@@ -187,6 +193,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-amount": "100",
         },
@@ -211,6 +218,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-amount": "15",
         },
@@ -298,6 +306,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
         },
         "method": "POST",
       }
@@ -309,6 +318,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
         },
         "method": "POST",
@@ -321,6 +331,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
         },
         "method": "POST",
@@ -342,6 +353,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-id": ""some-id"",
         },
@@ -365,6 +377,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-id": ""some-id"",
         },
@@ -387,6 +400,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-amount": "100",
         },
@@ -438,6 +452,7 @@ describe("duskAPI", () => {
       headers: {
         Accept: "application/json",
         "Accept-Charset": "utf-8",
+        Connection: "Keep-Alive",
       },
       method: "POST",
     });
@@ -445,6 +460,7 @@ describe("duskAPI", () => {
       headers: {
         Accept: "application/json",
         "Accept-Charset": "utf-8",
+        Connection: "Keep-Alive",
       },
       method: "POST",
     });
@@ -486,6 +502,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-id": ""1111111111111111111111111111111111111111111111111111111111111111"",
         },
@@ -499,6 +516,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-height": "1.1111111111111112e+63",
         },
@@ -527,6 +545,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-id": ""fda46b4e06cc78542db9c780adbaee83a27fdf917de653e8ac34294cf924dd63"",
         },
@@ -554,6 +573,7 @@ describe("duskAPI", () => {
         "headers": {
           "Accept": "application/json",
           "Accept-Charset": "utf-8",
+          "Connection": "Keep-Alive",
           "Content-Type": "application/json",
           "Rusk-gqlvar-height": "123456",
         },