defer: usage in case of long running functions?

huangapple go评论84阅读模式
英文:

defer: usage in case of long running functions?

问题

这里是一个示例函数,它从连接池中获取数据库连接,执行查询并处理返回的结果。

func dbQuery() error {
    con := db.getConn()
    result, err := con.Query()
    if err != nil {
       return err
    }
    defer con.close() // 或者 con.close()
    // 处理结果需要很长时间

    return nil
}

在这种情况下,处理结果需要很长时间,并且没有调用 close 来关闭连接,这意味着连接没有返回到连接池中。在这种情况下,直接调用 con.close() 是否可以,即使我们知道资源在不需要时会被长时间占用?

英文:

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.<br> 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?

答案1

得分: 4

我的建议是重构你的代码。这样,defer语句会在处理结果之前关闭连接:

func dbQuery() error {
    result, err := getResult()
    if err != nil {
        return err
    }
    // 处理结果需要很长时间

    return nil
}

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

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

    return result, err
}

这样做可以确保在处理结果之前关闭连接,避免资源泄漏。

英文:

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
}

答案2

得分: 2

在大多数情况下,Close() 方法是幂等的,可以安全地多次调用。在出现错误和/或提前函数返回的情况下,你可以使用 defer con.Close(),但是在完成后明确调用 Close() 也是完全可以的。因此,当你有一些长函数时,同时使用这两种方式是最佳实践。

英文:

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.

huangapple
  • 本文由 发表于 2017年8月13日 03:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/45653916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定