~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/introspection/socket.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package introspection
 
5
 
 
6
import (
 
7
        "net"
 
8
        "net/http"
 
9
        "runtime"
 
10
 
 
11
        "github.com/juju/errors"
 
12
        "launchpad.net/tomb"
 
13
 
 
14
        "github.com/juju/juju/worker"
 
15
        "github.com/juju/juju/worker/introspection/pprof"
 
16
)
 
17
 
 
18
// Config describes the arguments required to create the introspection worker.
 
19
type Config struct {
 
20
        SocketName string
 
21
}
 
22
 
 
23
// Validate checks the config values to assert they are valid to create the worker.
 
24
func (c *Config) Validate() error {
 
25
        if c.SocketName == "" {
 
26
                return errors.NotValidf("empty SocketName")
 
27
        }
 
28
        return nil
 
29
}
 
30
 
 
31
// socketListener is a worker and constructed with NewWorker.
 
32
type socketListener struct {
 
33
        tomb     tomb.Tomb
 
34
        listener *net.UnixListener
 
35
}
 
36
 
 
37
// NewWorker starts an http server listening on an abstract domain socket
 
38
// which will be created with the specified name.
 
39
func NewWorker(config Config) (worker.Worker, error) {
 
40
        if err := config.Validate(); err != nil {
 
41
                return nil, errors.Trace(err)
 
42
        }
 
43
        if runtime.GOOS != "linux" {
 
44
                return nil, errors.NotSupportedf("os %q", runtime.GOOS)
 
45
        }
 
46
 
 
47
        path := "@" + config.SocketName
 
48
        addr, err := net.ResolveUnixAddr("unix", path)
 
49
        if err != nil {
 
50
                return nil, errors.Annotate(err, "unable to resolve unix socket")
 
51
        }
 
52
 
 
53
        l, err := net.ListenUnix("unix", addr)
 
54
        if err != nil {
 
55
                return nil, errors.Annotate(err, "unable to listen on unix socket")
 
56
        }
 
57
        logger.Debugf("introspection worker listening on %q", path)
 
58
 
 
59
        w := &socketListener{
 
60
                listener: l,
 
61
        }
 
62
        go w.serve()
 
63
        go w.run()
 
64
        return w, nil
 
65
}
 
66
 
 
67
func (w *socketListener) serve() {
 
68
        mux := http.NewServeMux()
 
69
        mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
 
70
        mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
 
71
        mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
 
72
        mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
 
73
 
 
74
        srv := http.Server{
 
75
                Handler: mux,
 
76
        }
 
77
 
 
78
        logger.Debugf("stats worker now servering")
 
79
        srv.Serve(w.listener)
 
80
        logger.Debugf("stats worker servering finished")
 
81
}
 
82
 
 
83
func (w *socketListener) run() {
 
84
        <-w.tomb.Dying()
 
85
        logger.Debugf("stats worker closing listener")
 
86
        w.listener.Close()
 
87
        w.tomb.Done()
 
88
}
 
89
 
 
90
// Kill implements worker.Worker.
 
91
func (w *socketListener) Kill() {
 
92
        w.tomb.Kill(nil)
 
93
}
 
94
 
 
95
// Wait implements worker.Worker.
 
96
func (w *socketListener) Wait() error {
 
97
        return w.tomb.Wait()
 
98
}