本文整理汇总了Golang中labix/org/v2/mgo.Session.Clone方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.Clone方法的具体用法?Golang Session.Clone怎么用?Golang Session.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类labix/org/v2/mgo.Session
的用法示例。
在下文中一共展示了Session.Clone方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: New
// New returns a new mongo store
func New(session mgo.Session, database string, collection string, maxAge int, ensureTTL bool, keyPairs ...[]byte) nSessions.Store {
if ensureTTL {
conn := session.Clone()
defer conn.Close()
db := conn.DB(database)
c := db.C(collection)
c.EnsureIndex(mgo.Index{
Key: []string{"modified"},
Background: true,
Sparse: true,
ExpireAfter: time.Duration(maxAge) * time.Second,
})
}
return &mongoStore{
Codecs: securecookie.CodecsFromPairs(keyPairs...),
Token: nSessions.NewCookieToken(),
session: session,
database: database,
collection: collection,
options: &gSessions.Options{
MaxAge: maxAge,
},
}
}
示例2: Initiate
// Initiate sets up a replica set with the given replica set name with the
// single given member. It need be called only once for a given mongo replica
// set. The tags specified will be added as tags on the member that is created
// in the replica set.
//
// Note that you must set DialWithInfo and set Direct = true when dialing into a
// specific non-initiated mongo server.
//
// See http://docs.mongodb.org/manual/reference/method/rs.initiate/ for more
// details.
func Initiate(session *mgo.Session, address, name string, tags map[string]string) error {
monotonicSession := session.Clone()
defer monotonicSession.Close()
monotonicSession.SetMode(mgo.Monotonic, true)
cfg := Config{
Name: name,
Version: 1,
Members: []Member{{
Id: 1,
Address: address,
Tags: tags,
}},
}
logger.Infof("Initiating replicaset with config %#v", cfg)
var err error
for i := 0; i < maxInitiateAttempts; i++ {
err = monotonicSession.Run(bson.D{{"replSetInitiate", cfg}}, nil)
if err != nil && err.Error() == rsMembersUnreachableError {
time.Sleep(initiateAttemptDelay)
continue
}
break
}
return err
}
示例3: getKeys
// Get the list of all SSH keys allowed to access a box.
func getKeys(session *mgo.Session, boxname string) (boxUsers, usernames []string, keys string) {
this_session := session.Clone()
defer this_session.Close()
db := this_session.DB("")
staff := getStaff(db)
boxUsers = usersFromBox(db, boxname)
usernames = combineUsers(staff, boxUsers) // returned
// Looks through `canBeReally`.
keySlice := allKeysFromUsernames(db, usernames)
for _, k := range keySlice {
k = strings.Replace(k, "\n", "", -1)
if !strings.HasPrefix(k, "ssh-") && !strings.HasPrefix(k, "#") {
keys += "# NOT VAILD: "
}
keys += k + "\n"
}
return
}
示例4: db
func db(session *mgo.Session) martini.Handler {
// each request will have its own session
return func(c martini.Context) {
s := session.Clone()
c.Map(s.DB("GoThere"))
defer s.Close()
c.Next()
}
}
示例5: CurrentConfig
// CurrentConfig returns the Config for the given session's replica set. If
// there is no current config, the error returned will be mgo.ErrNotFound.
func CurrentConfig(session *mgo.Session) (*Config, error) {
cfg := &Config{}
monotonicSession := session.Clone()
monotonicSession.SetMode(mgo.Monotonic, true)
err := monotonicSession.DB("local").C("system.replset").Find(nil).One(cfg)
if err == mgo.ErrNotFound {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("cannot get replset config: %s", err.Error())
}
return cfg, nil
}
示例6: Initiate
// Initiate sets up a replica set with the given replica set name with the
// single given member. It need be called only once for a given mongo replica
// set. The tags specified will be added as tags on the member that is created
// in the replica set.
//
// Note that you must set DialWithInfo and set Direct = true when dialing into a
// specific non-initiated mongo server.
//
// See http://docs.mongodb.org/manual/reference/method/rs.initiate/ for more
// details.
func Initiate(session *mgo.Session, address, name string, tags map[string]string) error {
monotonicSession := session.Clone()
monotonicSession.SetMode(mgo.Monotonic, true)
cfg := Config{
Name: name,
Version: 1,
Members: []Member{{
Id: 1,
Address: address,
Tags: tags,
}},
}
logger.Infof("Initiating replicaset with config %#v", cfg)
return monotonicSession.Run(bson.D{{"replSetInitiate", cfg}}, nil)
}
示例7: Initiate
// Initiate sets up a replica set with the given replica set name with the
// single given member. It need be called only once for a given mongo replica
// set. The tags specified will be added as tags on the member that is created
// in the replica set.
//
// Note that you must set DialWithInfo and set Direct = true when dialing into a
// specific non-initiated mongo server.
//
// See http://docs.mongodb.org/manual/reference/method/rs.initiate/ for more
// details.
func Initiate(session *mgo.Session, address, name string, tags map[string]string) error {
monotonicSession := session.Clone()
defer monotonicSession.Close()
monotonicSession.SetMode(mgo.Monotonic, true)
cfg := Config{
Name: name,
Version: 1,
Members: []Member{{
Id: 1,
Address: address,
Tags: tags,
}},
}
logger.Infof("Initiating replicaset with config %#v", cfg)
var err error
for i := 0; i < maxInitiateAttempts; i++ {
monotonicSession.Refresh()
err = monotonicSession.Run(bson.D{{"replSetInitiate", cfg}}, nil)
if err != nil && err.Error() == rsMembersUnreachableError {
time.Sleep(initiateAttemptDelay)
continue
}
break
}
// Wait for replSetInitiate to complete. Even if err != nil,
// it may be that replSetInitiate is still in progress, so
// attempt CurrentStatus.
for i := 0; i < maxInitiateStatusAttempts; i++ {
monotonicSession.Refresh()
var status *Status
status, err = getCurrentStatus(monotonicSession)
if err != nil {
logger.Warningf("Initiate: fetching replication status failed: %v", err)
}
if err != nil || len(status.Members) == 0 {
time.Sleep(initiateAttemptStatusDelay)
continue
}
break
}
return err
}
示例8: MgoSessionInjectFilterFunc
// 创建revel_session 注入过滤器
func MgoSessionInjectFilterFunc(session *mgo.Session) func(c *revel.Controller, fc []revel.Filter) {
return func(c *revel.Controller, fc []revel.Filter) {
appCtrl := c.AppController
typeOfC := reflect.TypeOf(appCtrl).Elem()
_, ok := typeOfC.FieldByName("MSession")
if !ok {
fc[0](c, fc[1:])
return
}
valueOfC := reflect.ValueOf(appCtrl).Elem()
// 注入 session
newSession := session.Clone()
defer newSession.Close()
valueOfSession := reflect.ValueOf(newSession)
valoeOfElem := valueOfC.FieldByName("MSession")
valoeOfElem.Set(valueOfSession)
fc[0](c, fc[1:])
}
}
示例9: Register
func Register(dbsess *mgo.Session, dbname string) {
DefaultProvider.DBSess = dbsess.Clone()
DefaultProvider.DBName = dbname
}