当前位置: 首页>>代码示例>>Golang>>正文


Golang mgo.DialWithTimeout函数代码示例

本文整理汇总了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
}
开发者ID:supermouseno1,项目名称:db,代码行数:36,代码来源:mongo.go

示例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
}
开发者ID:mishudark,项目名称:gosexy,代码行数:32,代码来源:mongo.go

示例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
}
开发者ID:icattlecoder,项目名称:db,代码行数:36,代码来源:database.go

示例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)
	}
}
开发者ID:mfelicio,项目名称:go-einvite-traditional,代码行数:27,代码来源:dbsession.go

示例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
}
开发者ID:prinsmike,项目名称:Shock,代码行数:8,代码来源:db.go

示例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
}
开发者ID:jaredwilkening,项目名称:AWE,代码行数:8,代码来源:db.go

示例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
}
开发者ID:eberroca,项目名称:Shock,代码行数:8,代码来源:db.go

示例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
}
开发者ID:ryansb,项目名称:gofigure,代码行数:10,代码来源:mongo_test.go

示例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)
}
开发者ID:nleite,项目名称:wrkshp,代码行数:11,代码来源:mail.go

示例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()
}
开发者ID:robyoung,项目名称:performance-datastore,代码行数:12,代码来源:handlers.go

示例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()
}
开发者ID:gophergala2016,项目名称:gobench,代码行数:12,代码来源:backend.go

示例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()
}
开发者ID:sharewind,项目名称:push-server,代码行数:12,代码来源:mgo_helper.go

示例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
}
开发者ID:pombredanne,项目名称:camlistore,代码行数:13,代码来源:mongokv.go

示例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
}
开发者ID:narayandesai,项目名称:AWE,代码行数:15,代码来源:db.go

示例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
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:16,代码来源:storage.go


注:本文中的labix/org/v2/mgo.DialWithTimeout函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。