Various fixes in cluster.go

This commit is contained in:
Andreas Neue 2016-08-12 08:24:58 +02:00
parent 9e6de5ac37
commit 262da96e46
1 changed files with 10 additions and 9 deletions

View File

@ -1,4 +1,3 @@
could not parse "irc.go": /home/an/src/iqcomm_/src/irc/irc.go:128:20: expected boolean expression, found simple statement (missing parentheses around composite literal?)
// vim:ts=4:sts=4:sw=4:noet:tw=72 // vim:ts=4:sts=4:sw=4:noet:tw=72
package ircd package ircd
@ -16,7 +15,7 @@ type ClusterConnector struct {
subs map[string]*nats.Subscription subs map[string]*nats.Subscription
} }
func NewClusterConnector(servers string, ssl bool) *NatsConnector { func NewClusterConnector(servers string, ssl bool) *ClusterConnector {
opts := nats.DefaultOptions opts := nats.DefaultOptions
opts.Servers = strings.Split(servers, ",") opts.Servers = strings.Split(servers, ",")
for i, s := range opts.Servers { for i, s := range opts.Servers {
@ -28,29 +27,31 @@ func NewClusterConnector(servers string, ssl bool) *NatsConnector {
// foo // foo
} }
subs := make(map[string]*nats.Subscription) subs := make(map[string]*nats.Subscription)
return &NatsConnector{conn: conn, subs: subs} return &ClusterConnector{conn: conn, subs: subs}
} }
func (cc *ClusterConnector) Subscribe(subj string, ch chan *irc.Message) { func (cc *ClusterConnector) Subscribe(subj string, ch chan *irc.Message) {
if _, exists := c.subs[subj]; exists { if _, exists := cc.subs[subj]; exists {
return return
} }
sub, err := c.natsConn.Subscribe(subj, func(n *nats.Msg) { sub, err := cc.conn.Subscribe(subj, func(n *nats.Msg) {
m := irc.Parse(string(n.Data)) m := irc.Parse(string(n.Data))
ch <- m ch <- m
}) })
if err != nil { if err != nil {
c.subs[subj] = sub cc.subs[subj] = sub
} }
} }
func (cc *ClusterConnector) Unsubscribe(subj string) { func (cc *ClusterConnector) Unsubscribe(subj string) {
cc.conn.Unsubscribe(subj) if sub, exists := cc.subs[subj]; exists {
sub.Unsubscribe()
delete(cc.subs, subj) delete(cc.subs, subj)
}
} }
func (cc *ClusterConnector) Publish(msg *irc.Message) { func (cc *ClusterConnector) Publish(msg *irc.Message) {
subj := strings.ToLower(msg.Pre) subj := strings.ToLower(msg.Pre)
cc.conn.Publish(subj, []bytes(msg.String())) cc.conn.Publish(subj, []byte(msg.String()))
} }