// Connect tests the mongodb connection and initializes the mongo session
Connect tests the mongodb connection and initializes the mongo session
// Connect tests the mongodb connection and initializes the mongo session
func (m *Mongodb) Connect() error {
	dialInfo, err := mgo.ParseURL(m.uri)
	if err != nil {
		return fmt.Errorf("unable to parse uri (%s), %s\n", m.uri, err.Error())
	}
	if m.conf.Ssl != nil {
		tlsConfig := &tls.Config{}
		roots := x509.NewCertPool()
		if len(m.conf.Ssl.CaCerts) == 0 {
			tlsConfig.InsecureSkipVerify = true
		}
		for _, caCert := range m.conf.Ssl.CaCerts {
			if ok := roots.AppendCertsFromPEM([]byte(caCert)); !ok {
				return fmt.Errorf("failed to parse root certificate")
			}
		}
		tlsConfig.RootCAs = roots
		dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
			conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
			return conn, err
		}
	}
	dialInfo.Timeout = m.sessionTimeout
	m.mongoSession, err = mgo.DialWithInfo(dialInfo)
	if err != nil {
		return err
	}
	m.mongoSession.EnsureSafe(&mgo.Safe{W: m.conf.Wc, FSync: m.conf.FSync})
	m.mongoSession.SetBatch(1000)
	m.mongoSession.SetPrefetch(0.5)
	if m.tail {
		if iter := m.mongoSession.DB("local").C("oplog.rs").Find(bson.M{}).Limit(1).Iter(); iter.Err() != nil {
			return iter.Err()
		}
	}
	return nil
}
func ( m * Mongodb ) Connect ( ) error { dialInfo , err := mgo . ParseURL ( m . uri ) if err != nil { return fmt . Errorf ( " \n " , m . uri , err . Error ( ) ) } if m . conf . Ssl != nil { tlsConfig := & tls . Config { } roots := x509 . NewCertPool ( ) if len ( m . conf . Ssl . CaCerts ) == 0 { tlsConfig . InsecureSkipVerify = true } for _ , caCert := range m . conf . Ssl . CaCerts { if ok := roots . AppendCertsFromPEM ( [ ] byte ( caCert ) ) ; ! ok { return fmt . Errorf ( " " ) } } tlsConfig . RootCAs = roots dialInfo . DialServer = func ( addr * mgo . ServerAddr ) ( net . Conn , error ) { conn , err := tls . Dial ( " " , addr . String ( ) , tlsConfig ) return conn , err } } dialInfo . Timeout = m . sessionTimeout m . mongoSession , err = mgo . DialWithInfo ( dialInfo ) if err != nil { return err } m . mongoSession . EnsureSafe ( & mgo . Safe { W : m . conf . Wc , FSync : m . conf . FSync } ) m . mongoSession . SetBatch ( 1000 ) m . mongoSession . SetPrefetch ( 0.5 ) if m . tail { if iter := m . mongoSession . DB ( " " ) . C ( " " ) . Find ( bson . M { } ) . Limit ( 1 ) . Iter ( ) ; iter . Err ( ) != nil { return iter . Err ( ) } } return nil }
Connect tests the mongodb connection and initializes the mongo session
Connect tests the mongodb connection and initializes the mongo session
// set some options on the session