Skip to content

Instantly share code, notes, and snippets.

@JensRantil JensRantil/go.mod
Last active May 3, 2019

Embed
What would you like to do?
Demonstrating potential memory leak.
module github.com/tink-ab/tink-backend/src/nats-streaming-pinger
require (
github.com/armon/go-metrics v0.0.0-20190423201044-2801d9688273 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/hashicorp/go-msgpack v0.5.4 // indirect
github.com/hashicorp/raft v1.0.1 // indirect
github.com/lib/pq v1.1.0 // indirect
github.com/nats-io/gnatsd v1.4.1 // indirect
github.com/nats-io/go-nats v1.7.2
github.com/nats-io/go-nats-streaming v0.4.2
github.com/nats-io/nats-streaming-server v0.14.1
github.com/nats-io/nkeys v0.0.2 // indirect
github.com/nats-io/nuid v1.0.1
github.com/prometheus/client_golang v0.9.2
go.etcd.io/bbolt v1.3.2 // indirect
golang.org/x/sys v0.0.0-20190425045458-9f0b1ff7b46a // indirect
google.golang.org/appengine v1.5.0 // indirect
)
package main
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"time"
stan "github.com/nats-io/go-nats-streaming"
"github.com/nats-io/nuid"
"github.com/prometheus/client_golang/prometheus/promhttp"
nats "github.com/nats-io/go-nats"
)
var (
connect = flag.String("connect", nats.DefaultURL, fmt.Sprintf("NATS URL. (default: %s)", nats.DefaultURL))
listen = flag.String("listen", ":8080", "The interface to listen on for Prometheus.")
pprofListen = flag.String("pprof-listen", "localhost:6060", "The interface to listen on for pprof.")
clusterName = flag.String("cluster", "test-cluster", "NATS Streaming cluster name.")
debug = flag.Bool("debug", false, "Set to true for verbose logging.")
)
const promNamespace = "stanping"
func init() {
flag.Parse()
}
func main() {
go func() {
// Start pprof.
log.Println(http.ListenAndServe(*pprofListen, nil))
}()
go startPrometheusServer()
log.Println("Started.")
// Connect 20000 times
for i := 0; i < 20000; i++ {
log.Println("Iteration:", i)
sc, err := connectToNatsStreaming(*connect, *clusterName)
if err != nil {
log.Println("Unable to connect to STAN:", err)
}
if err := sc.Close(); err != nil {
log.Fatalln("Could not close STAN:", err)
}
}
log.Println("Done iterating.")
runtime.GC()
log.Println("Done GC:ing.")
log.Println("Sleeping for 5 min to allow for `go tool pprof http://localhost:6060/debug/pprof/heap`.")
time.Sleep(5 * time.Minute)
}
func startPrometheusServer() {
promHandler := http.NewServeMux()
promHandler.Handle("/metrics", promhttp.Handler())
log.Printf("Starting Prometheus metric exporter on %s", *listen)
log.Fatalln(http.ListenAndServe(*listen, promHandler))
}
func isReadable(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
f.Close()
return true
}
func debugLog(s string, a ...string) {
if *debug {
var args []interface{}
args = append(args, interface{}(s))
for _, e := range a {
// Can this be done in a simpler way?
args = append(args, interface{}(e))
}
log.Println(args...)
}
}
func connectToNatsStreaming(addr, clust string) (stan.Conn, error) {
return stan.Connect(clust, nuid.Next())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.