首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ERP 改单接口超时了,重发之前查什么?

ERP 改单接口超时了,重发之前查什么?

原创
作者头像
用户12915796
发布于 2026-12-03 13:44:44
发布于 2026-12-03 13:44:44
200
举报

订单从 500 件改到 600 件。ERP 已经保存,调用方却在收到响应前超时。如果重发时换了请求号,ERP 可能把第二次调用也当成一笔新业务。两次 HTTP 请求都走到哪里,得看写入日志。

用三个回放看差异

这次用一个本机 HTTP 测试服务模拟 ERP。服务按请求号记录提交,首次提交后故意延迟响应。查询接口也模拟了一次结果晚到。三个场景各用一个独立的演示请求编号。改动内容只用一笔虚构订单。

调用方看到什么

接下来做什么

本机演示结果

写入超时,换一个请求号重发

查两个请求号是否各有提交记录

两次提交

写入超时,按原请求号和原内容重试

先确认目标接口支持幂等,再比对原提交日志

一次提交。用同一请求号改内容,返回 409

查原请求号,第一遍没有记录

标记「待确认」,保留原请求号,下次再查

第二遍查到已写入,全程一次提交

真实接口是否支持这些行为,要看 ERP 的接口契约。有没有幂等键、同键能否接收不同内容、状态查询有无延迟,都可以在接入前问清楚。

把「待确认」做成验收动作

  1. 保留原请求号、提交内容、审批记录和发起前读到的 ERP 版本。
  2. 超时后用原请求号查写入日志和订单当前版本。已提交就沿用记录,结果冲突交业务复核,仍未知就写清谁接手、什么时候回查。
  3. 重跑验收时请操作人同时展示请求日志和 ERP 记录。两次成功提示无法说明实际提交次数,验收表要记目标系统里的提交记录。

本机复现

复制下方代码,保存为 erp-write-timeout-replay.py。用 Python 3.10 或更新版本运行 python3 erp-write-timeout-replay.py。脚本只监听本机回环地址,无需安装依赖。三组输出分别对应表中的换号、同键重试与状态回查。

代码语言:python
复制
from http.client import HTTPConnection
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from queue import Queue
from threading import Lock, Thread
from time import sleep
from urllib.parse import unquote

writes = {}
lookup_counts = {}
lock = Lock()
commits = Queue()


class Handler(BaseHTTPRequestHandler):
    def log_message(self, *_):
        pass

    def reply(self, code, body=b""):
        try:
            self.send_response(code)
            self.send_header("Content-Length", str(len(body)))
            self.end_headers()
            self.wfile.write(body)
        except BrokenPipeError:
            pass

    def do_POST(self):
        key = self.headers["Idempotency-Key"]
        payload = self.rfile.read(int(self.headers["Content-Length"]))
        with lock:
            previous = writes.get(key)
            if previous is None:
                writes[key] = payload
                commits.put(key)
        if previous is None and key.startswith("slow-"):
            sleep(.30)
        if previous is not None and previous != payload:
            self.reply(409)
            return
        self.reply(200, payload)

    def do_GET(self):
        key = unquote(self.path.removeprefix("/status/"))
        with lock:
            count = lookup_counts.get(key, 0) + 1
            lookup_counts[key] = count
            payload = writes.get(key)
        if payload is None or count == 1:
            self.reply(404)
        else:
            self.reply(200, payload)


with ThreadingHTTPServer(("127.0.0.1", 0), Handler) as server:
    Thread(target=server.serve_forever, daemon=True).start()

    def api(method, route, key=None, payload=None, timeout=1):
        conn = HTTPConnection(*server.server_address, timeout=timeout)
        try:
            headers = {"Idempotency-Key": key} if key else {}
            conn.request(method, route, body=payload, headers=headers)
            response = conn.getresponse()
            return response.status, response.read()
        finally:
            conn.close()

    def timeout_after_commit(key):
        try:
            api("POST", "/write", key, b"600", timeout=.08)
        except TimeoutError:
            committed = commits.get(timeout=1)
            assert committed == key, (committed, key)
        else:
            raise AssertionError(f"{key} completed before the read timeout")

    timeout_after_commit("slow-blind")
    assert api("POST", "/write", "new-key", b"600") == (200, b"600")
    assert commits.get(timeout=1) == "new-key"
    print("New key after a read timeout: 2 commits")

    timeout_after_commit("slow-same-key")
    assert api("POST", "/write", "slow-same-key", b"600") == (200, b"600")
    assert api("POST", "/write", "slow-same-key", b"601")[0] == 409
    assert commits.empty()
    print("Same key and payload: 1 commit; reuse with changed payload: 409")

    timeout_after_commit("slow-query")
    assert api("GET", "/status/slow-query")[0] == 404
    assert api("GET", "/status/slow-query") == (200, b"600")
    assert commits.empty()
    print("Status first absent then confirmed: 1 commit, no second write")
    server.shutdown()

HTTP 自动重试语义

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 用三个回放看差异
  • 把「待确认」做成验收动作
  • 本机复现
问题归档专栏文章快讯文章归档关键词归档