類似Go語言中的Channel通信方式的網絡服務包:Libchan
Libchan是一個超輕量級的網絡庫,它可以讓網絡服務之間的通信能夠與Go例程之間的通信渠道一樣。
- 簡單的消息傳遞
- 同步并發編程
- 嵌套,通道可以傳輸通道 </ul>
- In-memory Go channel
- Unix socket
- Raw TCP
- TLS
- HTTP2/SPDY
- Websocket
ibchan支持以下傳輸方式:
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")});
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!