當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。