Skip to content

Instantly share code, notes, and snippets.

@chendo
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chendo/223488399b25a648d2c9 to your computer and use it in GitHub Desktop.
Save chendo/223488399b25a648d2c9 to your computer and use it in GitHub Desktop.
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