[BUG] Connection headers mutability and unintended reuse of headers #291
Description
What is the bug?
Connection.js headers property is reused over multiple requests, and due to mutability of headers, this can cause unwanted headers to be sent with subsequent requests.
How can one reproduce the bug?
Using opensearch-project/opensearch with aws-opensearch-connector, call search repeatedly from multiple expressjs processes, being sure to execute queries on es cluster that run for more than 5 minutes (Ultrawarm index in our case).
Eventually, an X-Amz-Date header will be added to Connection.headers, and due to logic in aws4.js, this header will be sent with every request, and never get updated.
Eventually, the permanent X-Amz-Date header will expire, and all subsequent requests will fail with "Signature expired" error.
The issue only resolves itself when you recreate a new Client (which recreates a new Connection). Eventually, the header will be rewritten though, and the process will repeat itself.
What is the expected behavior?
headers should not be reused across multiple distinct requests. Connection class should not use a reference to this.headers when initializing a request, it should make a copy:
headers: Object.assign({}, this.headers), // create a clone
What is your host/environment?
Linux/node 16
Do you have any screenshots?
Do you have any additional context?
This issue seems to only arise in the presence of multiple requests being handled for an es cluster that has queued search threads and long running query executions that exceed 5 minutes.
Is there an initialization or usage pattern that would result in ability to pass in blank headers to each request? Otherwise, it really does seem like the Connection headers property is being passed around by reference but also reused over multiple requests.