feat(auth): 添加完整的用户认证API项目
- 实现用户注册、登录、JWT令牌认证功能 - 集成Gin、GORM、Viper、Zap等框架 - 添加密码加密、数据库操作、中间件等完整功能 - 配置多环境支持、日志轮转、CORS处理 - 创建完整的项目结构和配置文件体系
This commit is contained in:
35
go并发模型/04go-context-practice/cancel_basic.go
Normal file
35
go并发模型/04go-context-practice/cancel_basic.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 模拟一个可被取消的循环任务
|
||||
func worker(ctx context.Context, name string) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println(name, "收到取消信号:", ctx.Err())
|
||||
return
|
||||
default:
|
||||
fmt.Println(name, "还在干活...")
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
go worker(ctx, "worker-1")
|
||||
go worker(ctx, "worker-2")
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
fmt.Println("main: 决定取消所有 worker")
|
||||
cancel() // 发出取消信号
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Println("main 结束")
|
||||
}
|
||||
42
go并发模型/04go-context-practice/derived_context.go
Normal file
42
go并发模型/04go-context-practice/derived_context.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func subTask(ctx context.Context, name string, d time.Duration) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println(name, "提前被取消:", ctx.Err())
|
||||
case <-time.After(d):
|
||||
fmt.Println(name, "完成,用时", d)
|
||||
}
|
||||
}
|
||||
|
||||
func mainTask(ctx context.Context) {
|
||||
// 从上游 ctx 派生两个子 context
|
||||
ctx1, cancel1 := context.WithCancel(ctx)
|
||||
defer cancel1()
|
||||
|
||||
ctx2, cancel2 := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel2()
|
||||
|
||||
go subTask(ctx1, "subTask-1", 5*time.Second)
|
||||
go subTask(ctx2, "subTask-2", 5*time.Second)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Println("mainTask: 主动取消 subTask-1 的 ctx1")
|
||||
cancel1()
|
||||
|
||||
// 等待一会儿,看 subTask-2 是否因超时被取消
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
|
||||
func main() {
|
||||
root := context.Background()
|
||||
fmt.Println("开始 mainTask...")
|
||||
mainTask(root)
|
||||
fmt.Println("main 结束")
|
||||
}
|
||||
3
go并发模型/04go-context-practice/go.mod
Normal file
3
go并发模型/04go-context-practice/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module go-context-practice
|
||||
|
||||
go 1.22.2
|
||||
37
go并发模型/04go-context-practice/timeout_with_context.go
Normal file
37
go并发模型/04go-context-practice/timeout_with_context.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 模拟一个可能很慢的操作
|
||||
func slowJob(ctx context.Context) error {
|
||||
for i := 1; i <= 5; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("slowJob 被取消:", ctx.Err())
|
||||
return ctx.Err()
|
||||
default:
|
||||
fmt.Println("slowJob 进行中 step", i)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
fmt.Println("slowJob 正常完成")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 最多给 slowJob 3 秒时间
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
fmt.Println("开始执行 slowJob,超时时间 3 秒...")
|
||||
if err := slowJob(ctx); err != nil {
|
||||
fmt.Println("结束,原因:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("结束:正常完成")
|
||||
}
|
||||
Reference in New Issue
Block a user