国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Kubernetes GoRoutineMap工具包代碼詳解

這篇具有很好參考價(jià)值的文章主要介紹了Kubernetes GoRoutineMap工具包代碼詳解。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

1、概述

GoRoutineMap 定義了一種類型,可以運(yùn)行具有名稱的 goroutine 并跟蹤它們的狀態(tài)。它防止創(chuàng)建具有相同名稱的多個(gè)goroutine,并且在上一個(gè)具有該名稱的 goroutine 完成后的一段退避時(shí)間內(nèi)可能阻止重新創(chuàng)建 goroutine。

使用GoRoutineMap場(chǎng)景:

  • 使用協(xié)程的方式運(yùn)行函數(shù)邏輯,如果函數(shù)成功執(zhí)行,則退出該協(xié)程;如果函數(shù)執(zhí)行報(bào)錯(cuò),在指數(shù)退避的時(shí)間內(nèi)禁止再次執(zhí)行該函數(shù)邏輯。

使用GoRoutineMap大體步驟如下:

1)通過goRoutineMap.NewGoRoutineMap(exponentialBackOffOnError bool) GoRoutineMap {....}方法創(chuàng)建GoRoutineMap結(jié)構(gòu)體對(duì)象,用于管理goroutine 并跟蹤它們的狀態(tài);

2)調(diào)用GoRoutineMap結(jié)構(gòu)體對(duì)象Run(operationName, operation)方法,其能夠防止創(chuàng)建具有相同名稱的多個(gè)goroutine,并使用協(xié)程的方式運(yùn)行函數(shù)邏輯,如果函數(shù)成功執(zhí)行,則退出該協(xié)程;如果函數(shù)執(zhí)行報(bào)錯(cuò),在指數(shù)退避的時(shí)間內(nèi)禁止再次執(zhí)行該函數(shù)邏輯。

注意 1:本文代碼基于Kubernetes 1.24.10版本,包路徑kubernetes-1.24.10/pkg/util/goroutinemap/goroutinemap.go。

注意 2:概述中涉及的代碼會(huì)在下文進(jìn)行詳細(xì)解釋。

2、goroutinemap工具包代碼詳解

2.1 相關(guān)類型詳解

GoRoutineMap工具包接口定義:

type GoRoutineMap interface {
	// Run adds operation name to the list of running operations and spawns a
	// new go routine to execute the operation.
	// If an operation with the same operation name already exists, an
	// AlreadyExists or ExponentialBackoff error is returned.
	// Once the operation is complete, the go routine is terminated and the
	// operation name is removed from the list of executing operations allowing
	// a new operation to be started with the same operation name without error.
	Run(operationName string, operationFunc func() error) error

	// Wait blocks until operations map is empty. This is typically
	// necessary during tests - the test should wait until all operations finish
	// and evaluate results after that.
	Wait()

	// WaitForCompletion blocks until either all operations have successfully completed
	// or have failed but are not pending. The test should wait until operations are either
	// complete or have failed.
	WaitForCompletion()

	IsOperationPending(operationName string) bool
}

goRoutineMap結(jié)構(gòu)體實(shí)現(xiàn)GoRoutineMap接口,定義如下:

// goRoutineMap結(jié)構(gòu)體實(shí)現(xiàn)GoRoutineMap接口,
type goRoutineMap struct {
	// 用于記錄goRoutineMap維護(hù)協(xié)程的狀態(tài)
	operations                map[string]operation
	// 發(fā)生錯(cuò)誤時(shí)是否指數(shù)級(jí)補(bǔ)償
	exponentialBackOffOnError bool
	// 用在多個(gè) Goroutine 等待,一個(gè) Goroutine 通知(事件發(fā)生)的場(chǎng)景
	cond                      *sync.Cond
	lock                      sync.RWMutex
}

