Nex.Rx, and easier to use alloc mq apis

This commit is contained in:
2024-10-24 23:52:38 +08:00
parent 23d14e4e46
commit 421834ae5c
6 changed files with 60 additions and 17 deletions

32
pkg/nex/rx/mq_conn.go Normal file
View File

@ -0,0 +1,32 @@
package rx
import (
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"github.com/nats-io/nats.go"
)
type MqConn struct {
n *nex.Conn
Nt *nats.Conn
}
func NewMqConn(conn *nex.Conn) (*MqConn, error) {
c := &MqConn{
n: conn,
}
mqAddr := conn.AllocResource(nex.AllocatableResourceMq)
if mqAddr == nil {
return nil, fmt.Errorf("unable to allocate resource: message queue")
} else if addr, ok := mqAddr.(string); !ok {
return nil, fmt.Errorf("alloced mq resource address is not a string")
} else if nc, err := nats.Connect(addr); err != nil {
return nil, fmt.Errorf("unable to connect to nats server: %v", err)
} else {
c.Nt = nc
}
return c, nil
}

7
pkg/nex/rx/mq_io.go Normal file
View File

@ -0,0 +1,7 @@
package rx
import "git.solsynth.dev/hypernet/nexus/pkg/nex"
func (v *MqConn) Publish(topic string, data any) error {
return v.Nt.Publish(topic, nex.EncodeMap(data))
}