亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

使用SimpleTimeLimiter進(jìn)行方法調(diào)用超時(shí)控制

zhonghanwen / 1096人閱讀

原理

利用Executors的Future的限時(shí)get方法,Google的SimpleTimeLimiter本質(zhì)上是對(duì)Executors的Future的包裝。

實(shí)例
package com.facebook.presto.raptor.backup;

import com.facebook.presto.spi.PrestoException;
import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import com.google.common.util.concurrent.UncheckedTimeoutException;
import io.airlift.units.Duration;

import javax.annotation.PreDestroy;

import java.io.File;
import java.util.OptionalLong;
import java.util.UUID;
import java.util.concurrent.ExecutorService;

import static com.facebook.presto.raptor.RaptorErrorCode.RAPTOR_BACKUP_TIMEOUT;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

public class TimeoutBackupStore
        implements BackupStore
{
    private final ExecutorService executor;
    private final BackupStore store;

    public TimeoutBackupStore(BackupStore store, String connectorId, Duration timeout)
    {
        requireNonNull(store, "store is null");
        requireNonNull(connectorId, "connectorId is null");
        requireNonNull(timeout, "timeout is null");
        this.executor = newCachedThreadPool(daemonThreadsNamed("backup-proxy-" + connectorId + "-%s"));
        this.store = timeLimited(store, BackupStore.class, timeout, executor);
    }

    @PreDestroy
    public void shutdown()
    {
        executor.shutdownNow();
    }

    @Override
    public void backupShard(UUID uuid, File source)
    {
        try {
            store.backupShard(uuid, source);
        }
        catch (UncheckedTimeoutException e) {
            throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard backup timed out");
        }
    }

    @Override
    public void restoreShard(UUID uuid, File target)
    {
        try {
            store.restoreShard(uuid, target);
        }
        catch (UncheckedTimeoutException e) {
            throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard restore timed out");
        }
    }

    @Override
    public OptionalLong shardSize(UUID uuid)
    {
        try {
            return store.shardSize(uuid);
        }
        catch (UncheckedTimeoutException e) {
            throw new PrestoException(RAPTOR_BACKUP_TIMEOUT, "Shard backup size fetch timed out");
        }
    }

    private static  T timeLimited(T target, Class clazz, Duration timeout, ExecutorService executor)
    {
        TimeLimiter limiter = new SimpleTimeLimiter(executor);
        return limiter.newProxy(target, clazz, timeout.toMillis(), MILLISECONDS);
    }
}
參考

SimpleTimeLimiterShowcase

TimeoutBackupStore

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/65786.html

相關(guān)文章

  • Dubbo 源碼分析20 Dubbo服務(wù)提供者、服務(wù)消費(fèi)者并發(fā)度控制機(jī)制

    摘要:代碼根據(jù)服務(wù)提供者和服務(wù)調(diào)用方法名,獲取。代碼根據(jù)服務(wù)提供者配置的最大并發(fā)度,創(chuàng)建該服務(wù)該方法對(duì)應(yīng)的信號(hào)量對(duì)象??偨Y(jié)是控制消費(fèi)端對(duì)單個(gè)服務(wù)提供者單個(gè)服務(wù)允許調(diào)用的最大并發(fā)度。 本文將詳細(xì)分析< dubbo:service executes=/>與< dubbo:reference actives = />的實(shí)現(xiàn)機(jī)制,深入探...

    不知名網(wǎng)友 評(píng)論0 收藏0
  • 分布式熔斷降級(jí)平臺(tái)aegis

    摘要:現(xiàn)狀分布式場(chǎng)景中。因此要對(duì)在原服務(wù)不可用時(shí)進(jìn)行熔斷降級(jí)處理。分析熔斷降級(jí)可以服務(wù)端限流網(wǎng)關(guān)限流客戶端限流。它提供兩種資源隔離的模式信號(hào)量隔離和線程池隔離。支持流控熔斷降級(jí)系統(tǒng)保護(hù)等。它支持并發(fā)數(shù)的流量控制也支持熔斷降級(jí)。 現(xiàn)狀 分布式場(chǎng)景中。若服務(wù)不穩(wěn)定,會(huì)導(dǎo)致調(diào)用方服務(wù)也不可用,從而造成雪崩效應(yīng)。因此要對(duì)在原服務(wù)不可用時(shí)進(jìn)行熔斷降級(jí)處理。 分析 熔斷降級(jí)可以服務(wù)端限流、網(wǎng)關(guān)限流、客戶...

    ixlei 評(píng)論0 收藏0
  • 手撕ThreadPoolExecutor線程池源碼

    摘要:所以,在時(shí)執(zhí)行也是為了保證線程池在狀態(tài)下必須要有一個(gè)線程來執(zhí)行任務(wù)。 這篇文章對(duì)ThreadPoolExecutor創(chuàng)建的線程池如何操作線程的生命周期通過源碼的方式進(jìn)行詳細(xì)解析。通過對(duì)execute方法、addWorker方法、Worker類、runWorker方法、getTask方法、processWorkerExit從源碼角度詳細(xì)闡述,文末有彩蛋。 exexcte方法 public...

    Corwien 評(píng)論0 收藏0
  • HttpInterceptor 攔截器 - 網(wǎng)絡(luò)請(qǐng)求超時(shí)與重試的簡(jiǎn)單實(shí)現(xiàn)

    摘要:對(duì)象表示攔截器鏈表中的下一個(gè)攔截器。至此,攔截器只會(huì)再重試到最大次數(shù)還是失敗的情況下拋出超時(shí)錯(cuò)誤。完成上述步驟,一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)請(qǐng)求超時(shí)與重試的攔截器便實(shí)現(xiàn)了。 ... 攔截器在Angular項(xiàng)目中其實(shí)有著十分重要的地位,攔截器可以統(tǒng)一對(duì) HTTP 請(qǐng)求進(jìn)行攔截處理,我們可以在每個(gè)請(qǐng)求體或者響應(yīng)后對(duì)應(yīng)的流添加一系列動(dòng)作或者處理數(shù)據(jù),再返回給使用者調(diào)用。 每個(gè) API 調(diào)用的時(shí)候都不可避免...

    stonezhu 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<