Skip to content

Commit

Permalink
docs(sample): Improve keepAlive sample with transporterOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbankhead committed Oct 16, 2023
1 parent ef6aa2e commit f950465
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions samples/keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,26 @@ const https = require('https');
* Acquire a client, and make a request to an API that's enabled by default.
*/
async function main() {
// create a new agent with keepAlive enabled
const agent = new https.Agent({keepAlive: true});

const auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform',
clientOptions: {
transporterOptions: {
agent,
},
},
});
const client = await auth.getClient();
const projectId = await auth.getProjectId();
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;

// create a new agent with keepAlive enabled
const agent = new https.Agent({keepAlive: true});

// use the agent as an Axios config param to make the request
const res = await client.request({url, agent});
// the agent uses the provided agent.
const res = await client.request({url});
console.log(res.data);

// Re-use the same agent to make the next request over the same connection
// Can also use another agent per-request.
const res2 = await client.request({url, agent});
console.log(res2.data);
}
Expand Down

0 comments on commit f950465

Please sign in to comment.