Fork: lonng/nano
- 定义 session 接口, SessionId 使用随机数(伪)建立全局唯一ID, session 实体 Set context.Value
- cluster 包修改过的已 new_* 命名,原文件保留
- timer 修改一下结构
- 进程全局调度器,修改成进程handler内,并发调度器。保证handler处理不阻塞。
- 注册组件,修改注册方法接口,采用 context 参数开头,保证兼容性 func(ctx context.Context, in *msg) error
- 优雅关闭调度器,保证当前所有消息都消费完毕在退出
- 利用Master维护各节点的心跳,保证异常节点的Unregister的执行
- 修改Pipeline,添加ctx参数,用于实现dump请求记录日志或者一些中间设置数据操作
Nano is an easy to use, fast, lightweight game server networking library for Go. It provides a core network architecture and a series of tools and libraries that can help developers eliminate boring duplicate work for common underlying logic. The goal of nano is to improve development efficiency by eliminating the need to spend time on repetitious network related programming.
Nano was designed for server-side applications like real-time games, social games, mobile games, etc of all sizes.
The simplest "nano" application as shown in the following figure, you can make powerful applications by combining different components.
In fact, the nano
application is a collection of Component , and a component is a bundle of Handler, once you register a component to nano, nano will register all methods that can be converted to Handler
to nano service container. Service was accessed by Component.Handler
, and the handler will be called while client request. The handler will receive two parameters while handling a message:
*session.Session
: corresponding a client that apply this request or notify.*protocol.FooBar
: the payload of the request.
While you had processed your logic, you can response or push message to the client by session.Response(payload)
and session.Push('eventName', payload)
, or returns error when some unexpected data received.
Nano contains built-in distributed system solution, and make you creating a distributed game server easily.
See: The distributed chat demo
The Nano will remain simple, but you can perform any operations in the component and get the desired goals. You can startup a group of Nano
application as agent to dispatch message to backend servers.
func (manager *PlayerManager) Login(s *session.Session, msg *ReqPlayerLogin) error {
var onDBResult = func(player *Player) {
manager.players = append(manager.players, player)
s.Push("PlayerSystem.LoginSuccess", &ResPlayerLogin)
}
// run slow task in new gorontine
go func() {
player, err := db.QueryPlayer(msg.PlayerId) // ignore error in demo
// handle result in main logical gorontine
nano.Invoke(func(){ onDBResult(player) })
}
return nil
}
-
English
-
简体中文
-
Javascript
-
Demo
> go1.8
go get github.com/lonng/nano
# dependencies
go get -u github.com/pingcap/check
go get -u github.com/pingcap/errors
go get -u github.com/urfave/cli
go get -u google.golang.org/protobuf/proto
go get -u github.com/gorilla/websocket
go get -u google.golang.org/grpc
# protoc
# download form: https://github.com/protocolbuffers/protobuf/releases
# protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
# delve
go install github.com/go-delve/delve/cmd/dlv@latest
go test -v ./...
# Case: PingPong
# OS: Windows 10
# Device: i5-6500 3.2GHz 4 Core/1000-Concurrent => IOPS 11W(Average)
# Other: ...
cd ./benchmark/io
go test -v -tags "benchmark"