簡體   English   中英

Azure storage 'Lease' - 正確的異常方法

[英]Azure storage 'Lease' - correct exception approach

我正在開發 .NET Core web 應用程序,我正在使用 blob 來存儲一些可以在請求期間修改的對象。 我必須防止對一個 object 進行多個並行訪問,因此我將“租用”添加到我的存儲集成中。 在實踐中,當我收到請求時,一個 object 是從帶有租約的 blob 中獲取的。 在請求結束時,此 object 在存儲中更新並刪除租約 - 非常簡單。 但是什么是正確的異常處理呢? 當請求中間發生某些異常時,我再次遇到這個問題,租約沒有被釋放。 我嘗試將發布實施到處置中(在某些 class 中,我控制從 blob 中獲取和租賃)。 但是當拋出未處理的異常時,這不會執行。 添加 try/catch/finally 對我來說似乎不干凈。 我的問題是你知道一些最常見的方法如何根據最終請求釋放租約嗎? 謝謝

在此處輸入圖像描述

根據您的描述,我為您編寫了一個關於租約和中斷租約的簡單演示,只需嘗試以下代碼:

using System;
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Mvc;
using Azure.Storage.Blobs.Specialized;
using System.Threading;

namespace getSasTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class editBlob : ControllerBase
    {
        [HttpGet]
        public string get()
        {
            var connstr = "";
            var container = "";
            var blob = "";
            
            var blobClient = new BlobContainerClient(connstr,container).GetBlobClient(blob);

            var leaseClient = new BlobLeaseClient(blobClient);
            try
            {
                //auto break lease after 15s
                var duration = new TimeSpan(0, 0, 15);
                leaseClient.Acquire(duration, null);

            }
            //if some error occurs, request ends here
            catch (Azure.RequestFailedException e)
            {
               
                if (e.ErrorCode.Equals("LeaseAlreadyPresent"))
                {
                    return "Blob is under process,it will take some time,please try again later";
                }
                else
                {
                    return "some other Azure request errors:"+ e.Message;
                }

            }
            catch (Exception e) { 
                    return "some other errors:" + e.Message;
            }

            //mock time consumption to process blob
            Thread.Sleep(10000);

            //break relase first if process finishs in 15s.
            leaseClient.Break();

            return "Done";
        }
    }
}

因此,根據您的要求,您是否不能授權派對/cron/事件(注冊事件/ttl 處理程序?)在檢測到某些異常情況(無論這對您意味着什么)時終止租約。 看起來你真的很擔心“模式”而不是正確性?

這應該補充正確性。

在實踐中,異常處理策略應該導致足夠的可操作信息。

對於某些人來說,這意味着:

  • E -> E - 1(摘要或無摘要) -> E - 2(摘要或無摘要)..

這樣:

  • E - n:異常,在一定的嵌套層級
  • 摘要:你會傳播這個還是消化並繼續前進?

打破租約(本質上是程序的正確性)不應該意味着你妨礙了你的優雅版本。

每個服務通常是一對服務:

  • 服務本身
  • 服務清理處理程序 1 到 n
  • 服務邊緣案例處理程序 1 到 n
  • 維修底漆
  • 很快...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM