go.uber.org/ratelimit 源码分析

go.uber.org/ratelimit 源码分析

go 提供了一用来接口限流的包。其中"go.uber.org/ratelimit" 包正是基于漏桶算法实现的。

使用方式:

  1. 通过 ratelimit.New 创建限流器对象,参数为每秒允许的请求数(RPS)。
  2. 使用 Take() 方法来获取限流许可,该方法会阻塞请求知道满足限速要求。

官方示例:

import (
	"fmt"
	"time"

	"go.uber.org/ratelimit"
)

func main() {
    rl := ratelimit.New(100) // 每秒多少次

    prev := time.Now()
    for i := 0; i < 10; i++ {
        now := rl.Take()	// 平均时间
        fmt.Println(i, now.Sub(prev))
        prev = now
    }

    // Output:
    // 0 0
    // 1 10ms
    // 2 10ms
    // 3 10ms
    // 4 10ms
    // 5 10ms
    // 6 10ms
    // 7 10ms
    // 8 10ms
    // 9 10ms
}

ratelimit.New()指的是每秒平均多少次,在运行程序后,并不会严格按照官方给的样例输出。

源码分析

不仅知其然,还要知其所以然。

最大松弛量

传统的漏桶算法每隔请求的间隔是固定的,然而在实际上的互连网应用中,流量经常是突发性的。对于这种情况,uber引入了最大松弛量的概念。

假如我们要求每秒限定100个请求,平均每个请求间隔 10ms。但是实际情况下,有些间隔比较长,有些间隔比较短。如下图所示:

在这里插入图片描述

请求 1 完成后,15ms 后,请求 2 才到来,可以对请求 2 立即处理。请求 2 完成后,5ms 后,请求 3 到来,这个时候距离上次请求还不足 10ms,因此还需要等待 5ms。

但是,对于这种情况,实际上三个请求一共消耗了 25ms 才完成,并不是预期的 20ms。在 uber-go 实现的 ratelimit 中,可以把之前间隔比较长的请求的时间,匀给后面的使用,保证每秒请求数 (RPS) 即可。

了解完这个前缀知识就可以查看源码了。

New()

ratelimit.New() 内部调用的是 newAtomicInt64Based 方法。

type atomicInt64Limiter struct {
	prepadding [64]byte // 填充字节,确保state独占一个缓存行
	state      int64    // 最后一次权限发送的纳秒时间戳,用于控制请求的速度
	postpadding [56]byte // 填充字节,确保state独占一个缓存行

	perRequest time.Duration	// 限流器放行周期,用于计算下一个权限发送的state的值
	maxSlack   time.Duration	// 最大松弛量
	clock      Clock	// 指向当前时间获取函数的指针
}

// newAtomicBased返回一个新的基于原子的限制器。
func newAtomicInt64Based(rate int, opts ...Option) *atomicInt64Limiter {
	config := buildConfig(opts) // 加载配置,config.per 默认为 1s,config.slack 默认为 10
	perRequest := config.per / time.Duration(rate)
	l := &atomicInt64Limiter{
		perRequest: perRequest,
		maxSlack:   time.Duration(config.slack) * perRequest,	// 默认maxSlack为perRequest 10倍
		clock:      config.clock,
	}
	atomic.StoreInt64(&l.state, 0)	
	return l
}

Take()

// Take blocks to ensure that the time spent between multiple
// Take calls is on average time.Second/rate.
func (t *atomicInt64Limiter) Take() time.Time {
   var (
      newTimeOfNextPermissionIssue int64	// 下一次允许请求的时间
      now                          int64	// 当前时间
   )
   for {
      now = t.clock.Now().UnixNano()	
      timeOfNextPermissionIssue := atomic.LoadInt64(&t.state) // 上一次允许请求时间

      switch {
      case timeOfNextPermissionIssue == 0 || (t.maxSlack == 0 && now-timeOfNextPermissionIssue > int64(t.perRequest)):
        // if this is our first call or t.maxSlack == 0 we need to shrink issue time to now
         newTimeOfNextPermissionIssue = now
      case t.maxSlack > 0 && now-timeOfNextPermissionIssue > int64(t.maxSlack)+int64(t.perRequest):
         // a lot of nanoseconds passed since the last Take call
         // we will limit max accumulated time to maxSlack
         newTimeOfNextPermissionIssue = now - int64(t.maxSlack)
      default:
         // calculate the time at which our permission was issued
         newTimeOfNextPermissionIssue = timeOfNextPermissionIssue + int64(t.perRequest)
      }

      if atomic.CompareAndSwapInt64(&t.state, timeOfNextPermissionIssue, newTimeOfNextPermissionIssue) {
         break
      }
   }

   sleepDuration := time.Duration(newTimeOfNextPermissionIssue - now)
   if sleepDuration > 0 {
      t.clock.Sleep(sleepDuration)
      return time.Unix(0, newTimeOfNextPermissionIssue)
   }
   // return now if we don't sleep as atomicLimiter does
   return time.Unix(0, now)
}

