Skip to content

Commit

Permalink
Completed the work to improve API performance under heavy load
Browse files Browse the repository at this point in the history
  • Loading branch information
pzaino committed Sep 27, 2024
1 parent 59258fb commit d60d706
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 233 deletions.
72 changes: 10 additions & 62 deletions services/api/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
errFailedToCommitTransaction = "Failed to commit transaction"
)

func performAddSource(query string, qType int) (ConsoleResponse, error) {
func performAddSource(query string, qType int, db *cdb.Handler) (ConsoleResponse, error) {
var sqlQuery string
var sqlParams addSourceRequest
if qType == getQuery {
Expand All @@ -53,7 +53,7 @@ func performAddSource(query string, qType int) (ConsoleResponse, error) {
}

// Perform the addSource operation
results, err := addSource(sqlQuery, sqlParams)
results, err := addSource(sqlQuery, sqlParams, db)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "adding the source: %v", err)
return results, err
Expand Down Expand Up @@ -105,7 +105,7 @@ func extractAddSourceParams(query string, params *addSourceRequest) {

}

func addSource(sqlQuery string, params addSourceRequest) (ConsoleResponse, error) {
func addSource(sqlQuery string, params addSourceRequest, db *cdb.Handler) (ConsoleResponse, error) {
var results ConsoleResponse
results.Message = "Failed to add the source"

Expand All @@ -119,28 +119,14 @@ func addSource(sqlQuery string, params addSourceRequest) (ConsoleResponse, error
}
}

// Initialize the database handler
db, err := cdb.NewHandler(config)
if err != nil {
return results, err
}

// Connect to the database
err = db.Connect(config)
if err != nil {
cmn.DebugMsg(cmn.DbgLvlError, "connecting to the database: %v", err)
return results, err
}
defer db.Close()

// Get the JSON string for the Config field
configJSON, err := json.Marshal(params.Config)
if err != nil {
return results, err
}

// Execute the SQL statement
_, err = db.Exec(sqlQuery, params.URL, params.Status, params.Restricted, params.Disabled, params.Flags, string(configJSON), params.CategoryID, params.UsrID)
_, err = (*db).Exec(sqlQuery, params.URL, params.Status, params.Restricted, params.Disabled, params.Flags, string(configJSON), params.CategoryID, params.UsrID)
if err != nil {
return results, err
}
Expand Down Expand Up @@ -180,7 +166,7 @@ func validateAndReformatConfig(config *cfg.SourceConfig) error {
return nil
}

func performRemoveSource(query string, qType int) (ConsoleResponse, error) {
func performRemoveSource(query string, qType int, db *cdb.Handler) (ConsoleResponse, error) {
var results ConsoleResponse
var sourceURL string // Assuming the source URL is passed. Adjust as necessary based on input.

Expand All @@ -193,21 +179,8 @@ func performRemoveSource(query string, qType int) (ConsoleResponse, error) {
return ConsoleResponse{Message: "Invalid request"}, nil
}

// Initialize the database handler
db, err := cdb.NewHandler(config)
if err != nil {
return ConsoleResponse{Message: errFailedToInitializeDBHandler}, err
}

// Connect to the database
err = db.Connect(config)
if err != nil {
return ConsoleResponse{Message: errFailedToConnectToDB}, err
}
defer db.Close()

// Start a transaction
tx, err := db.Begin()
tx, err := (*db).Begin()
if err != nil {
return ConsoleResponse{Message: errFailedToStartTransaction}, err
}
Expand Down Expand Up @@ -269,7 +242,7 @@ func removeSource(tx *sql.Tx, sourceURL string) (ConsoleResponse, error) {
return results, nil
}

func performGetURLStatus(query string, qType int) (StatusResponse, error) {
func performGetURLStatus(query string, qType int, db *cdb.Handler) (StatusResponse, error) {
var results StatusResponse
var sourceURL string // Assuming the source URL is passed. Adjust as necessary based on input.

Expand All @@ -282,21 +255,8 @@ func performGetURLStatus(query string, qType int) (StatusResponse, error) {
return StatusResponse{Message: "Invalid request"}, nil
}

// Initialize the database handler
db, err := cdb.NewHandler(config)
if err != nil {
return StatusResponse{Message: errFailedToInitializeDBHandler}, err
}

// Connect to the database
err = db.Connect(config)
if err != nil {
return StatusResponse{Message: errFailedToConnectToDB}, err
}
defer db.Close()

// Start a transaction
tx, err := db.Begin()
tx, err := (*db).Begin()
if err != nil {
return StatusResponse{Message: errFailedToStartTransaction}, err
}
Expand Down Expand Up @@ -362,23 +322,11 @@ func getURLStatus(tx *sql.Tx, sourceURL string) (StatusResponse, error) {
return results, nil
}

func performGetAllURLStatus(_ int) (StatusResponse, error) {
func performGetAllURLStatus(_ int, db *cdb.Handler) (StatusResponse, error) {
// using _ instead of qType because for now we don't need it
// Initialize the database handler
db, err := cdb.NewHandler(config)
if err != nil {
return StatusResponse{Message: errFailedToInitializeDBHandler}, err
}

// Connect to the database
err = db.Connect(config)
if err != nil {
return StatusResponse{Message: errFailedToConnectToDB}, err
}
defer db.Close()

// Start a transaction
tx, err := db.Begin()
tx, err := (*db).Begin()
if err != nil {
return StatusResponse{Message: errFailedToStartTransaction}, err
}
Expand Down
Loading

0 comments on commit d60d706

Please sign in to comment.