Skip to content

Commit

Permalink
Make it impossible to specify config dir
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Nov 1, 2020
1 parent cd3499e commit 3e1a8dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
43 changes: 18 additions & 25 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,21 @@ import (
const defaultAddr = "127.0.0.1:0"

var (
mu sync.Mutex
portfile string
listener net.Listener
errLog io.Writer
mu sync.Mutex
portfile string
listener net.Listener
logWriter io.Writer
)

// Options is optional settings for the started agent.
type Options struct {
// The address the agent will be listening at.
// It must be in the form of "host:port".
// By default "127.0.0.1:0" is populated.
Addr string

// The directory to store the configuration file,
ConfigDir string

// Where to emit the error log to. By default io.Stderr is used.
ErrorLog io.Writer
// Where to emit the error log to. By default os.Stderr is used.
LogWriter io.Writer
}

// Listen starts the gosivy agent that serves the process statistics.
Expand All @@ -52,26 +50,21 @@ type Options struct {
func Listen(opts Options) error {
mu.Lock()
defer mu.Unlock()
errLog = opts.ErrorLog
if errLog == nil {
errLog = os.Stderr
logWriter = opts.LogWriter
if logWriter == nil {
logWriter = os.Stderr
}

if portfile != "" {
return fmt.Errorf("gosivy agent already listening at: %v", listener.Addr())
}

gosivyDir := opts.ConfigDir
if gosivyDir == "" {
cfgDir, err := pidfile.ConfigDir()
if err != nil {
return err
}
gosivyDir = cfgDir
cfgDir, err := pidfile.ConfigDir()
if err != nil {
return err
}

err := os.MkdirAll(gosivyDir, os.ModePerm)
if err != nil {
if err := os.MkdirAll(cfgDir, os.ModePerm); err != nil {
return err
}
gracefulShutdown()
Expand All @@ -86,7 +79,7 @@ func Listen(opts Options) error {
}
listener = ln
port := listener.Addr().(*net.TCPAddr).Port
portfile = fmt.Sprintf("%s/%d", gosivyDir, os.Getpid())
portfile = fmt.Sprintf("%s/%d", cfgDir, os.Getpid())
err = ioutil.WriteFile(portfile, []byte(strconv.Itoa(port)), os.ModePerm)
if err != nil {
return err
Expand Down Expand Up @@ -135,19 +128,19 @@ func listen() {
if err != nil {
// TODO: Find better way to check for closed connection, see: https://golang.org/issues/4373.
if !strings.Contains(err.Error(), "use of closed network connection") {
fmt.Fprintf(errLog, "gosivy: %v\n", err)
fmt.Fprintf(logWriter, "gosivy: %v\n", err)
}
if netErr, ok := err.(net.Error); ok && !netErr.Temporary() {
break
}
continue
}
if _, err := conn.Read(sig); err != nil {
fmt.Fprintf(errLog, "gosivy: %v\n", err)
fmt.Fprintf(logWriter, "gosivy: %v\n", err)
continue
}
if err := handle(conn, sig); err != nil {
fmt.Fprintf(errLog, "gosivy: %v\n", err)
fmt.Fprintf(logWriter, "gosivy: %v\n", err)
continue
}
conn.Close()
Expand Down
2 changes: 2 additions & 0 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type MemStats struct {
HeapInuse uint64
}

// NewStats gives back a Stats after getting the statistical data
// at that point in time. Undesirable to call it at high rate.
func NewStats() (*Stats, error) {
// TODO: Make it singleton if possible.
process, err := process.NewProcess(int32(os.Getpid()))
Expand Down

0 comments on commit 3e1a8dc

Please sign in to comment.