Exemplo n.º 1
0
func NewServer(cfg *config.Config) (*Server, error) {
	s := new(Server)

	s.cfg = cfg

	s.addr = cfg.Addr
	s.user = cfg.User
	s.password = cfg.Password

	if err := s.parseNodes(); err != nil {
		return nil, err
	}

	if err := s.parseSchemas(); err != nil {
		return nil, err
	}

	var err error
	netProto := "tcp"
	if strings.Contains(netProto, "/") {
		netProto = "unix"
	}
	s.listener, err = net.Listen(netProto, s.addr)

	if err != nil {
		return nil, err
	}

	log.Info("Server run MySql Protocol Listen(%s) at [%s]", netProto, s.addr)
	return s, nil
}
Exemplo n.º 2
0
func (c *conn) onRead() {
	defer func() {
		if err := recover(); err != nil {
			buf := make([]byte, 1024)
			buf = buf[:runtime.Stack(buf, false)]
			log.Fatal("crash %v:%s", err, buf)
		}
	}()

	for {
		p, err := c.decoder.Decode()
		if err != nil {
			if err != io.EOF {
				log.Info("on read error %v", err)
			}
			return
		}

		switch p.Method {
		case proto.Publish:
			err = c.handlePublish(p)
		case proto.Bind:
			err = c.handleBind(p)
		case proto.Unbind:
			err = c.handleUnbind(p)
		case proto.Ack:
			err = c.handleAck(p)
		case proto.Heartbeat:
			c.lastUpdate = time.Now().Unix()
		default:
			log.Info("invalid proto method %d", p.Method)
			return
		}

		if err != nil {
			c.writeError(err)
		}
	}
}
Exemplo n.º 3
0
func (c *conn) checkKeepAlive() {
	var f func()
	f = func() {
		if time.Now().Unix()-c.lastUpdate > int64(1.5*float32(c.app.cfg.KeepAlive)) {
			log.Info("keepalive timeout")
			c.c.Close()
			return
		} else {
			time.AfterFunc(time.Duration(c.app.cfg.KeepAlive)*time.Second, f)
		}
	}

	time.AfterFunc(time.Duration(c.app.cfg.KeepAlive)*time.Second, f)
}
Exemplo n.º 4
0
Arquivo: main.go Projeto: lengy/mixer
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	flag.Parse()

	if len(*configFile) == 0 {
		log.Error("must use a config file")
		return
	}

	cfg, err := config.ParseConfigFile(*configFile)
	if err != nil {
		log.Error(err.Error())
		return
	}

	if *logLevel != "" {
		setLogLevel(*logLevel)
	} else {
		setLogLevel(cfg.LogLevel)
	}

	var svr *proxy.Server
	svr, err = proxy.NewServer(cfg)
	if err != nil {
		log.Error(err.Error())
		return
	}

	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)

	go func() {
		sig := <-sc
		log.Info("Got signal [%d] to exit.", sig)
		svr.Close()
	}()

	svr.Run()
}
Exemplo n.º 5
0
func (n *Node) checkMaster() {
	n.Lock()
	db := n.db
	n.Unlock()

	if db == nil {
		log.Info("no master avaliable")
		return
	}

	if err := db.Ping(); err != nil {
		log.Error("%s ping master %s error %s", n, db.Addr(), err.Error())
	} else {
		n.lastMasterPing = time.Now().Unix()
		return
	}

	if int64(n.downAfterNoAlive) > 0 && time.Now().Unix()-n.lastMasterPing > int64(n.downAfterNoAlive) {
		log.Error("%s down master db %s", n, n.master.Addr())

		n.downMater()
	}
}