My main function looks something like this:
func main() {
dpPool := &DatabasePool{db: connectDb()}
defer dpPool.db.Close()
ctx := context.Background()
dpPool.setUpTables(ctx)
defer dpPool.dropTables(ctx)
mux := http.NewServeMux()
mux.HandleFunc("/", getRoot)
mux.HandleFunc("/hello", getHello)
mux.HandleFunc("/ping", ping)
server := &http.Server{
Addr: "<address>",
Handler: mux,
BaseContext: func(l net.Listener) context.Context {
ctx = context.WithValue(ctx, keyServerAddr, l.Addr().String())
return ctx
},
}
err := server.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error listening for server: %s\n", err)
}
}
I am trying to write a web server, and I want to close my database connection pool and drop tables when I close the server. I typically do that now with control + C, which SIGINT’s the program. The tables are not dropping so I think it has something to do with defer. Any help would be appreciated. Thank you in advance. If there is a need for more information, please let me know.
EDIT: I found a solution with this Stackoverflow post. It is a messy workaround, but it works:
In Main:
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
fmt.Println(sig)
dpPool.dropTables(ctx)
fmt.Println("Dropped those tables :)")
dpPool.db.Close()
pprof.StopCPUProfile()
os.Exit(1)
}
}()
If there is a better way to handle cleanup functions when a SIGINT interrupt occurs, please let me know