switch 这块挺绕的,刚开始一直以为timeOfNextPermissionIssue 为下次放行的时间戳,这样的话当t.maxSlack = 0时,只要 now-timeOfNextPermissionIssue > 0 就应该放行。无法解释(t.maxSlack == 0 && now-timeOfNextPermissionIssue > int64(t.perRequest))

让我们对上面的三个 case 分析一下

case 1

case timeOfNextPermissionIssue == 0 || (t.maxSlack == 0 && now-timeOfNextPermissionIssue > int64(t.perRequest))

这个比较好理解,我们仍以每秒100个请求为例,平均间隔 10ms。当本次请求时间与上次放行时间 > 时间间隔时即可放行,并记录本次访问时间,如图:

在这里插入图片描述

case 2

case t.maxSlack > 0 && now-timeOfNextPermissionIssue > int64(t.maxSlack)+int64(t.perRequest)

这块比较巧妙,假如松弛量是3 ms,当我们在第二次请求时的时间戳 > 13 ms,此时 newTimeOfNextPermissionIssue= now - maxSlack = 12 ms。

maxSlack 较大且与上次请求相隔较长时,后续的大量请求会被直接放行,以弥补此次浪费的时间。

假设第一次请求时间为0, maxSlack 为 100 ms,perRequest为10 ms,在第二次请求时与第一次间隔为 111 ms ,newTimeOfNextPermissionIssue = 111 - 100 = 11 ms。而 now 为 111 ms,限流器在后面的10次take中都会经过default直接放行,直到 newTimeOfNextPermissionIssue > now

在这里插入图片描述

case 3

对于其它的请求, newTimeOfNextPermissionIssue = timeOfNextPermissionIssue + int64(t.perRequest)

假如maxSlack为 100ms,perRequest 为 10ms,当请求2在15ms访问后,state 更新为 10ms,这样在请求3在20ms访问时,不会出现拦截的情况。

在这里插入图片描述

小结

uber 对基于漏桶实现的 ratelimit 进行了一些优化,让其限流更加的平滑。主要体现在两点:

  1. 本次请求时间距离上次放行时间 > 时间间隔 + 松弛量时,后面10次的请求会根据情况直接放行
  2. 时间间隔 + 松弛量 >= 本次请求时间距离上次放行时间 > 时间间隔state = state + perRequest
