Beispiel #1
0
func NewMongo(opts *MongoOptions) *Mongo {
	if opts.Uri == "" {
		var uri string
		if opts.User == "" && opts.Password == "" {
			uri = opts.Host + ":" + strconv.Itoa(int(opts.Port))
		} else {
			uri = opts.User + ":" + opts.Password + "@" + opts.Host + ":" +
				strconv.Itoa(int(opts.Port))
		}

		opts.Uri = uri
	}
	if opts.DbName == "" {
		opts.DbName = DEFAULT_DB_NAME
	}

	glog.Info("connect to mongo : ", opts.Uri)
	maxWait := time.Duration(5 * time.Second)
	session, err := mgo.DialWithTimeout(opts.Uri, maxWait)
	if err != nil {
		panic(err)
	}
	session.SetMode(mgo.Monotonic, true)

	return &Mongo{
		Opts:         opts,
		Session:      session,
		ReConnecting: false,
	}
}
Beispiel #2
0
//ReConnect
func (m *Mongo) ReConnect() {
	glog.Info("ReConnecting")
	maxWait := time.Duration(5 * time.Second)
	session, err := mgo.DialWithTimeout(m.Opts.Uri, maxWait)

	if err != nil {
		m.ReConnecting = false
		glog.Error(err.Error())
		return
	}

	session.SetMode(mgo.Monotonic, true)
	m.Session = session
	m.ReConnecting = false
}
Beispiel #3
0
func Pprof(pprofFile *string) {
	f, err := os.Create(*pprofFile)
	if err != nil {
		glog.Fatal(err)
	}
	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	go func() {
		<-c
		glog.Info("Ctrl+C to quit.")
		pprof.StopCPUProfile()
		os.Exit(1)
	}()
}
Beispiel #4
0
func main() {
	var (
		cfg *Config
		c   chan int
	)
	//VERSION
	version()
	//FLAG PARSE
	flag.Parse()
	//READ CONFIG FILE
	cfg = NewConfig(*confFile)
	//Daemon
	Daemon(cfg.Server)
	// log
	SetLogSettings(cfg.Log)
	//TURN ON PPROF
	if cfg.Debug.PprofFile != "" {
		Pprof(&cfg.Debug.PprofFile)
	}

	glog.Info("test info")
	glog.V(0).Info("test info v0")
	glog.V(1).Info("test info v1")
	glog.V(2).Info("test info v2")
	glog.Warning("warning")
	glog.Error("error")

	// glog.Info("Config: ", cfg)

	// db := mongo.NewMongo(cfg.Db)

	// glog.Info(*db.Opts)

	// go func(db *mongo.Mongo) {
	// 	timer := time.NewTicker(5 * time.Second)

	// 	for {
	// 		select {
	// 		case <-timer.C:
	// 			// event, err := db.MemorySelectEventsFromKey("hi")
	// 			event, err := db.MemorySelectOneEventFromKeyRandom("hi")
	// 			if err != nil {
	// 				glog.Info(err.Error())
	// 			} else {
	// 				glog.Info(event)
	// 			}
	// 		}
	// 	}
	// }(db)

	ms.server, err = libnet.Serve(cfg.TransportProtocols, cfg.Listen, libnet.Packet(libnet.Uint16BE, libnet.Json()))
	if err != nil {
		panic(err)
	}

	for {
		session, err := ms.server.Accept()
		if err != nil {
			break
		}

		go handleSession(ms, session)
	}

	select {
	case <-c:
		break
	}
}