类似Go语言中的Channel通信方式的网络服务包:Libchan
jopen
10年前
Libchan是一个超轻量级的网络库,它可以让网络服务之间的通信能够与Go例程之间的通信渠道一样。
- 简单的消息传递
- 同步并发编程
- 嵌套,通道可以传输通道
ibchan支持以下传输方式:
- In-memory Go channel
- Unix socket
- Raw TCP
- TLS
- HTTP2/SPDY
- Websocket
Example usage
下面是实现基本的RPC样式的请求/响应的例子。我们忽略错误处理,以简洁证明的其核心概念。
在客户端中:
var ch libchan.Sender // Send a message, indicate that we want a return channel to be automatically created ret1, err := ch.Send(&libchan.Message{Data: []byte("request 1!"), Ret: libchan.RetPipe}) // Send another message on the same channel ret2, err := ch.Send(&libchan.Message{Data: []byte("request 2!"), Ret: libchan.RetPipe}) // Wait for an answer from the first request. Set flags to zero // to indicate we don't want a nested return channel. msg, err := ret1.Receive(0)
在服务器中:
var ch libchan.Receiver // Wait for messages in a loop // Set the return channel flag to indicate that we // want to receive nested channels (if any). // Note: we don't send a nested return channel, but we could. for { msg, err := ch.Receive(libchan.Ret) msg.Ret.Send(&libchan.Message{Data: []byte("this is an extremely useful response")}); }