# mingo **Repository Path**: go-ming/mingo ## Basic Information - **Project Name**: mingo - **Description**: mingo - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-01 - **Last Updated**: 2026-03-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Mingo ## Feature - EventBus - Router - RedisDelay - RedisMq - RedisMutex ## EventBus ```go type TestContext struct{} func onBusResult(ctx *TestContext, event string, data any, allSuccess bool, details []BusEventResult) { fmt.Println(data, allSuccess, details) } func TestBus(t *testing.T) { bus := NewBus(onBusResult) // 测试正常处理事件 bus.On("event", "create", func(ctx *TestContext, event string, ev any) error { t.Log(event, "create", ev) return nil }) // 测试事件失败情况 bus.On("event", "update", func(ctx *TestContext, event string, ev any) error { t.Log("update", ev) return errors.New("fail") }) ctx := new(TestContext) bus.Emit(ctx, "event", "hello") bus.EmitPart(ctx, "event", "hello", []string{"update"}) } ``` ## Router ```go type MyContext struct { Context Uid int } func testController(ctx *MyContext) (any, error) { var req struct { A int `json:"a" validate:"required"` B int `json:"b" validate:"required"` } var res struct { C int `json:"c"` } if err := ctx.GetBody(&req); err != nil { fmt.Println(err) return nil, err } fmt.Println("uid:", ctx.Uid) res.C = req.A + req.B return res, nil } func TestRouter(t *testing.T) { var res any var err error // 创建路由 router := NewRouter[MyContext]() // 注册 controller router.On("test", testController, 1) // 构建上下文 var ctx MyContext ctx.Uid = 10 ctx.RawData = []byte("{\"a\":1,\"b\":2}") perm, _ := router.GetPerm("test") fmt.Println("perm:", perm) // 触发路由 if res, err = router.Invoke("test", &ctx); err != nil { t.Error(err) } // 检测结果 var c = res.(struct { C int `json:"c"` }).C if c != 3 { t.Log("C is ", c) t.Error("error result") return } // test non exist action if _, err = router.GetPerm("nonexistaction"); err == nil { t.Error("has not check out invalid action") } if _, err = router.Invoke("nonexistaction", &ctx); err == nil { t.Error("has not check out invalid action") } } ``` ## RedisDelay ```go func TestDelay(t *testing.T) { options := redis.Options{ Addr: "localhost:6379", DB: 0, } rdb := redis.NewClient(&options) // rdb.con delay := NewRedisDelay(rdb, "test") delay.On("hello", func(id string) { fmt.Println("hello", id) }) delay.On("kkkk", func(id string) { fmt.Println("kkk", id) }) delay.SetTimeout("hello", "123", time.Second*2) delay.SetTimeout("kkkk", "222", time.Second*3) delay.Start(time.Second) } ``` ## RedisMutex ```go var rdb = redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 0}) var mutex = NewRedisMutex(rdb) func getLock(id string) { mutex.Lock("test") defer mutex.Unlock("test") fmt.Println("lock", id, time.Now()) time.Sleep(time.Second * 3) fmt.Println("unlock", id, time.Now()) } func TestMutex(t *testing.T) { fmt.Println("test mutex") go getLock("1") go getLock("2") time.Sleep(time.Second * 10) } ``` ## RedisMq ```go func onChan1(id string, data string) { fmt.Println("onChan1", id, data) } func onChan2(id string, data string) { fmt.Println("onChan2", id, data) } func TestMq(t *testing.T) { rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) mq := NewRedisMq(rdb, "test", "test", time.Second) mq.On("chan1", onChan1) mq.On("chan2", onChan2) go func() { time.Sleep(time.Second) mq.Emit("chan1", "chan1 msg1") mq.Emit("chan2", "chan2 msg1") time.Sleep(time.Second) mq.Close() }() mq.Start() fmt.Println("test done") } ```