-
-
Save karalabe/3cf3a2eb47ee328579e59a0778ed5d94 to your computer and use it in GitHub Desktop.
TorfluxProxy: Anonymous metrics from Go - Demo 6
This file contains 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 ( | |
"io" | |
"io/ioutil" | |
"math/rand" | |
"time" | |
"github.com/influxdata/influxdb/client/v2" | |
"github.com/rcrowley/go-metrics" | |
) | |
var ( | |
clientCounter = metrics.NewCounter() | |
egressMeter = metrics.NewMeter() | |
) | |
func main() { | |
// Connect to the remote InfluxDB time series database | |
api, err := client.NewHTTPClient(client.HTTPConfig{ | |
Addr: "http://127.0.0.1:8086", | |
}) | |
if err != nil { | |
panic(err) | |
} | |
// Start a goroutine to report the stats to the user | |
go func() { | |
for { | |
// Convert the two collected metrics into InfluxDB data points | |
conns, _ := client.NewPoint("connections", nil, map[string]interface{}{ | |
"count": clientCounter.Count(), | |
}, time.Now()) | |
egress, _ := client.NewPoint("bandwidth", nil, map[string]interface{}{ | |
"egress": egressMeter.Rate1(), | |
}, time.Now()) | |
// Aggregate all the measurements into a single batch and push them | |
points, _ := client.NewBatchPoints(client.BatchPointsConfig{Database: "mydb"}) | |
points.AddPoint(conns) | |
points.AddPoint(egress) | |
api.Write(points) | |
time.Sleep(5 * time.Second) | |
} | |
}() | |
// Simulate a new client connecting every half a second, requesting data. To | |
// avoid going out of memory, lets also set a max connection limit of 10. | |
slots := make(chan struct{}, 10) | |
for { | |
// Fetch the next connection slot to avoid overloading | |
slots <- struct{}{} | |
// Simulate a random client retrieving some file from the server | |
go func() { | |
serve(ioutil.Discard) | |
<-slots // return the slot | |
}() | |
time.Sleep(500 * time.Millisecond) | |
} | |
} | |
// serve is a simple file server simulator that writes random-length blobs to the | |
// provided streams bfore returning. | |
func serve(conn io.Writer) { | |
// Track this client while it's connected | |
clientCounter.Inc(1) | |
defer clientCounter.Dec(1) | |
// Stream a randomly sized file in 512KB chunks to the connection | |
file := make([]byte, rand.Intn(16*1024*1024)) | |
// Simulate the peer having a certain bandwidth, delaying each chunk a bit | |
delay := time.Duration(rand.Intn(500)) * time.Millisecond | |
for len(file) > 512*1024 { | |
egressMeter.Mark(512 * 1024) | |
conn.Write(file[:512*1024]) | |
file = file[512*1024:] | |
time.Sleep(delay) | |
} | |
egressMeter.Mark(int64(len(file))) | |
conn.Write(file) // Yeah, no more sleep, insta-send! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment