Beispiel #1
0
//---------------------------------------------------------- system routine
func SysRoutine() {
	var sess Session
	sess.MQ = make(chan IPCObject, SYS_MQ_SIZE)
	gsdb.RegisterOnline(&sess, SYS_USR)

	// timer
	gc_timer := make(chan int32, 10)
	gc_timer <- 1

	for {
		select {
		case msg, ok := <-sess.MQ: // IPCObject to system routine
			if !ok {
				return
			}
			IPCRequestProxy(&sess, &msg)
		case <-gc_timer:
			runtime.GC()
			INFO("== PERFORMANCE LOG ==")
			INFO("Goroutine Count:", runtime.NumGoroutine())
			INFO("GC Summary:", GCSummary())
			INFO("Sysroutine MQ size:", len(sess.MQ))
			timer.Add(0, time.Now().Unix()+GC_INTERVAL, gc_timer)
		}
	}
}
Beispiel #2
0
func TestInspect(t *testing.T) {
	var sess Session
	sess.User = &User{Id: 1}
	gsdb.RegisterOnline(&sess, sess.User.Id)
	Inspect(1, os.Stdout)
	println("inspect field:")
	InspectField(1, ".User", os.Stdout)
	InspectField(1, ".Events", os.Stdout)
}
Beispiel #3
0
func TestIPC(t *testing.T) {
	// fake 2 user
	var sess1 Session
	sess1.User = &User{Id: 1}
	sess1.MQ = make(chan IPCObject, 10)
	var sess2 Session
	sess2.User = &User{Id: 2}
	sess2.MQ = make(chan IPCObject, 10)

	wg := &sync.WaitGroup{}
	wg.Add(2)
	go _simple_receiver(&sess1, wg)
	go _simple_receiver(&sess2, wg)

	gsdb.RegisterOnline(&sess1, sess1.User.Id)
	gsdb.RegisterOnline(&sess2, sess2.User.Id)

	if !ipc.Send(1, 2, ipc.SERVICE_PING, false, "ABC") {
		t.Fatal("ipc.Send failed")
	}
	wg.Wait()
}
Beispiel #4
0
//------------------------------------------------ 登陆后的数据加载
func LoginProc(sess *Session) bool {
	if sess.User == nil {
		return false
	}

	// 载入建筑表
	if !data_tbl.Get(estates.COLLECTION, sess.User.Id, &sess.Estates) {
		// 创建默认的建筑表
		e := &estates.Manager{}
		e.UserId = sess.User.Id
		sess.Estates = e
	} else {
		// 创建Grid
		sess.Grid = CreateGrid(sess.Estates)
	}

	if !data_tbl.Get(samples.COLLECTION, sess.User.Id, &sess.LatencySamples) {
		s := &samples.Samples{}
		s.UserId = sess.User.Id
		s.Init()
		sess.LatencySamples = s
	}

	//
	if sess.User.CountryCode == "" {
		sess.User.CountryCode = geoip.Query(sess.IP)
	}

	if sess.User.CreatedAt == 0 {
		sess.User.CreatedAt = time.Now().Unix()
	}

	// 开始计算Flush时间
	sess.LastFlushTime = time.Now().Unix()

	// 注册为在线
	gsdb.RegisterOnline(sess, sess.User.Id)

	// 最后, 载入离线消息,并push到MQ, 这里小心MQ的buffer长度,
	// 不能直接调用,有可能消息超过MQ被永远阻塞
	go LoadIPCObjects(sess.User.Id, sess.MQ)

	// 标记在线
	//sess.LoggedIn = true
	return true
}
Beispiel #5
0
//------------------------------------------------ 登陆后的数据加载
func LoginProc(sess *Session) bool {
	if sess.User == nil {
		return false
	}

	// TODO: init data structure for session, such as builds

	// set countrycode
	if sess.User.CountryCode == "" {
		sess.User.CountryCode = geoip.Query(sess.IP)
	}

	// register as online
	gsdb.RegisterOnline(sess, sess.User.Id)

	// load messages
	go LoadIPCObjects(sess.User.Id, sess.MQ)

	return true
}
Beispiel #6
0
func BenchmarkIPC(b *testing.B) {
	wg := &sync.WaitGroup{}

	for i := 1; i <= b.N; i++ {
		var sess Session
		sess.User = &User{Id: int32(i)}
		sess.MQ = make(chan IPCObject, 10)
		gsdb.RegisterOnline(&sess, sess.User.Id)
		go _simple_receiver(&sess, wg)
	}

	for i := 1; i <= b.N; i++ {
		src := rand.Int31n(int32(b.N)) + 1
		dest := rand.Int31n(int32(b.N)) + 1
		if src != dest {
			wg.Add(2)
			ipc.Send(src, dest, ipc.SERVICE_PING, false, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
		}
	}
	wg.Wait()
}