Exemplo n.º 1
0
// connect to all services
func (p *service_pool) connect_all(directory string) {
	client := p.client_pool.Get().(*etcd.Client)
	defer func() {
		p.client_pool.Put(client)
	}()

	// get the keys under directory
	log.Info("connecting services under:", directory)
	resp, err := client.Get(directory, true, true)
	if err != nil {
		log.Error(err)
		return
	}

	// validation check
	if !resp.Node.Dir {
		log.Error("not a directory")
		return
	}

	for _, node := range resp.Node.Nodes {
		if node.Dir { // service directory
			for _, service := range node.Nodes {
				p.add_service(service.Key, service.Value)
			}
		} else {
			log.Warning("malformed service directory:", node.Key)
		}
	}
	log.Info("services add complete")
}
Exemplo n.º 2
0
//---------------------------------------------------------- 读取GridFS文件
func LoadFile(filename string) (ok bool, content []byte) {
	ms := _global_ms.Copy()
	defer ms.Close()

	buf := &bytes.Buffer{}
	file, err := ms.DB("").GridFS("fs").Open(filename)
	if err != nil {
		log.Warning("gridfs", filename, err)
		return false, nil
	}

	n, err := io.Copy(buf, file)
	if err != nil {
		log.Error("gridfs", filename, n, err)
		return false, nil
	}

	err = file.Close()
	if err != nil {
		log.Error("gridfs", filename, err)
		return false, nil
	}

	log.Trace("gridfs", filename, "load from GridFS!!")
	return true, buf.Bytes()
}
Exemplo n.º 3
0
// stream receiver
func (s *server) recv(stream GameService_StreamServer) chan *Game_Frame {
	ch := make(chan *Game_Frame, 1)
	go func() {
		for {
			in, err := stream.Recv()
			if err == io.EOF { // client closed
				close(ch)
				return
			}

			if err != nil {
				log.Critical(err)
				close(ch)
				return
			}
			select {
			case ch <- in:
			case <-time.After(RECV_TIMEOUT):
				log.Warning("recv deliver timeout")
				close(ch)
				return
			}
		}
	}()
	return ch
}
Exemplo n.º 4
0
func (s *server) dump(db *bolt.DB, changes map[uint64]bool) {
	for k := range changes {
		// marshal
		var rs *RankSet
		s.lock_read(func() {
			rs = s.ranks[k]
		})
		if rs == nil {
			log.Warning("empty rankset:", k)
			continue
		}

		// serialization and save
		bin, err := rs.Marshal()
		if err != nil {
			log.Critical("cannot marshal:", err)
			os.Exit(-1)
		}

		db.Update(func(tx *bolt.Tx) error {
			b := tx.Bucket([]byte(BOLTDB_BUCKET))
			err := b.Put([]byte(fmt.Sprint(k)), bin)
			return err
		})
	}
}
Exemplo n.º 5
0
// collect & publish to statsd
func collect() {
	tag := ""
	if hostname, err := os.Hostname(); err == nil {
		tag += hostname
	} else {
		log.Warning(SERVICE, err)
	}
	// executable name
	if exe_name, err := os.Readlink("/proc/self/exe"); err == nil {
		tag += "." + path.Base(exe_name)
	} else {
		tag += fmt.Sprintf(".%v", os.Getpid())
		log.Warning(SERVICE, err)
	}

	memstats := &runtime.MemStats{}
	runtime.ReadMemStats(memstats)

	_statter.Counter(1.0, tag+".NumGoroutine", int(runtime.NumGoroutine()))
	_statter.Counter(1.0, tag+".NumCgoCall", int(runtime.NumCgoCall()))
	if memstats.NumGC > 0 {
		_statter.Counter(1.0, tag+".NumGC", int(memstats.NumGC))
		_statter.Timing(1.0, tag+".PauseTotal", time.Duration(memstats.PauseTotalNs))
		_statter.Timing(1.0, tag+".LastPause", time.Duration(memstats.PauseNs[(memstats.NumGC+255)%256]))
		_statter.Counter(1.0, tag+".Alloc", int(memstats.Alloc))
		_statter.Counter(1.0, tag+".TotalAlloc", int(memstats.TotalAlloc))
		_statter.Counter(1.0, tag+".Sys", int(memstats.Sys))
		_statter.Counter(1.0, tag+".Lookups", int(memstats.Lookups))
		_statter.Counter(1.0, tag+".Mallocs", int(memstats.Mallocs))
		_statter.Counter(1.0, tag+".Frees", int(memstats.Frees))
		_statter.Counter(1.0, tag+".HeapAlloc", int(memstats.HeapAlloc))
		_statter.Counter(1.0, tag+".HeapSys", int(memstats.HeapSys))
		_statter.Counter(1.0, tag+".HeapIdle", int(memstats.HeapIdle))
		_statter.Counter(1.0, tag+".HeapInuse", int(memstats.HeapInuse))
		_statter.Counter(1.0, tag+".HeapReleased", int(memstats.HeapReleased))
		_statter.Counter(1.0, tag+".HeapObjects", int(memstats.HeapObjects))
		_statter.Counter(1.0, tag+".StackInuse", int(memstats.StackInuse))
		_statter.Counter(1.0, tag+".StackSys", int(memstats.StackSys))
		_statter.Counter(1.0, tag+".MSpanInuse", int(memstats.MSpanInuse))
		_statter.Counter(1.0, tag+".MSpanSys", int(memstats.MSpanSys))
		_statter.Counter(1.0, tag+".MCacheInuse", int(memstats.MCacheInuse))
		_statter.Counter(1.0, tag+".MCacheSys", int(memstats.MCacheSys))
		_statter.Counter(1.0, tag+".BuckHashSys", int(memstats.BuckHashSys))
		_statter.Counter(1.0, tag+".GCSys", int(memstats.GCSys))
		_statter.Counter(1.0, tag+".OtherSys", int(memstats.OtherSys))
	}
}
Exemplo n.º 6
0
func main() {
	// to catch all uncaught panic
	defer utils.PrintPanicStack()

	// open profiling
	go func() {
		log.Info(http.ListenAndServe("0.0.0.0:6060", nil))
	}()

	// set log prefix
	log.SetPrefix(SERVICE)

	// resolve address & start listening
	tcpAddr, err := net.ResolveTCPAddr("tcp4", _port)
	checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	log.Info("listening on:", listener.Addr())

	// startup
	startup()

LOOP:
	// loop accepting
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			log.Warning("accept failed:", err)
			continue
		}
		go handleClient(conn) // start a goroutine for every incoming connection for reading

		// check server close signal
		select {
		case <-die:
			listener.Close()
			break LOOP
		default:
		}
	}

	// server closed, wait forever
	// other options:
	// select{} 	-- may cause deadlock detected error, not tested yet
	for {
		<-time.After(time.Second)
	}
}
Exemplo n.º 7
0
//---------------------------------------------------------- 删除GridFS文件
func RemoveFile(filename string) bool {
	ms := _global_ms.Copy()
	defer ms.Close()

	gridfs := ms.DB("").GridFS("fs")

	// 删除同名文件
	err := gridfs.Remove(filename)
	if err != nil {
		log.Warning("gridfs", filename, err)
		return false
	}

	log.Trace("gridfs", filename, "removed from GridFS!!")
	return true
}
Exemplo n.º 8
0
Arquivo: main.go Projeto: CowLeo/agent
func main() {
	defer utils.PrintPanicStack()
	go func() {
		log.Info(http.ListenAndServe("0.0.0.0:6060", nil))
	}()

	log.SetPrefix(SERVICE)

	// resolve
	tcpAddr, err := net.ResolveTCPAddr("tcp4", _port)
	checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	log.Info("listening on:", listener.Addr())

	// init services
	sp.Init()

	// startup
	startup()

	// loop accepting
LOOP:
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			log.Warning("accept failed:", err)
			continue
		}
		go handleClient(conn)

		// check server close signal
		select {
		case <-die:
			listener.Close()
			break LOOP
		default:
		}
	}

	// server closed, wait forever
	for {
		<-time.After(time.Second)
	}
}