Beispiel #1
0
package dao

import (
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/mongo"
	"github.com/xing4git/chirp/util"
	"labix.org/v2/mgo"
)

var log = chirplog.New("dao")

func collection(s *mongo.Session, collectionName string) *mgo.Collection {
	db := s.DB(util.MONGO_DATABASE)
	return db.C(collectionName)
}
Beispiel #2
0
package main

import (
	"errors"
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/conf"
	"github.com/xing4git/chirp/service/feedservice"
	"github.com/xing4git/chirp/util"
	"net"
	"net/rpc"
	"net/rpc/jsonrpc"
)

var log = chirplog.New("server.main")

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())
Beispiel #3
0
package feedservice

import (
	"fmt"
	"github.com/xing4git/chirp/backend"
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/dao"
	"github.com/xing4git/chirp/model"
	"labix.org/v2/mgo/bson"
)

var log = chirplog.New("FeedService")

type FeedService struct{}

func (fs *FeedService) Create(feed model.Feed, ret *model.Feed) error {
	logstr := fmt.Sprintf("create feed: %+v", feed)
	log.Info(logstr)
	tmp, err := dao.InsertFeed(feed)
	if err != nil {
		log.Errorf("%s, error: ", logstr, err.Error())
		return err
	}
	*ret = tmp
	log.Infof("create feed result: %+v", tmp)
	backend.CreateFeed(tmp)
	return nil
}

func (fs *FeedService) Delete(fid string, ret *model.Feed) error {
	logstr := fmt.Sprintf("delete feed: %s", fid)
Beispiel #4
0
package backend

import (
	"fmt"
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/dao"
	"github.com/xing4git/chirp/dao/redisdao"
	"github.com/xing4git/chirp/model"
	"github.com/xing4git/chirp/util"
)

var log = chirplog.New("backend")

func CreateFeed(feed model.Feed) {
	f := func() {
		logstr := fmt.Sprintf("create feed: %+v", feed)
		// insert into self timeline
		log.Infof("%s, insert into self timeline", logstr)
		err := redisdao.InsertTimeline(feed.Uid, feed)
		if err != nil {
			log.Errorf("%s, insert into self timeline error: %s", logstr, err.Error())
		}

		// insert into self feeds list
		log.Infof("%s, insert into self feeds list", logstr)
		err = redisdao.InsertUserFeed(feed.Uid, feed)
		if err != nil {
			log.Errorf("%s, insert into self feeds list error: %s", logstr, err.Error())
		}

		insertIntoFansTimeline(feed)
Beispiel #5
0
package mongo

import (
	"errors"
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/conf"
	"github.com/xing4git/chirp/util"
	"labix.org/v2/mgo"
	"strconv"
	"time"
)

var log = chirplog.New("mongo")

type pool struct {
	sessions    []*mgo.Session
	avaliableCh chan *mgo.Session
}

type Session struct {
	start time.Time
	*mgo.Session
	sp *pool
}

func (s *Session) Close() {
	if s.Session != nil {
		s.sp.avaliableCh <- s.Session
		s.Session = nil
		log.Debugf("session usage time: %f ms", time.Now().Sub(s.start).Seconds()*1e3)
	}
Beispiel #6
0
package redisdao

import (
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/model"
	"github.com/xing4git/chirp/r"
	"github.com/xing4git/chirp/util"
	"labix.org/v2/mgo/bson"
	// "github.com/alphazero/redis"
	"errors"
)

var log = chirplog.New("redisdao")

func RemoveKey(key string) (err error) {
	log.Warnf("remove key: %s", key)
	c := r.GetClient()
	defer c.Quit()

	success, err := c.Del(key)
	if err != nil {
		return err
	} else if !success {
		return errors.New("delete key " + key + " failed")
	}
	return nil
}

func InsertTimeline(uid string, feed model.Feed) (err error) {
	key := util.REDIS_USER_TIMELINE + uid
	return redisInsertFeed(key, feed)
Beispiel #7
0
package main

import (
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/dao"
	"github.com/xing4git/chirp/model"
	"labix.org/v2/mgo/bson"
)

var log = chirplog.New("test")

func main() {
	// testQueryFeed()
	// testRemoveFeed()
	// testQueryBatchFeed()
	// testConcurrency()
	// testInsertFeedLoc()
	// testQueryFeedLoc()
	// testRemoveFeedLoc()
	// testWithinBoxFeedLoc()
	// testWithinCircleFeedLoc()
	// testInsertTimeline()
	testTimelineFeeds()
}

func testTimelineFeeds() {
	uid := "123456abcdef"
	rets, err := dao.TimelineFeeds(uid, 0, 19876544, 10)
	if err != nil {
		log.Fatal(err)
	}
Beispiel #8
0
package r

import (
	"errors"
	"github.com/alphazero/redis"
	"github.com/xing4git/chirp/chirplog"
	"github.com/xing4git/chirp/conf"
	"github.com/xing4git/chirp/util"
	"strconv"
	"time"
)

var log = chirplog.New("r/r.go")

type pool struct {
	clients     []redis.Client
	avaliableCh chan redis.Client
}

type Client struct {
	start time.Time
	redis.Client
	sp *pool
}

func (c *Client) Quit() {
	if c.Client != nil {
		c.sp.avaliableCh <- c.Client
		c.Client = nil
		log.Debugf("client usage time: %f ms", time.Now().Sub(c.start).Seconds()*1e3)
	}