search

Home  >  Q&A  >  body text

java - web端百度网盘的一个操作为什么要分两次请求服务器, 有什么好处吗

以 文件重命名 为例:

返回如下结果

{
  "errno": 0,
  "info": [],
  "request_id": 88137407060055336,
  "taskid": 307843054247316
}

可以联想到在server端建立了一个task, 并返回了taskid让客户端后续取状态来更新ui

#进行中的返回值
{
  "errno": 0,
  "request_id": 88137707954758994,
  "task_errno": 0,
  "status": "pending"
}

#进行中
{
  "errno": 0,
  "request_id": 88137707954758994,
  "task_errno": 0,
  "status": "running"
}


#操作成功的返回值
{
  "errno": 0,
  "request_id": 88138584419582326,
  "task_errno": 0,
  "status": "success",
  "list": [
    {
      "from": "/test1/我的照片",
      "to": "/test1/我的照片2"
    }
  ],
  "total": 1
}

当 status 为success时候, 则轮询结束, 更新UI元素

问题: 直接访问重命名接口不行吗? 为什么要这么设计, 好处是什么?

迷茫迷茫2802 days ago905

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:50:35

    You have made it very clear. Is there anything else you don’t understand?

    The first time is to initiate a name change application to the server.
    The server will start the task.
    The subsequent polling is to check whether the task is completed, and the front-end will perform corresponding operations after completion. In case of failure, the front-end will also perform rollback operations.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:50:35

    Guess the reason: In extreme cases, the operation may take a long time and cannot be returned immediately. When the operation is completed, the socket link may have been disconnected and the final result cannot be obtained. Designed as a task queue, customers can be guaranteed Get the final result.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:50:35

    In addition to the above considerations, there may be another very important reason, and that is concurrency pressure. Made asynchronous, it can solve concurrency very well

    reply
    0
  • Cancelreply