MGO和长时间运行的Web服务 – 恢复

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

MGO and long running Web Services - recovery

问题

我已经写了一个使用Mongo作为后端数据存储的REST Web服务。在部署之前,我想知道在一个基本上永远运行的服务中,最佳实践是什么。

目前,我正在遵循这种模式:

// database.go
...

type DataStore struct {
mongoSession *mgo.Session
}

...

func (d *DataStore) OpenSession () {
... // 从环境中读取设置

mongoSession, err = mgo.Dial(mongoURI)
if err != nil {}
...
}

func (d *DataStore) CloseSession() {...}

func (d *DataStore) Find (...) (results...) {
s := d.mongoSession.Copy()
defer s.Close()

// 做一些操作,返回结果

}

在main.go中:

func main() {
ds := NewDataStore()
ds.OpenSession()
defer ds.CloseSession()

// Web服务路由...
...
ws.Handle("/find/{abc}", doFindFunc)
...

}

我的问题是,对于已超时的会话、丢失连接的情况(我使用的Mongo服务提供商是远程的,所以我认为这种情况可能会发生),从这些情况中恢复的推荐做法是什么?在任何特定的Web服务调用中,如何处理这些情况以检测会话是否不再有效,并建立一个"新鲜"的会话?

谢谢!

英文:

I've written a REST web service that uses mongo as the backend data store. I was wondering at this stage (before deployment), what the best practices were, considering a service that essentially runs forever(ish).

Currently, I'm following this type of pattern:

// database.go
...

type DataStore struct {
  mongoSession  *mgo.Session
}

...
func (d *DataStore) OpenSession () {
   ... // read setup from environment

   mongoSession, err = mgo.Dial(mongoURI)
   if err != nil {}
   ...
}

func (d *DataStore) CloseSession() {...}

func (d *DataStore) Find (...) (results...) {
    s := d.mongoSession.Copy()
    defer s.Close()

    // do stuff, return results
}

In main.go:

func main() {
    ds := NewDataStore()
    ds.OpenSession()
    defer ds.CloseSession()


    // Web Service Routes..
    ...
    ws.Handle("/find/{abc}", doFindFunc)
    ...
}

My question is - what's the recommended practice for recovery from session that has timed out, lost connection (the mongo service provider I'm using is remote, so I assume that this will happen), so on any particular web service call, the database session may no longer work? How do people handle these cases to detect that the session is no longer valid and a "fresh" one should be established?

Thanks!

答案1

得分: 2

你可能想要做的是对每个传入的HTTP请求执行会话.Copy()(带有延迟的.Close()),如果需要的话,在处理程序中再次从新会话中复制。

连接和重新连接由mgo管理,您可以在向Web服务发出HTTP请求时停止和重新启动MongoDB,以查看其影响。

如果在处理HTTP请求时出现数据库连接问题,数据库操作最终将超时(可以通过使用DialWithTimeout而不是常规的Dial来配置超时),因此您可以在这种情况下使用5xx HTTP错误代码进行响应。

英文:

what you may want is to do the session .Copy() for each incoming HTTP request (with deffered .Close()), copy again from the new session in your handlers if ever needed..

connections and reconnections are managed by mgo, you can stop and restart MongoDB while making an HTTP request to your web service to see how its affected.

if there's a db connection problem while handling an HTTP request, a db operation will eventually timeout (timeout can be configured by using DialWithTimeout instead of the regular Dial, so you can respond with a 5xx HTTP error code in such case.

huangapple
  • 本文由 发表于 2014年12月21日 00:47:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/27582272.html
匿名

发表评论

匿名网友

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

确定