I am trying to understand if the following is good or bad practice.
If it turns out that this is good / normal practice, it makes it easier for me to implement multiple database connectors into my project.
Situation: I have created an API server and every time a call is done to the API this piece of code runs:
ctx := context.Background()
client, err := datastore.NewClient(ctx, "foobar")
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
The service gets many requests per second and I'm not sure if I should run the newClient()
function only once on startup, or every time the API is called.
PS:
The same would go if another connector would be MySQL. Every time the API gets a request, the following code will run:
db, err = sql.Open("mysql", "yourusername:yourpassword@/yourdatabase")
if err != nil {
panic(err.Error())
}