llmongodb/dbs.go
Michele 7a52478c63 gestisce cluster di mongodb
aggiunte le funzioni di hash con SHA1 e MD5 (default)
2017-06-09 16:08:32 +02:00

105 lines
2 KiB
Go

// dbs
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/garyburd/redigo/redis"
"gopkg.in/mgo.v2"
)
var (
dbs = Dbs{
RedisURI: "",
Database: "",
}
)
// Dbs structure
type Dbs struct {
MongoURI string
Database string
RedisURI string
rdb *redis.Pool //*redis.Client
mdb []*mgo.Session
}
// MongoLogin structure
type MongoLogin struct {
ID string `json:"_id" bson:"_id" gorethink:"id"`
User string `json:"user" bson:"user" gorethink:"user"`
Protocol string `json:"protocol" bson:"protocol" gorethink:"protocol"`
IP string `json:"ip" bson:"ip" gorethink:"ip"`
Date time.Time `json:"date" bson:"date" gorethink:"date"`
Insert time.Time `json:"insert" bson:"insert" gorethink:"insert"`
Country string `json:"country" bson:"country" gorethink:"country"`
}
// Ips structure
type Ips struct {
IP string `json:"ip"`
}
// UserLogin structure
type UserLogin struct {
User string `json:"user"`
Date time.Time `json:"date"`
Lock bool `json:"lock"`
}
// Index structure
type Index struct {
User string `json:"user"`
Date time.Time `json:"date"`
}
func (db *Dbs) isMongodb() bool {
if db.MongoURI != "" {
return true
}
return false
}
func (db *Dbs) poolRedis() {
dbs.rdb = &redis.Pool{
MaxIdle: 128,
MaxActive: 1000,
Wait: true,
IdleTimeout: 1 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", db.RedisURI)
if err != nil {
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func (db *Dbs) connectMongo() {
mongoList := strings.Split(db.MongoURI, "|")
for m := range mongoList {
nm, err := mgo.Dial(fmt.Sprintf("mongodb://%s", mongoList[m]))
if err != nil {
log.Println("Mongodb connect Error: ", err.Error())
os.Exit(-3)
}
nm.SetSocketTimeout(5 * time.Second)
nm.SetSyncTimeout(5 * time.Second)
db.mdb = append(db.mdb, nm)
}
}