func init() { sp = &pool{} url, ok := conf.Conf[util.CONF_KEY_MONGO_URL] if !ok { util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_MONGO_URL + " in conf")) } maxSessonsStr, ok := conf.Conf[util.CONF_KEY_MONGO_MAX_SESSIONS] if !ok { util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_MONGO_MAX_SESSIONS + " in conf")) } maxSessons, err := strconv.Atoi(maxSessonsStr) util.StartupFatalErr(err) if maxSessons <= 0 { util.StartupFatalErr(errors.New(util.CONF_KEY_MONGO_MAX_SESSIONS + " must larger then 0")) } sp.sessions = make([]*mgo.Session, 0, maxSessons) sp.avaliableCh = make(chan *mgo.Session, maxSessons) s, err := mgo.Dial(url) util.StartupFatalErr(err) s.SetSafe(&mgo.Safe{}) sp.sessions = append(sp.sessions, s) sp.avaliableCh <- s for i := 1; i < maxSessons; i++ { news := s.Copy() sp.sessions = append(sp.sessions, news) sp.avaliableCh <- news } }
func init() { sp = &pool{} maxClientsStr, ok := conf.Conf[util.CONF_KEY_REDIS_MAX_CLIENTS] if !ok { util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_REDIS_MAX_CLIENTS + " in conf")) } maxClients, err := strconv.Atoi(maxClientsStr) util.StartupFatalErr(err) if maxClients <= 0 { util.StartupFatalErr(errors.New(util.CONF_KEY_REDIS_MAX_CLIENTS + " must larger then 0")) } sp.clients = make([]redis.Client, maxClients) sp.avaliableCh = make(chan redis.Client, maxClients) spec := redis.DefaultSpec().Db(util.REDIS_DATABASE) var c redis.Client for i := 0; i < maxClients; i++ { c, err = redis.NewSynchClientWithSpec(spec) util.StartupFatalErr(err) sp.clients = append(sp.clients, c) sp.avaliableCh <- c } }
func init() { logpath, ok := conf.Conf[util.CONF_KEY_LOGPATH] if !ok { util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_LOGPATH + " in conf")) } var err error logfile, err = os.OpenFile(logpath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0664) util.StartupFatalErr(err) loglevelstr, ok := conf.Conf[util.CONF_KEY_LOGLEVEL] if !ok { util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_LOGLEVEL + " in conf")) } loglevel, err = strconv.Atoi(loglevelstr) util.StartupFatalErr(err) }
func init() { flag.Parse() confPath := *flag.String("conf", path.Join(os.Getenv("HOME"), "conf/chirp.conf"), "conf file path") var err error Conf, err = goprop.Load(confPath) util.StartupFatalErr(err) fmt.Printf("%+v\n", Conf) }
func main() { port, ok := conf.Conf[util.CONF_KEY_SERVER_LISTEN_PORT] if !ok { util.StartupFatalErr(errors.New("Must contain " + util.CONF_KEY_SERVER_LISTEN_PORT + " in conf")) } fs := new(feedservice.FeedService) rpc.Register(fs) l, err := net.Listen("tcp", ":"+port) util.StartupFatalErr(err) log.Info("server start at port: " + port) for { conn, err := l.Accept() log.Infof("conn from remote: %+v", conn.RemoteAddr()) if err != nil { log.Errorf("accept error: %s", err.Error()) } go rpc.ServeCodec(jsonrpc.NewServerCodec(conn)) } }