-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from YanHeDoki/master
RequestPool 模式添加开关,补充了一个example
- Loading branch information
Showing
7 changed files
with
224 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aceld/zinx/zconf" | ||
"github.com/aceld/zinx/ziface" | ||
"github.com/aceld/zinx/znet" | ||
) | ||
|
||
// 如果不使用对象池模式则可以直接传递但是产生大量的 Request 对象 | ||
|
||
func NoPoll1(request ziface.IRequest) { | ||
request.Set("num", 1) | ||
go NoPoll2(request) | ||
} | ||
|
||
func NoPoll2(request ziface.IRequest) { | ||
time.Sleep(time.Second * 3) | ||
get, _ := request.Get("num") | ||
fmt.Printf("num:%v \n", get) | ||
|
||
} | ||
|
||
func NoPoll4(request ziface.IRequest) { | ||
// 非对象池模式永远不会影响原本的 Request | ||
request.Set("num", 3) | ||
} | ||
|
||
func main() { | ||
|
||
// 开启 Request 对象池模式 | ||
server := znet.NewUserConfServer(&zconf.Config{RouterSlicesMode: true, TCPPort: 8999, Host: "127.0.0.1", RequestPoolMode: false}) | ||
server.AddRouterSlices(1, NoPoll1) | ||
server.AddRouterSlices(2, NoPoll4) | ||
server.Serve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aceld/zinx/zconf" | ||
"github.com/aceld/zinx/ziface" | ||
"github.com/aceld/zinx/znet" | ||
) | ||
|
||
func Poll1(request ziface.IRequest) { | ||
// 如果需要连接信息 | ||
request.Set("conn", request.GetConnection()) | ||
request.Set("num", 1) | ||
fmt.Printf("request 1 addr:%p,conn:%p \n", &request, request.GetConnection()) | ||
|
||
// 需要新线程同时也需要上下文的情况,则需要调用 copy 方法拷贝一份 | ||
cp := request.Copy() | ||
go Poll2(cp) | ||
|
||
// 如果不使用 copy 方法拷贝对象则会出现同一个对象但是信息可能不一致的问题,不启动 poll2 会更直接 | ||
go Poll3(request) | ||
} | ||
|
||
func Poll2(request ziface.IRequest) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
// 接收一个panic | ||
fmt.Println(err) | ||
} | ||
|
||
}() | ||
get_conn, ok := request.Get("conn") | ||
if ok { | ||
// 如果直接取用则会导致空指针 | ||
request.GetConnection().GetConnID() | ||
// 打印出的 Request 对象的地址是不一致的 | ||
conn := get_conn.(ziface.IConnection) | ||
fmt.Printf("request copy addr:%p,conn:%p \n", &request, conn) | ||
// conn.sendMsg() | ||
} | ||
} | ||
|
||
// 如果请求的次数多,则开启对象池且直接传递不copy Request 就可能导致值不一致 | ||
func Poll3(request ziface.IRequest) { | ||
time.Sleep(time.Second * 3) | ||
get, _ := request.Get("num") | ||
// 池化对象如果直接传递被影响可能随机打印被修改的值 3 | ||
fmt.Printf("num:%v \n", get) | ||
|
||
} | ||
|
||
func Poll4(request ziface.IRequest) { | ||
// 影响原本的 request 对象 | ||
request.Set("num", 3) | ||
} | ||
|
||
func main() { | ||
|
||
// 开启 Request 对象池模式 | ||
server := znet.NewUserConfServer(&zconf.Config{RouterSlicesMode: true, TCPPort: 8999, Host: "127.0.0.1", RequestPoolMode: true}) | ||
server.AddRouterSlices(1, Poll1) | ||
server.AddRouterSlices(2, Poll4) | ||
server.Serve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/aceld/zinx/examples/zinx_client/c_router" | ||
"github.com/aceld/zinx/ziface" | ||
"github.com/aceld/zinx/zlog" | ||
"github.com/aceld/zinx/znet" | ||
) | ||
|
||
// Custom business logic of the client (客户端自定义业务) | ||
func business(conn ziface.IConnection) { | ||
|
||
for i := 0; i < 100; i++ { | ||
err := conn.SendMsg(1, []byte("Ping...[FromClient]")) | ||
if err != nil { | ||
fmt.Println(err) | ||
zlog.Error(err) | ||
break | ||
} | ||
|
||
err = conn.SendMsg(2, []byte("Ping...[FromClient]")) | ||
if err != nil { | ||
fmt.Println(err) | ||
zlog.Error(err) | ||
break | ||
} | ||
|
||
} | ||
} | ||
|
||
// Function to execute when the connection is created (创建连接的时候执行) | ||
func DoClientConnectedBegin(conn ziface.IConnection) { | ||
zlog.Debug("DoConnecionBegin is Called ... ") | ||
|
||
// Set two connection properties after the connection is created (设置两个链接属性,在连接创建之后) | ||
conn.SetProperty("Name", "刘丹冰Aceld") | ||
conn.SetProperty("Home", "https://yuque.com/aceld") | ||
|
||
go business(conn) | ||
} | ||
|
||
// Function to execute when the connection is lost (连接断开的时候执行) | ||
func DoClientConnectedLost(conn ziface.IConnection) { | ||
// Get the Name and Home properties of the connection before it is destroyed | ||
// (在连接销毁之前,查询conn的Name,Home属性) | ||
if name, err := conn.GetProperty("Name"); err == nil { | ||
zlog.Debug("Conn Property Name = ", name) | ||
} | ||
|
||
if home, err := conn.GetProperty("Home"); err == nil { | ||
zlog.Debug("Conn Property Home = ", home) | ||
} | ||
|
||
zlog.Debug("DoClientConnectedLost is Called ... ") | ||
} | ||
|
||
func main() { | ||
// Create a client handle using Zinx's Method (创建一个Client句柄,使用Zinx的方法) | ||
client := znet.NewClient("127.0.0.1", 8999) | ||
|
||
// Set the business logic to execute when the connection is created or lost | ||
// (添加首次建立链接时的业务) | ||
client.SetOnConnStart(DoClientConnectedBegin) | ||
client.SetOnConnStop(DoClientConnectedLost) | ||
|
||
// Register routers for the messages received from the server | ||
// (注册收到服务器消息业务路由) | ||
client.AddRouter(2, &c_router.PingRouter{}) | ||
client.AddRouter(3, &c_router.HelloRouter{}) | ||
|
||
// Start the client | ||
client.Start() | ||
|
||
// close | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, os.Interrupt, os.Kill) | ||
sig := <-c | ||
fmt.Println("===exit===", sig) | ||
client.Stop() | ||
time.Sleep(time.Second * 2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters