Ankit Deshpande
Ankit Deshpande

Reputation: 3604

defer: usage in case of long running functions?

Here is a sample function that takes a connection to the database from the connection pool and does a query and processes the result returned.

func dbQuery() error {
    con := db.getConn()
    result, err := con.Query()
    if err != nil {
       return err
    }
    defer con.close() // or con.close()
    // Processing the result takes a long time


    return nil
}

Processing the result in this case takes a long time and close is not called for the connection, which means it is not returned to the connection pool.
Is it ok to directly call con.close() in cases like this when we know the resources are held for a long time even when they are not needed?

Upvotes: 1

Views: 142

Answers (2)

TehSphinX
TehSphinX

Reputation: 7430

My suggestion would be to refactor your code. This way a defer closes the connection before you process the result:

func dbQuery() error {
    result, err := getResult()
    if err != nil {
        return err
    }
    // Processing the result takes a long time

    return nil
}

func getResult() (*Rows, error) {
    con := db.getConn()
    defer con.close() // or con.close()

    result, err := con.Query()
    if err != nil {
        return nil, err
    }

    return result, err
}

Upvotes: 5

RayfenWindspear
RayfenWindspear

Reputation: 6274

In most cases Close() methods are idempotent and can be called more than once safely. You defer con.Close() in case of an error and/or early function return, but you are perfectly fine to explicitly call Close() as soon as you are done with it. So yes, it is best practice to use both when you have some long function.

Upvotes: 2

Related Questions