// operation結(jié)構(gòu)體對(duì)象維護(hù)單個(gè)goroutine的狀態(tài)。
type operation struct {
	// 是否操作掛起
	operationPending bool
	// 單個(gè)goroutine執(zhí)行邏輯報(bào)錯(cuò)時(shí),實(shí)現(xiàn)以指數(shù)退避方式
	expBackoff       exponentialbackoff.ExponentialBackoff
}

ExponentialBackoff結(jié)構(gòu)體包含最后一次出現(xiàn)的錯(cuò)誤、最后一次出現(xiàn)錯(cuò)誤的時(shí)間以及不允許重試的持續(xù)時(shí)間。

// ExponentialBackoff contains the last occurrence of an error and the duration
// that retries are not permitted.
type ExponentialBackoff struct {
	lastError           error
	lastErrorTime       time.Time
	durationBeforeRetry time.Duration
}

2.2?GoRoutineMap結(jié)構(gòu)體對(duì)象初始化

通過goRoutineMap.NewGoRoutineMap方法創(chuàng)建GoRoutineMap結(jié)構(gòu)體對(duì)象,用于管理goroutine 并跟蹤它們的狀態(tài);  

// NewGoRoutineMap returns a new instance of GoRoutineMap.
func NewGoRoutineMap(exponentialBackOffOnError bool) GoRoutineMap {
	g := &goRoutineMap{
		operations:                make(map[string]operation),
		exponentialBackOffOnError: exponentialBackOffOnError,
	}

	g.cond = sync.NewCond(&g.lock)
	return g
}

2.3? GoRoutineMap.Run方法代碼詳解

調(diào)用GoRoutineMap結(jié)構(gòu)體對(duì)象Run(operationName, operation)方法,其能夠防止創(chuàng)建具有相同名稱的多個(gè)goroutine,并使用協(xié)程的方式運(yùn)行函數(shù)邏輯,如果函數(shù)成功執(zhí)行,則退出該協(xié)程;如果函數(shù)執(zhí)行報(bào)錯(cuò),在指數(shù)退避的時(shí)間內(nèi)禁止再次執(zhí)行該函數(shù)邏輯。

// Run函數(shù)是外部函數(shù),是goRoutineMap核心方法,其能夠防止創(chuàng)建具有相同名稱的多個(gè)goroutine,并使用協(xié)程的方式運(yùn)行函數(shù)邏輯
// 如果函數(shù)成功執(zhí)行,則退出該協(xié)程;如果函數(shù)執(zhí)行報(bào)錯(cuò),在指數(shù)退避的時(shí)間內(nèi)禁止再次執(zhí)行該函數(shù)邏輯。
func (grm *goRoutineMap) Run(
	operationName string,
	operationFunc func() error) error {
	grm.lock.Lock()
	defer grm.lock.Unlock()

	// 判斷grm.operations這個(gè)map中是否存在具有operationName名稱的協(xié)程
	existingOp, exists := grm.operations[operationName]
	if exists {
		// 如果grm.operations這個(gè)map中已經(jīng)存在operationName名稱的協(xié)程,并且existingOp.operationPending==true,說明grm.operations中operationName名稱這個(gè)協(xié)程正在執(zhí)行函數(shù)邏輯,在這期間又有一個(gè)同名的
		// operationName希望加入grm.operations這個(gè)map,此時(shí)加入map失敗并報(bào)AlreadyExistsError錯(cuò)誤
		if existingOp.operationPending {
			return NewAlreadyExistsError(operationName)
		}

		// 到這步說明名稱為operationName名稱的協(xié)程執(zhí)行函數(shù)邏輯失敗,此時(shí)判斷此協(xié)程最后一次失敗時(shí)間 + 指數(shù)退避的時(shí)間 >= 當(dāng)前時(shí)間,如果不符合條件的話禁止執(zhí)行該協(xié)程函數(shù)邏輯。
		if err := existingOp.expBackoff.SafeToRetry(operationName); err != nil {
			return err
		}
	}

	// 如果grm.operations這個(gè)map中不存在operationName名稱的協(xié)程 或者 此協(xié)程最后一次失敗時(shí)間 + 指數(shù)退避的時(shí)間 < 當(dāng)前時(shí)間,則在grm.operations這個(gè)map中重新維護(hù)此協(xié)程(注意,operationPending=true)
	grm.operations[operationName] = operation{
		operationPending: true,
		expBackoff:       existingOp.expBackoff,
	}

	// 以協(xié)程方式執(zhí)行函數(shù)邏輯operationFunc()
	go func() (err error) {
		// 捕獲崩潰并記錄錯(cuò)誤,默認(rèn)不傳參的話,在程序發(fā)送崩潰時(shí),在控制臺(tái)打印一下崩潰日志后再崩潰,方便技術(shù)人員排查程序錯(cuò)誤。
		defer k8sRuntime.HandleCrash()

		// 如果執(zhí)行operationFunc()函數(shù)邏輯不報(bào)錯(cuò)或者grm.exponentialBackOffOnError=false的話,將從grm.operations這個(gè)map中移除此operationName名稱協(xié)程;
		// 如果執(zhí)行operationFunc()函數(shù)邏輯報(bào)錯(cuò)并且grm.exponentialBackOffOnError=true,則將產(chǎn)生指數(shù)級(jí)補(bǔ)償,到達(dá)補(bǔ)償時(shí)間后才能再調(diào)用此operationName名稱協(xié)程的函數(shù)邏輯
		// Handle completion of and error, if any, from operationFunc()
		defer grm.operationComplete(operationName, &err)
		// 處理operationFunc()函數(shù)發(fā)生的panic錯(cuò)誤,以便defer grm.operationComplete(operationName, &err)能執(zhí)行
		// Handle panic, if any, from operationFunc()
		defer k8sRuntime.RecoverFromPanic(&err)
		return operationFunc()
	}()

	return nil
}

如果給定lastErrorTime的durationBeforeRetry周期尚未過期,則SafeToRetry返回錯(cuò)誤。否則返回零。

// SafeToRetry returns an error if the durationBeforeRetry period for the given
// lastErrorTime has not yet expired. Otherwise it returns nil.
func (expBackoff *ExponentialBackoff) SafeToRetry(operationName string) error {
	if time.Since(expBackoff.lastErrorTime) <= expBackoff.durationBeforeRetry {
		return NewExponentialBackoffError(operationName, *expBackoff)
	}

	return nil
}

operationComplete是一個(gè)內(nèi)部函數(shù),用于處理在goRoutineMap中已經(jīng)運(yùn)行完函數(shù)邏輯的協(xié)程。

// operationComplete是一個(gè)內(nèi)部函數(shù),用于處理在goRoutineMap中已經(jīng)運(yùn)行完函數(shù)邏輯的協(xié)程
// 如果執(zhí)行operationFunc()函數(shù)邏輯不報(bào)錯(cuò)或者grm.exponentialBackOffOnError=false的話,將從grm.operations這個(gè)map中移除此operationName名稱協(xié)程;
// 如果執(zhí)行operationFunc()函數(shù)邏輯報(bào)錯(cuò)并且grm.exponentialBackOffOnError=true,則將產(chǎn)生指數(shù)級(jí)補(bǔ)償,到達(dá)補(bǔ)償時(shí)間后才能再調(diào)用此operationName名稱協(xié)程的函數(shù)邏輯
// operationComplete handles the completion of a goroutine run in the
// goRoutineMap.
func (grm *goRoutineMap) operationComplete(
	operationName string, err *error) {
	// Defer operations are executed in Last-In is First-Out order. In this case
	// the lock is acquired first when operationCompletes begins, and is
	// released when the method finishes, after the lock is released cond is
	// signaled to wake waiting goroutine.
	defer grm.cond.Signal()
	grm.lock.Lock()
	defer grm.lock.Unlock()

	if *err == nil || !grm.exponentialBackOffOnError {
		// 函數(shù)邏輯執(zhí)行完成無錯(cuò)誤或已禁用錯(cuò)誤指數(shù)級(jí)補(bǔ)償,將從grm.operations這個(gè)map中移除此operationName名稱協(xié)程;
		// Operation completed without error, or exponentialBackOffOnError disabled
		delete(grm.operations, operationName)
		if *err != nil {
			// Log error
			klog.Errorf("operation for %q failed with: %v",
				operationName,
				*err)
		}
	} else {
		//  函數(shù)邏輯執(zhí)行完成有錯(cuò)誤則將產(chǎn)生指數(shù)級(jí)補(bǔ)償,到達(dá)補(bǔ)償時(shí)間后才能再調(diào)用此operationName名稱協(xié)程的函數(shù)邏輯(注意,指數(shù)補(bǔ)充的協(xié)程,operationPending=false)
		// Operation completed with error and exponentialBackOffOnError Enabled
		existingOp := grm.operations[operationName]
		existingOp.expBackoff.Update(err)
		existingOp.operationPending = false
		grm.operations[operationName] = existingOp

		// Log error
		klog.Errorf("%v",
			existingOp.expBackoff.GenerateNoRetriesPermittedMsg(operationName))
	}
}

Update是一個(gè)外部函數(shù),用于計(jì)算指數(shù)級(jí)別的退避時(shí)間。

const (
   initialDurationBeforeRetry time.Duration = 500 * time.Millisecond
   maxDurationBeforeRetry time.Duration = 2*time.Minute + 2*time.Second
)
func (expBackoff *ExponentialBackoff) Update(err *error) {
	if expBackoff.durationBeforeRetry == 0 {
		expBackoff.durationBeforeRetry = initialDurationBeforeRetry
	} else {
		expBackoff.durationBeforeRetry = 2 * expBackoff.durationBeforeRetry
		if expBackoff.durationBeforeRetry > maxDurationBeforeRetry {
			expBackoff.durationBeforeRetry = maxDurationBeforeRetry
		}
	}

	expBackoff.lastError = *err
	expBackoff.lastErrorTime = time.Now()
}

3、總結(jié)

本文對(duì)Kubernetes GoRoutineMap工具包代碼進(jìn)行了詳解,通過?GoRoutineMap工具包能夠防止創(chuàng)建具有相同名稱的多個(gè)goroutine,并使用協(xié)程的方式運(yùn)行函數(shù)邏輯,如果函數(shù)成功執(zhí)行,則退出該協(xié)程;如果函數(shù)執(zhí)行報(bào)錯(cuò),在指數(shù)退避的時(shí)間內(nèi)禁止再次執(zhí)行該函數(shù)邏輯。使用Kubernetes GoRoutineMap包的好處包括以下幾點(diǎn):

  1. 減輕負(fù)載:當(dāng)出現(xiàn)錯(cuò)誤時(shí),使用指數(shù)退避時(shí)間可以避免過于頻繁地重新嘗試操作,從而減輕系統(tǒng)的負(fù)載。指數(shù)退避時(shí)間通過逐漸增加重試之間的等待時(shí)間,有效地減少了對(duì)系統(tǒng)資源的過度使用。

  2. 提高穩(wěn)定性:通過逐漸增加重試之間的等待時(shí)間,指數(shù)退避時(shí)間可以幫助應(yīng)對(duì)瞬時(shí)的故障或錯(cuò)誤。這種策略使得系統(tǒng)能夠在短時(shí)間內(nèi)自動(dòng)恢復(fù),并逐漸增加重試頻率,直到操作成功為止。這有助于提高應(yīng)用程序的穩(wěn)定性和可靠性。

  3. 降低網(wǎng)絡(luò)擁塞:當(dāng)網(wǎng)絡(luò)出現(xiàn)擁塞時(shí),頻繁地進(jìn)行重試可能會(huì)加重?fù)砣麊栴}并影響其他任務(wù)的正常運(yùn)行。指數(shù)退避時(shí)間通過增加重試之間的等待時(shí)間,可以降低對(duì)網(wǎng)絡(luò)的額外負(fù)載,有助于緩解網(wǎng)絡(luò)擁塞問題。

  4. 避免過早放棄:某些錯(cuò)誤可能是瞬時(shí)的或暫時(shí)性的,因此過早放棄重試可能會(huì)導(dǎo)致不必要的失敗。指數(shù)退避時(shí)間確保了在錯(cuò)誤發(fā)生時(shí)進(jìn)行適當(dāng)?shù)闹卦嚕员阆到y(tǒng)有更多機(jī)會(huì)恢復(fù)并成功完成操作。

