From a31b6f590f539f7bfaa5d80f03fb40c3b56f322d Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sat, 8 Jul 2023 18:34:53 +0800 Subject: [PATCH] refactor(main): add rate limit (#120) * refactor(main): add rate limit Signed-off-by: cuisongliu * refactor(main): add rate limit Signed-off-by: cuisongliu --------- Signed-off-by: cuisongliu --- .github/workflows/pages.yml | 3 +- helm.sh | 14 ++++++++ library/controller/rate_limiter.go | 58 ++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 helm.sh create mode 100644 library/controller/rate_limiter.go diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 7f300d3..8b55df2 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -41,8 +41,7 @@ jobs: destination: ./_site - name: Helm package run: | - wget https://github.com/labring/endpoints-operator/releases/download/v0.2.1/endpoints-operator-0.2.1.tgz - helm repo index . --url https://github.com/labring/endpoints-operator/releases/download/v0.2.1 + bash helm.sh sudo cp index.yaml _site/ - name: Upload artifact uses: actions/upload-pages-artifact@v1 diff --git a/helm.sh b/helm.sh new file mode 100644 index 0000000..0c65ad3 --- /dev/null +++ b/helm.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# 仓库名称 +repository="labring/endpoints-operator" + +# 获取最新release的版本号 +latest_release=$(curl -s "https://api.github.com/repos/$repository/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') +# 构建下载链接 +download_url="https://github.com/$repository/releases/download/$latest_release/endpoints-operator-${latest_release#v}.tgz" + +# 下载最新release +wget $download_url + +helm repo index . --url https://github.com/$repository/releases/download/$latest_release diff --git a/library/controller/rate_limiter.go b/library/controller/rate_limiter.go new file mode 100644 index 0000000..14649da --- /dev/null +++ b/library/controller/rate_limiter.go @@ -0,0 +1,58 @@ +// Copyright © 2023 sealos. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package controller + +import ( + "flag" + "time" + + "k8s.io/client-go/util/workqueue" + "sigs.k8s.io/controller-runtime/pkg/ratelimiter" +) + +const ( + defaultMinRetryDelay = 750 * time.Millisecond + defaultMaxRetryDelay = 15 * time.Minute + flagMinRetryDelay = "min-retry-delay" + flagMaxRetryDelay = "max-retry-delay" +) + +// RateLimiterOptions used on reconcilers. +type RateLimiterOptions struct { + MinRetryDelay time.Duration + + MaxRetryDelay time.Duration +} + +func (o *RateLimiterOptions) BindFlags(fs *flag.FlagSet) { + fs.DurationVar(&o.MinRetryDelay, flagMinRetryDelay, defaultMinRetryDelay, + "The minimum amount of time for which an object being reconciled will have to wait before a retry.") + fs.DurationVar(&o.MaxRetryDelay, flagMaxRetryDelay, defaultMaxRetryDelay, + "The maximum amount of time for which an object being reconciled will have to wait before a retry.") +} + +func GetRateLimiter(opts RateLimiterOptions) ratelimiter.RateLimiter { + return workqueue.NewItemExponentialFailureRateLimiter( + opts.MinRetryDelay, + opts.MaxRetryDelay) +} + +// GetDefaultRateLimiter +// rate-limiter.RateLimiter with the default configuration. +func GetDefaultRateLimiter() ratelimiter.RateLimiter { + return workqueue.NewItemExponentialFailureRateLimiter( + defaultMinRetryDelay, + defaultMaxRetryDelay) +}