本文整理汇总了Golang中labix/org/v2/mgo.DialWithTimeout函数的典型用法代码示例。如果您正苦于以下问题:Golang DialWithTimeout函数的具体用法?Golang DialWithTimeout怎么用?Golang DialWithTimeout使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DialWithTimeout函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Open
// Opens a connection to the datasource. See Session().
func (self *Source) Open() error {
var err error
connURL := &url.URL{Scheme: "mongodb"}
if self.config.Port == 0 {
self.config.Port = 27017
}
if self.config.Host == "" {
self.config.Host = "127.0.0.1"
}
connURL.Host = fmt.Sprintf("%s:%d", self.config.Host, self.config.Port)
if self.config.User != "" {
connURL.User = url.UserPassword(self.config.User, self.config.Password)
}
if self.config.Database != "" {
connURL.Path = "/" + self.config.Database
}
self.session, err = mgo.DialWithTimeout(connURL.String(), 5*time.Second)
if err != nil {
return err
}
if self.config.Database != "" {
self.Use(self.config.Database)
}
return nil
}
示例2: Open
// Connects to the previously specified datasource. See Session().
func (m *MongoDataSource) Open() error {
var err error
connURL := &url.URL{Scheme: "mongodb"}
if m.config.Port == 0 {
m.config.Port = 27017
}
if m.config.Host == "" {
m.config.Host = "127.0.0.1"
}
connURL.Host = fmt.Sprintf("%s:%d", m.config.Host, m.config.Port)
if m.config.User != "" {
connURL.User = url.UserPassword(m.config.User, m.config.Password)
}
m.session, err = mgo.DialWithTimeout(connURL.String(), 5*time.Second)
if err != nil {
return fmt.Errorf("Could not connect to %v.", m.config.Host)
}
if m.config.Database != "" {
m.Use(m.config.Database)
}
return nil
}
示例3: Open
// Attempts to connect to a database using the stored settings.
func (self *Source) Open() error {
var err error
connURL := &url.URL{Scheme: `mongodb`}
if self.config.Port == 0 {
self.config.Port = 27017
}
if self.config.Host == "" {
self.config.Host = `127.0.0.1`
}
connURL.Host = fmt.Sprintf(`%s:%d`, self.config.Host, self.config.Port)
if self.config.User != "" {
connURL.User = url.UserPassword(self.config.User, self.config.Password)
}
if self.config.Database != "" {
connURL.Path = "/" + self.config.Database
}
if self.config.Database == "" {
return db.ErrMissingDatabaseName
}
if self.session, err = mgo.DialWithTimeout(connURL.String(), connTimeout); err != nil {
return err
}
self.Use(self.config.Database)
return nil
}
示例4: _createSession
//private to this file
func _createSession() (*mgo.Session, error) {
if _cfg == nil {
_cfg = &mongoConfig{}
framework.Config.ReadInto("mongo", &_cfg)
}
var timeout = 10 * time.Second
if _cfg.ConnectionString != "" {
return mgo.DialWithTimeout(_cfg.ConnectionString, timeout)
} else {
dialInfo := mgo.DialInfo{}
dialInfo.Database = _cfg.Database
dialInfo.Username = _cfg.User
dialInfo.Password = _cfg.Password
dialInfo.Addrs = []string{fmt.Sprintf("%s:%s", _cfg.Host, _cfg.Port)}
dialInfo.Timeout = timeout
fmt.Println("Logging to", dialInfo.Addrs[0])
return mgo.DialWithInfo(&dialInfo)
}
}
示例5: DBConnect
func DBConnect() (d *db, err error) {
session, err := mgo.DialWithTimeout(conf.MONGODB, DbTimeout)
if err != nil {
return
}
d = &db{User: session.DB("ShockDB").C("Users"), Session: session}
return
}
示例6: DBConnect
func DBConnect() (d *db, err error) {
session, err := mgo.DialWithTimeout(conf.MONGODB, DbTimeout)
if err != nil {
return
}
d = &db{Jobs: session.DB("AWEDB").C("Jobs"), Session: session}
return
}
示例7: DBConnect
func DBConnect() (d *db, err error) {
session, err := mgo.DialWithTimeout(conf.MONGODB, DbTimeout)
if err != nil {
return
}
d = &db{Nodes: session.DB("ShockDB").C("Nodes"), PreAuth: session.DB("ShockDB").C("PreAuth"), Session: session}
return
}
示例8: connectMongo
func connectMongo(c *C) *mgo.Session {
session, err := mgo.DialWithTimeout(testMongoHost, 500*time.Millisecond)
if err != nil {
c.Fatalf(err.Error())
}
session.SetMode(mgo.Monotonic, true)
session.SetSyncTimeout(200 * time.Millisecond)
session.SetSocketTimeout(200 * time.Millisecond)
return session
}
示例9: main
func main() {
host := flag.String("host", "nair.local", "mongodb host name") // "nair.local"
dbname := flag.String("db", "enron", "database name") //"enron"
colname := flag.String("col", "messages", "collection name") //"messages"
keyword := flag.String("kw", "Sunday", "keyword to search") //"Sunday"
flag.Parse()
timeout := time.Duration(1) * time.Second
session, _ := mgo.DialWithTimeout(*host, timeout)
operator := NewOperator(session.DB(*dbname).C(*colname))
operator.FindKeyword(*keyword)
}
示例10: getMgoSession
func getMgoSession() *mgo.Session {
if mgoSession == nil {
var err error
mgoSession, err = mgo.DialWithTimeout(mgoURL, 5*time.Second)
if err != nil {
panic(err)
}
// Set timeout to suitably small value by default.
mgoSession.SetSyncTimeout(5 * time.Second)
}
return mgoSession.Copy()
}
示例11: connectDB
func (b *Backend) connectDB() error {
var err error
b.session, err = mgo.DialWithTimeout(fmt.Sprintf("%s:%d/%s", b.dbConfig.IP, b.dbConfig.Port, b.dbConfig.Name), time.Second*3)
if err != nil {
return err
}
b.session.SetMode(mgo.Monotonic, true)
return b.session.Ping()
}
示例12: getSession
func getSession() *mgo.Session {
if mgoSession == nil {
var err error
mgoSession, err = mgo.DialWithTimeout(databaseServer, 60*time.Second)
mgoSession.SetMode(mgo.Monotonic, true)
mgoSession.SetMode(mgo.Strong, true)
if err != nil {
panic(err) // no, not really
}
}
return mgoSession.Clone()
}
示例13: ping
// ping won't work with old (1.2) mongo servers.
func (ins *instance) ping(timeout time.Duration) bool {
session, err := mgo.DialWithTimeout(ins.url(), timeout)
if err != nil {
return false
}
defer session.Close()
session.SetSyncTimeout(timeout)
if err = session.Ping(); err != nil {
return false
}
return true
}
示例14: Initialize
func Initialize() (err error) {
c := connection{}
s, err := mgo.DialWithTimeout(conf.MONGODB_HOST, DbTimeout)
if err != nil {
e := errors.New(fmt.Sprintf("no reachable mongodb server(s) at %s", conf.MONGODB_HOST))
return e
}
c.Session = s
c.DB = c.Session.DB(conf.MONGODB_DATABASE)
if conf.MONGODB_USER != "" && conf.MONGODB_PASSWD != "" {
c.DB.Login(conf.MONGODB_USER, conf.MONGODB_PASSWD)
}
Connection = c
return
}
示例15: open
func open(addr, dbname string) (*Storage, error) {
sess, err := mgo.DialWithTimeout(addr, 1e9)
if err != nil {
return nil, err
}
copy := sess.Copy()
go func(t time.Duration) {
time.Sleep(t)
copy.Close()
}(maxIdleTime)
storage := &Storage{session: copy, dbname: dbname}
mut.Lock()
conn[addr] = &session{s: sess, used: time.Now()}
mut.Unlock()
return storage, nil
}