Last active
August 29, 2015 14:06
-
-
Save chendo/223488399b25a648d2c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/apcera/nats" | |
"runtime" | |
"sync/atomic" | |
"time" | |
) | |
func main() { | |
runtime.GOMAXPROCS(1) | |
pubConn, err := nats.Connect("nats://localhost:4222") | |
subConn, err := nats.Connect("nats://localhost:4222") | |
if err != nil { | |
panic(err) | |
} | |
sent := uint64(0) | |
received := uint64(0) | |
subConn.Subscribe("test", func(msg *nats.Msg) { | |
atomic.AddUint64(&received, 1) | |
}) | |
subConn.Flush() | |
go func() { | |
for _ = range time.NewTicker(time.Second).C { | |
s := atomic.LoadUint64(&sent) | |
r := atomic.LoadUint64(&received) | |
fmt.Printf("Sent: %d\tRecv: %d\tLost: %d\n", s, r, s-r) | |
atomic.StoreUint64(&received, 0) | |
atomic.StoreUint64(&sent, 0) | |
} | |
}() | |
for { | |
for i := 0; i < 1000; i++ { | |
err = pubConn.Publish("test", []byte("yo")) | |
if err != nil { | |
fmt.Printf("Error: %s\n", err) | |
} else { | |
atomic.AddUint64(&sent, 1) | |
} | |
} | |
pubConn.Flush() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment