I’m having performance issues using a controller-service-repository architecture in my Golang app.
I’m using Postman to make a request, which takes over a minute to successfully insert a record in the database. I’ve also tried giving the controller direct access to the database, but the same issue occurs.
When I write a simple handler to establish a connection to the database and perform a query, the query executes quickly.
If anyone has an idea of what might be causing the performance issue, please let me know.
Here is the code for the main function, as well as the controller, service and repository:
func main() {
r := setupRouter()
pgInstance := db2.ConnectToDatabase()
repo := repository.NewRepository(pgInstance)
service := service2.NewService(repo)
c := controller2.NewController(service)
routes.InitializeRoutes(r, c)
// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
}
Controller:
type Controller struct {
service service.Service
}
func NewController(service service.Service) Controller {
return Controller{
service: service,
}
}
Service:
type Service struct {
repository repository.Repository
}
func NewService(repo repository.Repository) Service {
return Service{
repository: repo,
}
}
Repository:
type Repository struct {
db *gorm.DB
}
func NewRepository(db *gorm.DB) Repository {
return Repository{
db: db,
}
}
func (r Repository) Db() *gorm.DB {
return r.db
}