-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
44 lines (32 loc) · 926 Bytes
/
main.go
File metadata and controls
44 lines (32 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"net/http"
_ "expvar"
_ "net/http/pprof"
"github.com/couchbaselabs/sgload/cmd"
"github.com/couchbaselabs/sgload/sgload"
)
func main() {
exposeExpvars(9876)
cmd.Execute()
}
// Exposes expvars on a port. Starts with startingPort, but will
// keep incrementing until it finds one. After a certain number of failed
// attempts it panics
func exposeExpvars(startingPort int) {
logger := sgload.Logger()
maxAttempts := 100
go func() {
for i := 0; i < maxAttempts; i++ {
portToTry := startingPort + i
listenUrl := fmt.Sprintf(":%v", portToTry) // eg: :9876
logger.Info("Attempting to expose expvars", "port", portToTry)
err := http.ListenAndServe(listenUrl, http.DefaultServeMux)
if err != nil {
logger.Warn("Unable to listen on port. Will try another port", "port", portToTry)
}
}
panic(fmt.Sprintf("Unable to bind to any ports for expvars"))
}()
}