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
package ircd
@ -16,7 +15,7 @@ type ClusterConnector struct {
subs map[string]*nats.Subscription
}
func NewClusterConnector(servers string, ssl bool) *NatsConnector {
func NewClusterConnector(servers string, ssl bool) *ClusterConnector {
opts := nats.DefaultOptions
opts.Servers = strings.Split(servers, ",")
for i, s := range opts.Servers {
@ -28,29 +27,31 @@ func NewClusterConnector(servers string, ssl bool) *NatsConnector {
// foo
}
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) {
if _, exists := c.subs[subj]; exists {
if _, exists := cc.subs[subj]; exists {
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))
ch <- m
})
if err != nil {
c.subs[subj] = sub
cc.subs[subj] = sub
}
}
func (cc *ClusterConnector) Unsubscribe(subj string) {
cc.conn.Unsubscribe(subj)
delete(cc.subs, subj)
if sub, exists := cc.subs[subj]; exists {
sub.Unsubscribe()
delete(cc.subs, subj)
}
}
func (cc *ClusterConnector) Publish(msg *irc.Message) {
subj := strings.ToLower(msg.Pre)
cc.conn.Publish(subj, []bytes(msg.String()))
cc.conn.Publish(subj, []byte(msg.String()))
}