Skip to content

Commit

Permalink
feat: add limit doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Jul 11, 2024
1 parent ce34c21 commit a9a5ffc
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
123 changes: 123 additions & 0 deletions docs/tutorials/syncx/limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# syncx.Limit

`syncx` 包提供了一种管理并发请求的限流机制。通过定义一个 `Limit` 类型,可以控制同时进行的请求数量。

## 变量

### ErrLimitReturn

```go
var ErrLimitReturn = errors.New("discarding limited token, resource pool is full, someone returned multiple times")
```

`ErrLimitReturn` 表示当归还的元素超过借用的数量时,资源池已满的错误信息。

## 类型

### Limit

```go
type Limit struct {
pool chan lang.PlaceholderType
}
```

`Limit` 是一个包含带缓冲通道 `pool` 的结构体,用于控制并发请求的数量。

## 函数

### NewLimit

```go
func NewLimit(n int) Limit
```

`NewLimit` 创建一个可以同时借用 `n` 个元素的 `Limit` 实例。

#### 参数

- `n` (`int`): 可同时借用的元素数量。

#### 返回值

- `Limit`: 一个新的 `Limit` 实例。

### Borrow

```go
func (l Limit) Borrow()
```

`Borrow` 方法以阻塞模式借用一个元素。

### Return

```go
func (l Limit) Return() error
```

`Return` 方法归还借用的资源。如果归还的资源超过借用的数量,则返回错误 `ErrLimitReturn`。

#### 返回值

- `error`: 如果归还成功返回 `nil`,否则返回 `ErrLimitReturn` 错误。

### TryBorrow

```go
func (l Limit) TryBorrow() bool
```

`TryBorrow` 方法以非阻塞模式尝试借用一个元素。

#### 返回值

- `bool`: 如果成功借用,返回 `true`,否则返回 `false`。

## 用法示例

```go
package main

import (
"fmt"
"log"
"syncx"
)

func main() {
limit := syncx.NewLimit(2)

if limit.TryBorrow() {
fmt.Println("Successfully borrowed the first token.")
} else {
fmt.Println("Failed to borrow the first token.")
}

if limit.TryBorrow() {
fmt.Println("Successfully borrowed the second token.")
} else {
fmt.Println("Failed to borrow the second token.")
}

if limit.TryBorrow() {
fmt.Println("Successfully borrowed the third token.")
} else {
fmt.Println("Failed to borrow the third token.")
}

err := limit.Return()
if err != nil {
log.Println("Error returning token:", err)
} else {
fmt.Println("Successfully returned a token.")
}

err = limit.Return()
if err != nil {
log.Println("Error returning token:", err)
} else {
fmt.Println("Successfully returned a token.")
}
}
```
12 changes: 6 additions & 6 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ module.exports = {
label: '指南',
position: 'left',
},
// {
// type: 'doc',
// docId: 'components',
// label: '组件',
// position: 'left',
// },
{
type: 'doc',
docId: 'components',
label: '组件',
position: 'left',
},
{
type: 'doc',
docId: 'reference',
Expand Down
4 changes: 4 additions & 0 deletions i18n/en/docusaurus-theme-classic/navbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"message": "Guides",
"description": "Navbar item with label 指南"
},
"item.label.组件": {
"message": "Components",
"description": "Navbar item with label 组件"
},
"item.label.参考": {
"message": "References",
"description": "Navbar item with label 参考"
Expand Down
4 changes: 4 additions & 0 deletions i18n/zh/docusaurus-theme-classic/navbar.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"message": "指南",
"description": "Navbar item with label 指南"
},
"item.label.组件": {
"message": "组件",
"description": "Navbar item with label 组件"
},
"item.label.参考": {
"message": "参考",
"description": "Navbar item with label 参考"
Expand Down

0 comments on commit a9a5ffc

Please sign in to comment.