Esempio n. 1
0
//创建一个新的redis连接
func NewRedisCache(ip string, port string, password string) (*RedisCache, error) {
	var (
		c   redis.Conn
		err error
	)

	c, err = redis.DialTimeout("tcp", ip+":"+port, 0, 1*time.Second, 1*time.Second)
	if err != nil {
		glog.Error("Error:", err)
		return nil, err
	}

	if password != "" {
		_, err = c.Do("AUTH", password)
		if err != nil {
			c.Close()
			glog.Error("Error:", err)
			return nil, err
		}
	}

	return &RedisCache{
		session: c,
	}, err
}
Esempio n. 2
0
func NewConfig(confFile string) *Config {
	temp := &ConfigTemp{}

	conf := goconf.New()
	if err := conf.Parse(confFile); err != nil {
		glog.Error(err.Error())
		return temp.Parse()
	}

	// temp := &ConfigTemp{}
	if err := conf.Unmarshal(temp); err != nil {
		glog.Error(err.Error())
		return temp.Parse()
	}

	return temp.Parse()
}
Esempio n. 3
0
func (self *RedisCache) Set(key string, value interface{}) error {
	var err error

	_, err = self.session.Do("SET", key, value)
	if err != nil {
		glog.Error("Error:", err)
		return err
	}

	return nil
}
Esempio n. 4
0
func (self *RedisCache) HMSetStrings(key string, value []string) error {
	var err error

	_, err = self.session.Do("HMSET", redis.Args{}.Add(key).AddFlat(&value)...)
	if err != nil {
		glog.Error("Error:", err)
		return err
	}

	return nil
}
Esempio n. 5
0
func (self *RedisCache) GetStrings(key string) ([]string, error) {
	var (
		err  error
		temp interface{}
		strs []string
	)

	temp, err = self.Get(key)
	if err != nil {
		glog.Error("Error:", err)
		return []string{}, err
	}

	strs, err = redis.Strings(temp, err)
	if err != nil {
		glog.Error("Error:", err)
		return []string{}, err
	}

	return strs, err
}
Esempio n. 6
0
func (self *RedisCache) HMGetTest(key string) (Test, error) {
	var (
		err error
		t   Test
		v   []interface{}
	)

	v, err = redis.Values(self.session.Do("HGETALL", key))
	if err != nil {
		glog.Error("Error:", err)
		return t, err
	}

	err = redis.ScanStruct(v, &t)
	if err != nil {
		glog.Error("Error:", err)
		return t, err
	}

	return t, nil
}
Esempio n. 7
0
func (self *RedisCache) DbSize() (int64, error) {
	var (
		size interface{}
		err  error
	)
	size, err = self.session.Do("DBSIZE")
	if err != nil {
		glog.Error("Error:", err)
		return 0, err
	}

	return size.(int64), nil
}
Esempio n. 8
0
func (self *RedisCache) GetString(key string) (string, error) {
	var (
		err  error
		temp interface{}
	)

	temp, err = self.Get(key)
	if err != nil {
		glog.Error("Error:", err)
		return "", err
	}

	return string(temp.([]uint8)), err
}
Esempio n. 9
0
func (self *RedisCache) Get(key string) (interface{}, error) {
	var (
		err   error
		value interface{}
	)

	value, err = self.session.Do("GET", key)
	if err != nil {
		glog.Error("Error:", err)
		return nil, err
	}

	return value, nil
}
Esempio n. 10
0
func (m *Mongo) MemorySelectEventsFromKey(Key string) ([]Event, error) {
	var (
		err    error
		result *Memory
	)

	op := m.Session.DB(m.Opts.DbName).C(MEMORY_COLLECTION_NAME)
	err = op.Find(bson.M{"key": Key}).One(&result)
	if err != nil {
		glog.Error(err.Error())
		return nil, m.Error(err)
	}

	return result.Value, nil
}
Esempio n. 11
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
}
Esempio n. 12
0
func (m *Mongo) MemorySelectOneEventFromKeyRandom(Key string) (Event, error) {
	var (
		err    error
		event  Event
		events []Event
	)

	events, err = m.MemorySelectEventsFromKey(Key)
	if err != nil {
		glog.Error(err.Error())
		return event, m.Error(err)
	}

	if len(events) > 0 {
		return events[rand.Intn(len(events))], nil
	} else {
		return event, errors.New("not found")
	}
}
Esempio n. 13
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
	}
}