Beispiel #1
0
func (this *Engine) LoadConfigFile() *Engine {
	log.Info("Engine[%s] loading config file %s", BuildID, this.configFile)

	cf := new(engineConfig)
	var err error
	cf.Conf, err = conf.Load(this.configFile)
	if err != nil {
		panic(err)
	}

	this.conf = cf
	this.doLoadConfig()

	// RegisterHttpApi is ready
	this.setupHttpServ()

	// when config loaded, create the servants
	svr := servant.NewFunServant(config.Servants)
	this.rpcProcessor = rpc.NewFunServantProcessor(svr)
	svr.Start()

	this.peer = peer.NewPeer(this.conf.peerGroupAddr,
		this.conf.peerHeartbeatInterval, this.conf.peerDeadThreshold)

	return this
}
Beispiel #2
0
func (this *Engine) LoadConfigFile() *Engine {
	log.Debug("Loading config file %s", this.configFile)

	cf := new(engineConfig)
	var err error
	cf.Conf, err = conf.Load(this.configFile)
	if err != nil {
		panic(err)
	}

	this.conf = cf
	this.doLoadConfig()

	// RegisterHttpApi is ready
	this.setupHttpServ()

	// when config loaded, create the servants
	this.rpcProcessor = rpc.NewFunServantProcessor(servant.NewFunServant(config.Servants))

	return this
}
Beispiel #3
0
// thrift internal layer
//
// Server
// Processor (compiler genereated)
// Protocol (JSON/compact/...), what is transmitted
// Transport (TCP/HTTP/...), how is transmitted
func (this *Engine) launchRpcServe() (done chan null.NullStruct) {
	var (
		protocolFactory  thrift.TProtocolFactory
		serverTransport  thrift.TServerTransport
		transportFactory thrift.TTransportFactory
		err              error
		serverNetwork    string
	)

	switch config.Engine.Rpc.Protocol {
	case "binary":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()

	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()

	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()

	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()

	default:
		panic(fmt.Sprintf("Unknown protocol: %s", config.Engine.Rpc.Protocol))
	}

	// client-side Thrift protocol/transport stack must match
	// the server-side, otherwise you are very likely to get in trouble
	switch {
	case config.Engine.Rpc.Framed:
		// each payload is sent over the wire with a frame header containing its size
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)

	default:
		// there is no BufferedTransport in Java: only FramedTransport
		transportFactory = thrift.NewTBufferedTransportFactory(
			config.Engine.Rpc.BufferSize)
	}

	switch {
	case strings.Contains(config.Engine.Rpc.ListenAddr, "/"):
		serverNetwork = "unix"
		if config.Engine.Rpc.SessionTimeout > 0 {
			serverTransport, err = NewTUnixSocketTimeout(
				config.Engine.Rpc.ListenAddr, config.Engine.Rpc.SessionTimeout)
		} else {
			serverTransport, err = NewTUnixSocket(
				config.Engine.Rpc.ListenAddr)
		}

	default:
		serverNetwork = "tcp"
		if config.Engine.Rpc.SessionTimeout > 0 {
			serverTransport, err = thrift.NewTServerSocketTimeout(
				config.Engine.Rpc.ListenAddr, config.Engine.Rpc.SessionTimeout)
		} else {
			serverTransport, err = thrift.NewTServerSocket(
				config.Engine.Rpc.ListenAddr)
		}
	}
	if err != nil {
		panic(err)
	}

	// dial zk before startup servants
	// because proxy servant is dependent upon zk
	if config.Engine.EtcdSelfAddr != "" {
		if err := etclib.Dial(config.Engine.EtcdServers); err != nil {
			panic(err)
		} else {
			log.Debug("etcd connected: %+v", config.Engine.EtcdServers)
		}
	}

	// when config loaded, create the servants
	this.svt = servant.NewFunServantWrapper(config.Engine.Servants)
	this.rpcProcessor = rpc.NewFunServantProcessor(this.svt)
	this.svt.Start()

	this.rpcServer = NewTFunServer(this,
		config.Engine.Rpc.PreforkMode,
		this.rpcProcessor,
		serverTransport, transportFactory, protocolFactory)
	log.Info("RPC server ready at %s:%s", serverNetwork, config.Engine.Rpc.ListenAddr)

	this.launchDashboard()

	done = make(chan null.NullStruct)
	go func() {
		if err = this.rpcServer.Serve(); err != nil {
			log.Error("RPC server: %+v", err)
		}

		done <- null.Null
	}()

	return done
}