I have an application which uses Firestore and is hosted on Cloud Run.
I have a global variable which holds the client instance, and my question is whether it needs to be closed explicitly, since the Close
method itself states “Close need not be called at program exit.”?
The “config” package which holds the Firebase instances:
var App *firebase.App
var Firestore
func LoadFirebase(ctx context.Context) {
app, err := firebase.NewApp(ctx, nil)
if err != nil {
log.Fatalln("Firebase initialisation failed:", err)
}
firestore, err := app.Firestore(ctx)
if err != nil {
log.Fatalln("Firestore initialisation failed:", err)
}
App = app
Firestore = firestore
}
The “main” package:
func init() {
initialisers.LoadFirebase(context.Background())
}
func main() {
// initialise server
PORT := os.Getenv("PORT")
router := http.NewServeMux()
Routes(router)
server := http.Server{
Addr: ":" + PORT,
Handler: middleware.CheckAuth(router),
}
// IS THIS NECESSARY?
defer config.Firestore.Close()
fmt.Println("Server is running on port:", PORT)
log.Fatalln("Error starting server:", server.ListenAndServe())
}