綜上所述,使用Kubernetes GoRoutineMap工具包以協(xié)程方式處理函數(shù)邏輯可以提高系統(tǒng)的可靠性、穩(wěn)定性和性能,減輕負(fù)載并有效應(yīng)對(duì)錯(cuò)誤和故障情況。這是在Kubernetes中實(shí)施的一種常見的重試策略,常用于處理容器化應(yīng)用程序中的操作錯(cuò)誤。文章來源地址http://www.zghlxwxcb.cn/news/detail-463636.html

到了這里,關(guān)于Kubernetes GoRoutineMap工具包代碼詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 使用OpenCV工具包成功實(shí)現(xiàn)人臉檢測(cè)與人臉識(shí)別,包括傳統(tǒng)視覺和深度學(xué)習(xí)方法(附完整代碼,模型下載......)

    使用OpenCV工具包成功實(shí)現(xiàn)人臉檢測(cè)與人臉識(shí)別,包括傳統(tǒng)視覺和深度學(xué)習(xí)方法(附完整代碼,模型下載......)

    要實(shí)現(xiàn)人臉識(shí)別功能,首先要進(jìn)行人臉檢測(cè),判斷出圖片中人臉的位置,才能進(jìn)行下一步的操作。 參考鏈接: 1、OpenCV人臉檢測(cè) 2、【OpenCV-Python】32.OpenCV的人臉檢測(cè)和識(shí)別——人臉檢測(cè) 3、【youcans 的圖像處理學(xué)習(xí)課】23. 人臉檢測(cè):Haar 級(jí)聯(lián)檢測(cè)器 4、OpenCV實(shí)戰(zhàn)5:LBP級(jí)聯(lián)分類器

    2024年02月08日
    瀏覽(29)
  • 儀酷LabVIEW AI視覺工具包及開放神經(jīng)網(wǎng)絡(luò)交互工具包常見問題解答

    儀酷LabVIEW AI視覺工具包及開放神經(jīng)網(wǎng)絡(luò)交互工具包常見問題解答

    哈嘍,各位朋友,好久不見~ 之前給大家分享了基于LabVIEW開發(fā)的AI視覺工具包及開放神經(jīng)網(wǎng)絡(luò)交互工具包,不少朋友私信說在安裝和使用過程中會(huì)遇到一些問題,今天我們就集中回復(fù)一下大家問到最多的問題。如果大家在使用過程中還有其他問題,可以補(bǔ)充到評(píng)論區(qū),我們這

    2024年02月16日
    瀏覽(23)
  • GIS工具包

    GIS工具包,根據(jù)jts工具,結(jié)合實(shí)際使用場(chǎng)景提取出來的常用工具集合;涵蓋幾何格式轉(zhuǎn)換(WKT,GeoJSON等)與geometry轉(zhuǎn)換、gis距離計(jì)算、度距離單位換算、角度計(jì)算、buffer運(yùn)算、映射截取、幾何穿串等操作 gis-tools源碼庫地址 1.1 WktTool使用說明 wkt格式與geometry互轉(zhuǎn); wkt轉(zhuǎn)geometry操作

    2024年02月06日
    瀏覽(123)
  • 學(xué)習(xí)筆記-JVM-工具包(JVM分析工具)

    學(xué)習(xí)筆記-JVM-工具包(JVM分析工具)

    常用工具 JDK工具 ① jps: JVM Process status tool:JVM進(jìn)程狀態(tài)工具,查看進(jìn)程基本信息 ② jstat: JVM statistics monitoring tool : JVM統(tǒng)計(jì)監(jiān)控工具,查看堆,GC詳細(xì)信息 ③ jinfo:Java Configuration Info :查看配置參數(shù)信息,支持部分參數(shù)運(yùn)行時(shí)修改 ④ jmap:Java Memory Map :分析堆內(nèi)存工具,du

    2024年02月13日
    瀏覽(27)
  • MATLAB添加工具包(詳細(xì))

    MATLAB添加工具包(詳細(xì))

    我這里要添加的文件包為:DeepLearnToolbox-master 我這里的安裝目錄是:D:softwareMATLABtoolbox (1)以中文版為例,在主界面找到“設(shè)置路徑”按鈕 (2)點(diǎn)擊“設(shè)置路徑”,彈出設(shè)置界面 第一步:點(diǎn)“添加文件夾” (注:如果要工具包中有多個(gè)子文件夾,則點(diǎn)“添加并包含子文

    2024年02月02日
    瀏覽(21)
  • 【Linux】基本開發(fā)工具包使用

    【Linux】基本開發(fā)工具包使用

    目錄 一, yum ——linux軟件包管理器 ?1. 軟件包是啥子? ?2.? yum基本使用? 1. 步驟:? 2. 開發(fā)工具推薦(centos 7.6) 二,vim —— linux文本編輯器 1. Normal mode? ——? 命令模式(記不住沒關(guān)系,多練就行) 2.? last line? mode——? ?末行模式 (如何進(jìn)入;shift :) 3.?Insert mode ——插

    2024年02月08日
    瀏覽(29)
  • Hardhat工具包1--安裝使用

    Hardhat工具包1--安裝使用

    參考資料: 官方文檔 : https://hardhat.org/getting-started/ https://hardhat.org/hardhat-runner/docs/getting-started#overview 基于Hardhat和Openzeppelin開發(fā)可升級(jí)合約(一) 基于Hardhat和Openzeppelin開發(fā)可升級(jí)合約(一)_灬倪先森_的博客-CSDN博客 ---------------------------------------------------------------------------------

    2023年04月11日
    瀏覽(25)
  • Windows11滲透工具包分享

    Windows11滲透工具包分享

    ? ? ? ? ? 項(xiàng)目地址 下載地址

    2024年02月13日
    瀏覽(20)
  • Quanto: PyTorch 量化工具包

    量化技術(shù)通過用低精度數(shù)據(jù)類型 (如 8 位整型 (int8)) 來表示深度學(xué)習(xí)模型的權(quán)重和激活,以減少傳統(tǒng)深度學(xué)習(xí)模型使用 32 位浮點(diǎn) (float32) 表示權(quán)重和激活所帶來的計(jì)算和內(nèi)存開銷。 減少位寬意味著模型的內(nèi)存占用更低,這對(duì)在消費(fèi)設(shè)備上部署大語言模型至關(guān)重要。量化技術(shù)也

    2024年04月10日
    瀏覽(31)
  • NetAssist網(wǎng)絡(luò)調(diào)試工具使用指南 (附NetAssist工具包)

    1、NetAssist簡(jiǎn)介 NetAssist網(wǎng)絡(luò)調(diào)試助手,是Windows平臺(tái)下開發(fā)的TCP/IP網(wǎng)絡(luò)調(diào)試工具,集TCP/UDP服務(wù)端及客戶端于一體,是網(wǎng)絡(luò)應(yīng)用開發(fā)及調(diào)試工作必備的專業(yè)工具之一,可以幫助網(wǎng)絡(luò)應(yīng)用設(shè)計(jì)、開發(fā)、測(cè)試人員檢查所開發(fā)的網(wǎng)絡(luò)應(yīng)用軟/硬件產(chǎn)品的數(shù)據(jù)收發(fā)狀況,提高開發(fā)速度,簡(jiǎn)

    2024年02月16日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包