<think> 我们正在使用PowerShell的Invoke-WebRequest命令下载文件。 目标URL: https://repo.eclipse.org/content/repositories/lemminx-releases/org/eclipse/lemminx/org.eclipse.lemminx.uber-jar/0.12.0/org.eclipse.lemminx.uber-jar-0.12.0.jar 保存路径: sts-dir/plugins/org.eclipse.lemminx.uber-jar_0.12.0.jar 注意:保存路径中的目录必须存在,如果不存在需要先创建。 步骤: 1. 确定STS安装目录的路径,假设为C:\sts,则完整保存路径为:C:\sts\plugins\org.eclipse.lemminx.uber-jar_0.12.0.jar 2. 使用Invoke-WebRequest下载文件,并使用-OutFile参数指定保存路径。 命令如下: ```powershell # 确保plugins目录存在 $pluginsDir = "C:\sts-dir\plugins" if (-not (Test-Path -Path $pluginsDir)) { New-Item -ItemType Directory -Path $pluginsDir } # 下载文件 Invoke-WebRequest -Uri "https://repo.eclipse.org/content/repositories/lemminx-releases/org/eclipse/lemminx/org.eclipse.lemminx.uber-jar/0.12.0/org.eclipse.lemminx.uber-jar-0.12.0.jar" -OutFile "$pluginsDir\org.eclipse.lemminx.uber-jar_0.12.0.jar" ``` 注意: - 将上面的C:\sts-dir替换为你的实际STS安装目录。 - 如果遇到SSL/TLS安全错误,可以尝试添加`-UseBasicParsing`参数,或者更新PowerShell的TLS版本。 如果PowerShell版本较低(如Windows 7默认的PowerShell 2.0),可能不支持Invoke-WebRequest,我们可以使用System.Net.WebClient替代: ```powershell $pluginsDir = "C:\sts-dir\plugins" if (-not (Test-Path -Path $pluginsDir)) { New-Item -ItemType Directory -Path $pluginsDir } $url = "https://repo.eclipse.org/content/repositories/lemminx-releases/org/eclipse/lemminx/org.eclipse.lemminx.uber-jar/0.12.0/org.eclipse.lemminx.uber-jar-0.12.0.jar" $output = "$pluginsDir\org.eclipse.lemminx.uber-jar_0.12.0.jar" (New-Object System.Net.WebClient).DownloadFile($url, $output) ``` 另外,由于下载的是jar文件,我们需要确保下载的文件没有损坏,下载后可以检查文件大小(通常应约为17MB)或进行校验。 如果在执行WebClient下载时出现安全协议错误,可以尝试在下载前设置安全协议类型(如TLS1.2): ```powershell [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ``` 因此,完整的脚本可能如下(针对PowerShell 2.0及以上): ```powershell # 设置TLS1.2(如果需要) [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $pluginsDir = "C:\sts-dir\plugins" if (-not (Test-Path -Path $pluginsDir)) { New-Item -ItemType Directory -Path $pluginsDir -Force } $url = "https://repo.eclipse.org/content/repositories/lemminx-releases/org/eclipse/lemminx/org.eclipse.lemminx.uber-jar/0.12.0/org.eclipse.lemminx.uber-jar-0.12.0.jar" $output = "$pluginsDir\org.eclipse.lemminx.uber-jar_0.12.0.jar" # 使用WebClient下载 (New-Object System.Net.WebClient).DownloadFile($url, $output) # 检查文件是否存在 if (Test-Path -Path $output) { Write-Host "下载成功!文件保存在 $output" } else { Write-Host "下载失败。" } ``` 请将`C:\sts-dir`替换为你的STS安装目录的实际路径。 </think> ### 使用 Invoke-WebRequest 下载 LemMinX 插件的完整指南 #### 1. 基础下载命令 ```powershell # 确保 plugins 目录存在 if (-not (Test-Path "sts-dir\plugins")) { New-Item -ItemType Directory -Path "sts-dir\plugins" -Force } # 下载插件到指定位置 Invoke-WebRequest -Uri "https://repo.eclipse.org/content/repositories/lemminx-releases/org/eclipse/lemminx/org.eclipse.lemminx.uber-jar/0.12.0/org.eclipse.lemminx.uber-jar-0.12.0.jar" ` -OutFile "sts-dir\plugins\org.eclipse.lemminx.uber-jar_0.12.0.jar" ``` #### 2. 增强下载方案(解决常见问题) ```powershell # HTTPS 安全协议设置(解决 TLS 错误) [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # 带进度显示和重试机制 $retryCount = 0 $maxRetries = 3 do { try { Invoke-WebRequest -Uri "https://repo.eclipse.org/content/repositories/lemminx-releases/org/eclipse/lemminx/org.eclipse.lemminx.uber-jar/0.12.0/org.eclipse.lemminx.uber-jar-0.12.0.jar" ` -OutFile "sts-dir\plugins\org.eclipse.lemminx.uber-jar_0.12.0.jar" ` -UseBasicParsing ` -DisableKeepAlive ` -ErrorAction Stop break } catch { $retryCount++ if ($retryCount -ge $maxRetries) { Write-Error "下载失败:$_" exit 1 } Start-Sleep -Seconds (5 * $retryCount) } } while ($true) ``` #### 3. 验证下载完整性 ```powershell # 检查文件大小(正常应≈17MB) $file = Get-Item "sts-dir\plugins\org.eclipse.lemminx.uber-jar_0.12.0.jar" if ($file.Length -lt 17000000) { Write-Warning "文件大小异常:$($file.Length) 字节,建议重新下载" } # 验证数字签名(可选) Get-AuthenticodeSignature -FilePath $file.FullName ``` #### 4. 手动安装后的插件激活 1. **重建 Eclipse 插件索引**: ```powershell Start-Process "sts-dir\SpringToolSuite4.exe" -ArgumentList "-clean -clearPersistedState" ``` 2. **验证安装**: 启动 STS 后创建测试 XML 文件,检查语法高亮和代码补全功能是否恢复正常。 > **注意**:Windows 7 默认 PowerShell 版本(2.0)可能不支持某些参数,若遇到错误建议: > 1. 升级到 PowerShell 5.1([下载链接](https://www.microsoft.com/en-us/download/details.aspx?id=54616)) > 2. 使用备用命令: > ```powershell > (New-Object System.Net.WebClient).DownloadFile( > "https://repo.eclipse.org/.../org.eclipse.lemminx.uber-jar-0.12.0.jar", > "sts-dir\plugins\org.eclipse.lemminx.uber-jar_0.12.0.jar" > ) > ```[^2]
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值