Skip to content

Commit

Permalink
=优化rpc通信
Browse files Browse the repository at this point in the history
  • Loading branch information
liangdas committed Dec 19, 2017
1 parent 13818b7 commit e741a24
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
24 changes: 15 additions & 9 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type DefaultApp struct {
version string
serverList map[string]module.ServerSession
settings conf.Config
processId string
routes map[string]func(app module.App, Type string, hash string) module.ServerSession
defaultRoutes func(app module.App, Type string, hash string) module.ServerSession
rpcserializes map[string]module.RPCSerialize
Expand All @@ -87,7 +88,7 @@ func (app *DefaultApp) Run(debug bool, mods ...module.Module) error {
ProcessID := flag.String("pid", "development", "Server ProcessID?")
Logdir := flag.String("log", "", "Log file directory?")
flag.Parse() //解析输入的参数

app.processId=*ProcessID
ApplicationDir := ""
if *wdPath != "" {
_, err := os.Open(*wdPath)
Expand Down Expand Up @@ -208,13 +209,16 @@ func (app *DefaultApp) OnInit(settings conf.Config) error {
if err != nil {
continue
}
if moduel.Rabbitmq != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewRabbitmqClient(moduel.Rabbitmq)
}
if moduel.Redis != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewRedisClient(moduel.Redis)
if app.GetProcessID()!=moduel.ProcessID{
//同一个ProcessID下的模块直接通过local channel通信就可以了
if moduel.Rabbitmq != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewRabbitmqClient(moduel.Rabbitmq)
}
if moduel.Redis != nil {
//如果远程的rpc存在则创建一个对应的客户端
client.NewRedisClient(moduel.Redis)
}
}
session := basemodule.NewServerSession(moduel.Id, Type, client)
app.serverList[moduel.Id] = session
Expand Down Expand Up @@ -284,7 +288,9 @@ func (app *DefaultApp) GetRouteServer(filter string, hash string) (s module.Serv
func (app *DefaultApp) GetSettings() conf.Config {
return app.settings
}

func (app *DefaultApp) GetProcessID() string {
return app.processId
}
func (app *DefaultApp) RpcInvoke(module module.RPCModule, moduleType string, _func string, params ...interface{}) (result interface{}, err string) {
server, e := app.GetRouteServer(moduleType, module.GetServerId())
if e != nil {
Expand Down
6 changes: 3 additions & 3 deletions gate/base/gate_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (h *handler) Push(Sessionid string, Settings map[string]string) (result gat
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserid() != "" {
err := h.gate.GetStorageHandler().Storage(agent.(gate.Agent).GetSession().GetUserid(), agent.(gate.Agent).GetSession())
if err != nil {
log.Error("gate session storage failure")
log.Warning("gate session storage failure : %s",err.Error())
}
}

Expand All @@ -186,7 +186,7 @@ func (h *handler) Set(Sessionid string, key string, value string) (result gate.S
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserid() != "" {
err := h.gate.GetStorageHandler().Storage(agent.(gate.Agent).GetSession().GetUserid(), agent.(gate.Agent).GetSession())
if err != nil {
log.Error("gate session storage failure")
log.Error("gate session storage failure : %s",err.Error())
}
}

Expand All @@ -208,7 +208,7 @@ func (h *handler) Remove(Sessionid string, key string) (result interface{}, err
if h.gate.GetStorageHandler() != nil && agent.(gate.Agent).GetSession().GetUserid() != "" {
err := h.gate.GetStorageHandler().Storage(agent.(gate.Agent).GetSession().GetUserid(), agent.(gate.Agent).GetSession())
if err != nil {
log.Error("gate session storage failure")
log.Error("gate session storage failure :%s",err.Error())
}
}

Expand Down
1 change: 1 addition & 0 deletions module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type App interface {
*/
ProtocolMarshal(Result interface{},Error string)(ProtocolMarshal,string)
NewProtocolMarshal(data []byte)(ProtocolMarshal)
GetProcessID()string
}

type Module interface {
Expand Down
2 changes: 1 addition & 1 deletion utils/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (this RedisFactory) GetPool(url string) *redis.Pool {
// 最大的空闲连接数,表示即使没有redis连接时依然可以保持N个空闲的连接,而不被清除,随时处于待命状态
MaxIdle: 10,
// 最大的激活连接数,表示同时最多有N个连接 ,为0事表示没有限制
MaxActive: 0,
MaxActive: 100,
//最大的空闲连接等待时间,超过此时间后,空闲连接将被关闭
IdleTimeout: 240 * time.Second,
// 当链接数达到最大后是否阻塞,如果不的话,达到最大后返回错误
Expand Down

0 comments on commit e741a24

Please sign in to comment.