gpt4 book ai didi

azure-cognitive-search - 每个套接字地址(协议(protocol)/网络地址/端口)通常只允许使用一次

转载 作者:行者123 更新时间:2023-12-01 19:38:50 25 4
gpt4 key购买 nike

过去几周,我们在使用 Azure 搜索 SDK (1.1.1 - 1.1.2) 和执行搜索时遇到此错误消息。

我们使用内部 API(部署为 Azure Web 应用程序)的搜索 SDK,这些 API 根据流量向上扩展(因此可能有超过 1 个 API 实例执行搜索)。

我们的 API 查询 5 个不同的索引,并维护一个与每个索引对应的 SearchIndexClient 对象的内存副本,一个非常简单的实现如下所示:

public class AzureSearchService
{
private readonly SearchServiceClient _serviceClient;

private Dictionary<string, SearchIndexClient> _clientDictionary;

public AzureSearchService()
{
_serviceClient = new SearchServiceClient("myservicename", new SearchCredentials("myservicekey"));
_clientDictionary = new Dictionary<string, SearchIndexClient>();
}

public SearchIndexClient GetClient(string indexName)
{
try
{
if (!_clientDictionary.ContainsKey(indexName))
{
_clientDictionary.Add(indexName, _serviceClient.Indexes.GetClient(indexName));
}
return _clientDictionary[indexName];
}
catch
{
return null;
}
}

public async Task<SearchResults> SearchIndex(SearchIndexClient client, string text)
{
var parameters = new SearchParameters();
parameters.Top = 10;
parameters.IncludeTotalResultCount = true;
var response = await client.Documents.SearchWithHttpMessagesAsync(text, parameters, null, null);
return response.Body;
}
}

API 将通过以下方式调用服务:

public class SearchController : ApiController
{
private readonly AzureSearchService service;

public SearchController()
{
service = new AzureSearchService();
}


public async Task<HttpResponseMessage> Post(string indexName, [FromBody] string text)
{
var indexClient = service.GetClient(indexName);
var results = await service.SearchIndex(indexClient, text);
return Request.CreateResponse(HttpStatusCode.OK, results, Configuration.Formatters.JsonFormatter);
}

}

由于需要接收自定义 HTTP header 而不是 SearchAsync 方法,我们正在使用 SearchWithHttpMessagesAsync

这样我们就可以避免在流量激增的情况下打开/关闭客户端。在使用此内存缓存(并将每个客户端包装在 using 子句中)之前,我们会在 Azure 应用服务上收到端口耗尽警报。

这是一个好的模式吗?由于并行运行多个实例,我们会收到此错误吗?

如果需要,堆栈跟踪显示:

System.Net.Http.HttpRequestException: Only one usage of each socket address (protocol/network address/port) is normally permitted service.ip.address.hidden:443


[SocketException:Only one usage of each socket address (protocol/network address/port)is normally permitted service.ip.address.hidden:443]

at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket& socket,IPAddress& address,ConnectSocketState state,IAsyncResult asyncResult,Exception& exception)



[WebException:Unable to connect to the remote server]

at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult,TransportContext& context)

at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)

编辑:我们收到此错误连接尝试失败,因为连接方在一段时间后未正确响应:

System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond service.ip.address.hidden:443


[SocketException:A connection attempt failed because the connected party did not properly respond after a period of time,or established connection failed because connected host has failed to respond service.ip.address.hidden:443]

at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket& socket,IPAddress& address,ConnectSocketState state,IAsyncResult asyncResult,Exception& exception)



[WebException:Unable to connect to the remote server]

at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult,TransportContext& context)

at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)

最佳答案

正如您问题中的代码所实现的那样,缓存不会阻止端口耗尽。这是因为您将其实例化为 ApiController 的字段,每个请求创建一次。如果你想避免端口耗尽,缓存必须在所有请求之间共享。要使其并发安全,您应该使用类似 ConcurrentDictionary 的东西而不是 Dictionary

“连接尝试失败”错误可能无关。

关于azure-cognitive-search - 每个套接字地址(协议(protocol)/网络地址/端口)通常只允许使用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37469740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com