-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow disabling of automatic Ping #1141
Conversation
@jinzhu Any issues with this? |
Hi @pjebs What's the problem of send another I think it is actually good to keep that, to ensure the connection is alive. |
Yes I agree it is important and useful to call ...But when I create a database/sql.Db{} Object, I've already called it. I already know the connection is alive. Then when I feed it into Gorm, I increase latency a tiny bit because Gorm calls it as well. Why not give us the option of deciding. The default behaviour of pull-request is that it already automcatically calls Ping(). |
@jinzhu What's wrong with letting us individually decide what's best? |
as a by.stander.. where would this be in the configuration ?? is it a gorm thing, and application thing/// ?? a cron in app etc etc.. Kinda cool idea to have a ping alive sometimes.. particular on busy www etc.. So where would the config go? |
@pedromorgan Currently Gorm ALWAYS calls Originally, gorm's There are somethings in my app (and other peoples) where we needed to create/use the sql.DB object ourselves (independent of gorm). It was an optimisation if we could then feed that already-created Someone then created a pull-request and included that functionality: https://github.com/jinzhu/gorm/pull/277 My pull-request extends that functionality so we can specify that sometimes Gorm should not call The pull-request keeps the default behaviour (i.e. always calls It has no negatives to Gorm and only positives. I think @jinzhu wrongly believes this Pull-request ALWAYS disables the automatic |
@pedromorgan Do you see any issues with this? I think @jinzhu doesn't realise that the whole point of feeding a |
I agree there should be option to disable gorm's default |
When using postgres in a kubenetes pod, deleting the statefulset pod (but not it's service) will hang Ping(). I had to remove it from gorm.Open() so I can call my own db.Ping() that supports a timeout, otherwise Ping() takes forever to return and it's hard to tell that the database is down and it's time to switch the application to one of the postgres slaves... So that's an example where having Open() with an optional ping() is usefull so I can call my own ping(), or upgrade gorm to a ping() that supports timeout. Exampe: func PingWithTimeout(db *sql.DB, ctx context.Context) error {
} |
The
gorm.Open(...)
function allows feeding in a*sql.DB
object instead of allowing gorm to create the object itself.In many cases, the reason why we do it is because we already have a valid sql.DB object that we have used elsewhere. We know the connection is valid already. We don't need to waste a round trip by using
.Ping()
to test if it is valid.This pull request allows us to
opt-out
of gorm automatically calling Ping.The default is the current behaviour. It is also backwards-compatible so it should not affect prior code.