最佳实践打开数据库。

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

Best Practise opening DB

问题

注意:我不确定这是否是这篇文章的最准确的标题,如果不是,请提供一个更好的标题。

目前,我正在创建一个服务器,其中包含几个处理程序(使用goji)。在接收到请求后,我想要与我拥有的MongoDB数据库进行交互(使用mgo)。我的问题是:

我假设每次处理请求时执行这种操作是昂贵的:

  1. uri := os.Getenv("MONGOHQ_URL")
  2. if uri == "" {
  3. panic("no DB connection string provided")
  4. }
  5. session, err := mgo.Dial(uri)

那么,对我来说,是否更好的做法是拥有一个可以从处理程序内部访问的全局变量呢? 所以我会这样做:

  1. var session *mgo.Session
  2. func main() {
  3. session = setupDB()
  4. defer session.Close()
  5. goji.Get("/user", getUser)
  6. goji.Serve()
  7. }
  8. func getUser(c web.C, w http.ResponseWriter, r *http.Request) {
  9. // 在这里使用 session 变量
  10. }

我的问题与在这里应该采用什么最佳实践有关?是每次请求到来时打开数据库,还是在整个应用程序的生命周期内保持数据库连接打开?

英文:

Note: I am not sure if this is the most accurate title for this post, if not, please advise on a better one.

Currently I am creating a server where I have a couple of handlers (using goji). After receiving a request, I want to interact with a MongoDB database I have (using mgo). My question is:

I am assuming doing this kind of stuff every time I am handling a request is expensive:

  1. uri := os.Getenv("MONGOHQ_URL")
  2. if uri == "" {
  3. panic("no DB connection string provided")
  4. }
  5. session, err := mgo.Dial(uri)

So, would it be better for me to have a global var that I can access from inside the handlers? So I would go with something like this:

  1. var session *mgo.Session
  2. func main() {
  3. session = setupDB()
  4. defer session.Close()
  5. goji.Get("/user", getUser)
  6. goji.Serve()
  7. }
  8. func getUser(c web.C, w http.ResponseWriter, r *http.Request) {
  9. // Use the session var here
  10. }

My question is related to what would be the best practise here? Opening the DB every time a request comes in, or keep it open for the entire duration of the application.

答案1

得分: 1

你可以像这样将你的处理程序包装在一个Controller结构体中:(http://play.golang.org/p/NK6GO_lqgk)

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "github.com/zenazn/goji"
  8. "github.com/zenazn/goji/web"
  9. )
  10. type Controller struct {
  11. session *Session
  12. }
  13. func NewController() (*Controller, error) {
  14. if uri := os.Getenv("MONGOHQ_URL"); uri == "" {
  15. return nil, fmt.Errorf("no DB connection string provided")
  16. }
  17. session, err := mgo.Dial(uri)
  18. if err != nil {
  19. return nil, err
  20. }
  21. return &Controller{
  22. session: session,
  23. }, nil
  24. }
  25. func (c *Controller) getUser(c web.C, w http.ResponseWriter, r *http.Request) {
  26. // 在这里使用session变量
  27. }
  28. func main() {
  29. ctl, err := NewController()
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. defer ctl.session.Close()
  34. goji.Get("/user", ctl.getUser)
  35. goji.Serve()
  36. }
  37. 这样你可以将session嵌入到处理程序中并添加任何其他你可能需要的数据
  38. <details>
  39. <summary>英文:</summary>
  40. What about wraping your handler in a Controller struct like this: (http://play.golang.org/p/NK6GO_lqgk)
  41. package main
  42. import (
  43. &quot;fmt&quot;
  44. &quot;log&quot;
  45. &quot;net/http&quot;
  46. &quot;os&quot;
  47. &quot;github.com/zenazn/goji&quot;
  48. &quot;github.com/zenazn/goji/web&quot;
  49. )
  50. type Controller struct {
  51. session *Session
  52. }
  53. func NewController() (*Controller, error) {
  54. if uri := os.Getenv(&quot;MONGOHQ_URL&quot;); uri == &quot;&quot; {
  55. return nil, fmt.Errorf(&quot;no DB connection string provided&quot;)
  56. }
  57. session, err := mgo.Dial(uri)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &amp;Controller{
  62. session: session,
  63. }, nil
  64. }
  65. func (c *Controller) getUser(c web.C, w http.ResponseWriter, r *http.Request) {
  66. // Use the session var here
  67. }
  68. func main() {
  69. ctl, err := NewController()
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. defer ctl.session.Close()
  74. goji.Get(&quot;/user&quot;, ctl.getUser)
  75. goji.Serve()
  76. }
  77. This way, you can embed your session in your handler and add any other data that you might need.
  78. </details>

huangapple
  • 本文由 发表于 2014年8月29日 22:23:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/25570321.html
匿名

发表评论

匿名网友

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

确定