我们可以通过DB.Stats方法获取orm对象的连接池状态。我们来看个示例:

    1. package main
    2. import (
    3. _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
    4. "github.com/gogf/gf/v2/database/gdb"
    5. "github.com/gogf/gf/v2/frame/g"
    6. "github.com/gogf/gf/v2/os/gctx"
    7. )
    8. func main() {
    9. var ctx = gctx.New()
    10. db, err := gdb.New(gdb.ConfigNode{
    11. Link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test",
    12. })
    13. if err != nil {
    14. g.Log().Fatal(ctx, err)
    15. }
    16. err = db.PingMaster()
    17. if err != nil {
    18. g.Log().Fatal(ctx, err)
    19. }
    20. stats := db.Stats(ctx)
    21. g.Dump(stats)
    22. }

    执行后,终端输出如下,可以看到每个链接的数据库节点以及对应的连接池状态信息。

    1. [
    2. {
    3. node: {
    4. Host: "127.0.0.1",
    5. Port: "3306",
    6. User: "root",
    7. Pass: "12345678",
    8. Name: "test",
    9. Type: "mysql",
    10. Link: "",
    11. Extra: "",
    12. Role: "",
    13. Debug: false,
    14. Prefix: "",
    15. DryRun: false,
    16. Weight: 0,
    17. Charset: "utf8",
    18. Protocol: "tcp",
    19. Timezone: "",
    20. Namespace: "",
    21. MaxIdleConnCount: 0,
    22. MaxOpenConnCount: 0,
    23. MaxConnLifeTime: 0,
    24. QueryTimeout: 0,
    25. ExecTimeout: 0,
    26. TranTimeout: 0,
    27. PrepareTimeout: 0,
    28. CreatedAt: "",
    29. UpdatedAt: "",
    30. DeletedAt: "",
    31. TimeMaintainDisabled: false,
    32. },
    33. stats: {
    34. MaxOpenConnections: 0,
    35. OpenConnections: 1,
    36. InUse: 0,
    37. Idle: 1,
    38. WaitCount: 0,
    39. WaitDuration: 0,
    40. MaxIdleClosed: 0,
    41. MaxIdleTimeClosed: 0,
    42. MaxLifetimeClosed: 0,
    43. },
    44. },
    45. ]