Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] [core-http/search-sdk] How can I specify max sockets or provide my own HTTP agent to an Azure SearchClient? #25522

Closed
mdmower opened this issue Apr 11, 2023 · 7 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. feature-request This issue requires a new behavior in the product in order be resolved. Search

Comments

@mdmower
Copy link

mdmower commented Apr 11, 2023

Is there a way to pass in our own http.Agent or to specify maxSockets when creating an Azure SearchClient? I'm aiming to allow the use of both node-fetch and Azure Search SDK in an Azure Function to make requests to <mysearch>.search.windows.net, but need to ensure I don't run into port exhaustion (max 128 connections per host/port combination).

I started this question here and @xirzec kindly offered some tips, but I haven't managed to piece it all together yet. The suggestion there was:

I think you could do this by creating a custom PipelinePolicy that sets the agent property on each PipelineRequest made by the Search SDK when performing an operation. To have the SDK use your custom policy, you can pass it via the additionalPolicies client option.

I haven't seen where I can pass the additionalPolicies option for the search client.

The constructor for SearchClient allows passing SearchClientOptions (which is a small extension of PipelineOptions). Of these options, I take advantage of keepAliveOptions, but I haven't figured out how to provide a customized HttpClient. I can construct an instance of NodeFetchHttpClient, but it doesn't appear configurable. Any hints would be appreciated.

const client = new SearchClient<MyEntity>(endpoint, index, new AzureKeyCredential(apiKey), {
  keepAliveOptions: { enable: true },
  httpClient: /* is there a way to configure an HttpClient? */
});
@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Apr 11, 2023
@mdmower
Copy link
Author

mdmower commented Apr 11, 2023

In case it's useful, I drafted a PR of what adding max sockets support would look like: #25523 .

@xirzec xirzec added Search Client This issue points to a problem in the data-plane of the library. feature-request This issue requires a new behavior in the product in order be resolved. and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Apr 12, 2023
@github-actions github-actions bot removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Apr 12, 2023
@xirzec
Copy link
Member

xirzec commented Apr 12, 2023

Oh I see the issue here, the problem is that the GA version of @azure/search-documents still depends on @azure/core-http, a soon to be deprecated package.

@dgetu is there a plan to release a new beta or GA of search anytime soon? It looks like the last beta pre-dates the most recent GA and I'm not sure what will be lost if we suggest @mdmower switch over to it?

@mdmower if you wanted to at least test out the new pipeline functionality you could try pulling down the latest dev tag of the package to get what is in main and see the policy pipeline options I mentioned.

@dgetu
Copy link
Member

dgetu commented Apr 12, 2023

@xirzec The most recent beta release is at feature parity with GA. The more recent 11.3.1 GA release was a hotfix for a bug that isn't present in the beta.

@mdmower I would give the latest beta a try, 11.3.0-beta.8. So long as you avoid using preview features, you shouldn't have too much trouble moving back to GA as soon as we're able to migrate away from @azure/core-http.

@mdmower
Copy link
Author

mdmower commented Apr 12, 2023

Thanks @dgetu. I took a peek at the beta and the capabilities of additionalPolicies in SearchClient construction. From what I can see, it's possible to affect the request and response with a PipelinePolicy, but not to tinker with the https.Agent (Node.js). I'd have to submit a PR. Is that right? For example, the disableKeepAlivePolicy isn't sufficient as a standalone policy; explicit handling had to be added to getOrCreateAgent() to support the feature. I just want to make sure I'm not missing anything before I work on a PR.

@dgetu
Copy link
Member

dgetu commented Apr 12, 2023

@mdmower You can modify in-flight PipelineRequests with more granularity by using a custom PipelinePolicy. For example,

import { PipelinePolicy } from "@azure/core-rest-pipeline";
import { SearchClient } from "@azure/search-documents";
import https from "https";

const agentPolicy: PipelinePolicy = {
  name: "agentPolicy",
  async sendRequest(request, next) {
    request.agent = new https.Agent({
      /**/
    });
    return next(request);
  },
};

const client = new SearchClient("endpoint", "indexName", "credential" as any, {
  additionalPolicies: [{ policy: agentPolicy, position: "perCall" }],
});

This will insert a https.Agent directly into all Node HTTP client requests that come from this SearchClient. Let me know if there's anything I can clear up for you.

@mdmower
Copy link
Author

mdmower commented Apr 14, 2023

Aha! Thank you for the example @dgetu! I see where it's utilized here. It's working as expected with @azure/search-documents@11.3.0-beta.8 and @azure/core-rest-pipeline@1.10.3. I look forward to the GA release. Again, thanks so much for your help @xirzec and @dgetu.

@dgetu
Copy link
Member

dgetu commented Aug 21, 2023

As of 11.3.2, you should be able to use the same technique with the stable version of @azure/search-documents.

@dgetu dgetu closed this as completed Aug 21, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. feature-request This issue requires a new behavior in the product in order be resolved. Search
Projects
None yet
Development

No branches or pull requests

3 participants