Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/intob/chamux
net.Conn muxer for Go, based on channels
https://github.com/intob/chamux
channels concurrency connection multiplexer mux network tcp
Last synced: about 2 months ago
JSON representation
net.Conn muxer for Go, based on channels
- Host: GitHub
- URL: https://github.com/intob/chamux
- Owner: intob
- Created: 2022-02-23T20:26:11.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-24T21:47:37.000Z (almost 3 years ago)
- Last Synced: 2024-06-20T05:05:13.232Z (7 months ago)
- Topics: channels, concurrency, connection, multiplexer, mux, network, tcp
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# chamux
A simple net.Conn multiplexer based on named topics & channels. Subscribing to a topic returns a channel on which updates will be sent.## Usage
```go
func dialer() {
// ignore the errors for brevity (it's an example)
conn, _ := net.Dial("unix", "/tmp/example")// implement chamux.Serializer to use another
mc := chamux.NewMConn(conn, chamux.Gob{}, chamux.Options{})// get a channel for a topic named: coffee
topic := chamux.NewTopic("coffee")
subscription := topic.Subscribe()
mc.AddTopic(&topic)sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)loop:
for {
select {
case <-sigint:
mc.Close()
break loop
case msg := <-subscription:
log.Println("something about coffee:", string(msg))
}
}func listener() {
listener, _ := net.Listen("unix", "/tmp/example")
for {
conn, _ := listener.Accept()
mc := chamux.NewMConn(conn, chamux.Gob{}, chamux.Options{})// send lots of messages about coffee
go func(chamux.MConn) {
for {
msg := []byte("we need more coffee")
frame := chamux.NewFrame(msg, "coffee")
mc.Publish(frame)
}
}(mc)
}
}
```## Serialization
This package exports `Gob{}`. If you want to use another encoding, simply implement `Serializer`.
```go
type Serializer interface {
Serialize(f *Frame) ([]byte, error)
Deserialize(f []byte) (*Frame, error)
}
```