Kamva / mgm

Mongo Go Models (mgm) is a fast and simple MongoDB ODM for Go (based on official Mongo Go Driver)
Apache License 2.0
754 stars 63 forks source link

Expose client.ping #65

Closed jterryhao closed 2 years ago

jterryhao commented 2 years ago

Is your feature request related to a problem? Please describe. The first request to mongo is slow due to lazy connection. I'd like to use client.Ping() to remove this initial delay. Plus client.Ping() can help to detect mongoDB healthiness. Describe the solution you'd like Some way for library users to call client.Ping()

Describe alternatives you've considered

Additional context Here I did two rounds of CRUD. First create took ~500ms while second only took ~35ms

2022/02/18 15:17:51 init mongo client took 73.7678ms
2022/02/18 15:17:52 create took 517.0204ms
created todo item with id: ObjectID("620ffeefc3185ab9eb7a5824")
2022/02/18 15:17:52 read took 30.3916ms
read todo item: &{DefaultModel:{IDField:{ID:ObjectID("620ffeefc3185ab9eb7a5824")} DateFields:{CreatedAt:2022-02-18 20:17:51.812 +0000 UTC UpdatedAt:2022-02-18 20:17:51.812 +0000 UTC}} Description:Mongo and Redis}
updated todo item: &{DefaultModel:{IDField:{ID:ObjectID("620ffeefc3185ab9eb7a5824")} DateFields:{CreatedAt:2022-02-18 20:17:51.8124566 +0000 UTC UpdatedAt:2022-02-18 20:17:52.3598686 +0000 UTC}} Description:Redis and Mongo}
2022/02/18 15:17:52 update took 33.3613ms
deleted todo item: ObjectID("620ffeefc3185ab9eb7a5824")
2022/02/18 15:17:52 delete took 69.4286ms
2022/02/18 15:17:52 create took 35.5955ms
created todo item with id: ObjectID("620ffef0c3185ab9eb7a5825")
2022/02/18 15:17:52 read took 51.4237ms
read todo item: &{DefaultModel:{IDField:{ID:ObjectID("620ffef0c3185ab9eb7a5825")} DateFields:{CreatedAt:2022-02-18 20:17:52.462 +0000 UTC UpdatedAt:2022-02-18 20:17:52.462 +0000 UTC}} Description:Mongo and Redis}
2022/02/18 15:17:52 update took 34.8029ms
updated todo item: &{DefaultModel:{IDField:{ID:ObjectID("620ffef0c3185ab9eb7a5825")} DateFields:{CreatedAt:2022-02-18 20:17:52.4626585 +0000 UTC UpdatedAt:2022-02-18 20:17:52.5496777 +0000 UTC}} Description:Redis and Mongo}
deleted todo item: ObjectID("620ffef0c3185ab9eb7a5825")
2022/02/18 15:17:52 delete took 69.8679ms
mehran-prs commented 2 years ago

Hi @jterryhao mgm is a wrapper around the mongo go driver, so you have it. You need to something like this:

    _, cli, _, err := mgm.DefaultConfigs()
    if err != nil {
        panic(err)
    }
    err=cli.Ping(context.Background(), nil)

Feel free to reopen the issue if needed, please.