Skip to content

Commit

Permalink
refactor(main): add rate limit (#120)
Browse files Browse the repository at this point in the history
* refactor(main): add rate limit

Signed-off-by: cuisongliu <[email protected]>

* refactor(main): add rate limit

Signed-off-by: cuisongliu <[email protected]>

---------

Signed-off-by: cuisongliu <[email protected]>
  • Loading branch information
cuisongliu committed Jul 8, 2023
1 parent 7516af2 commit a31b6f5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions helm.sh
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions library/controller/rate_limiter.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a31b6f5

Please sign in to comment.