简体   繁体   English

向 API 发布多个请求,但不重复使用连接

[英]Post multiple requests to an API but without re-using connections

I have a console application which is calling a third party API, basically it takes records from a database and pushes them to the third party via their WCF API.我有一个调用第三方 API 的控制台应用程序,基本上它从数据库中获取记录并通过他们的 WCF API 将它们推送给第三方。

For various reasons (Mainly the third party API being very slow - 7 seconds to respond) we want to post multiple records in parallel so we have started doing this, however we are now seeing some strange behaviour from the third party API where it is duplicating records.由于各种原因(主要是第三方 API 非常慢 - 响应 7 秒),我们希望并行发布多个记录,因此我们已经开始这样做,但是我们现在看到来自第三方 API 的一些奇怪行为,它正在复制记录。

It has been suggested to us by the developers of the API that this is because we are sending the requests over the same connection (which makes sense as .net will reuse connections) and they dont/cant/wont support that, they will only support one request over one connection and then the connection must be closed. API 的开发人员向我们建议,这是因为我们通过同一连接发送请求(这是有道理的,因为 .net 将重用连接)并且他们不/不能/不会支持,他们只会支持一个请求通过一个连接,然后连接必须关闭。

My question is, How do I do this in .net core (2.2) We are currently using a HttpClient which I'd expect to reuse connections where possible - how can I guarantee that we use a new connection for each request?我的问题是,如何在 .net core (2.2) 中执行此操作我们目前正在使用 HttpClient,我希望在可能的情况下重用连接 - 我如何保证我们为每个请求使用新连接?

I have after some digging worked out what the problem is and there is no way to fix it.经过一番挖掘,我发现了问题所在,但没有办法解决。

The process is :过程是:

  1. We Post into the API我们发布到 API
  2. The API creates a record in a staging table in the database with a flag showing a status of "to be processed" and then starts polling the table waiting for change. API 在数据库的临时表中创建一个记录,并带有一个显示“待处理”状态的标志,然后开始轮询等待更改的表。
  3. The API then invokes an executable on the server which looks at the staging table for any records with a status of "to be processed"然后 API 调用服务器上的可执行文件,该可执行文件查看暂存表中是否有状态为“待处理”的任何记录
  4. The executable does its processing and then changes the status on the record to "complete"可执行文件进行处理,然后将记录上的状态更改为“完成”
  5. The API which has been polling the table sees the changed status, reads the record and returns the result to the client.一直在轮询表的 API 看到更改的状态,读取记录并将结果返回给客户端。

All fine if you only ever post one record at a time.如果您一次只发布一条记录,那一切都很好。 But as Im executing in parallel what is happening is :但是当我并行执行时,正在发生的事情是:

  1. We call the API 10 times within a few ms of each other我们在几毫秒内调用 API 10 次
  2. The API creates 10 records in the staging table all with the "to be processed" status. API 在临时表中创建 10 条记录,所有记录都处于“待处理”状态。
  3. The API starts polling the staging table for changes and at the same time invokes the executable TEN TIMES API 开始轮询临时表以进行更改,同时调用可执行文件十次
  4. All 10 instances of the executable read all 10 records and process them - each instance unaware that there are another 9 instances all doing the same thing.可执行文件的所有 10 个实例读取所有 10 个记录并处理它们 - 每个实例都不知道还有另外 9 个实例都在做同样的事情。
  5. All 10 instances of the executable finish processing and change the status on the staging table to "complete"可执行文件的所有 10 个实例都完成处理并将暂存表上的状态更改为“完成”
  6. The API sees the status change and returns all of the changed records to me in the response - So each of the 10 requests I sent gets 10 records returned to it. API 看到状态更改并在响应中将所有更改的记录返回给我 - 因此我发送的 10 个请求中的每一个都会返回 10 个记录。

Needless to say, we have entered into discussions with the provider of the API, it might be EOL and pretty much out of support but we're paying for licencing of this thing and this is a really stupid process that they need to provide a fix or a workaround for.不用说,我们已经与 API 的提供者进行了讨论,它可能是 EOL 并且几乎没有支持,但我们正在为这个东西的许可付费,这是一个非常愚蠢的过程,他们需要提供修复或解决方法。

So in the end it was nothing to do with reusing connections, dont know why we were told it was.所以最后这与重用连接无关,不知道为什么我们被告知是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM