overflow_probes/probe/probe.go

127 lines
2.4 KiB
Go
Raw Normal View History

2017-09-18 09:21:58 +00:00
package probe
import (
2017-10-12 10:02:38 +00:00
"context"
2017-09-27 13:55:29 +00:00
"fmt"
"net/http"
2017-09-21 11:04:30 +00:00
2017-09-27 13:55:29 +00:00
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_probes/central/api/module"
"git.loafle.net/overflow/overflow_probes/central/client"
2017-09-21 11:04:30 +00:00
"git.loafle.net/overflow/overflow_probes/commons"
2017-09-27 13:55:29 +00:00
"git.loafle.net/overflow/overflow_probes/config"
opuu "git.loafle.net/overflow/overflow_probes/util/url"
2017-09-18 09:21:58 +00:00
)
2017-09-27 13:55:29 +00:00
const (
2017-10-12 10:02:38 +00:00
probeEntryPoint = "/probe"
fileEntryPoint = "/file"
metricEntryPoint = "/metric"
2017-09-27 13:55:29 +00:00
)
2017-09-18 09:21:58 +00:00
2017-10-12 10:02:38 +00:00
type Prober interface {
commons.Starter
commons.Shutdowner
}
2017-09-18 09:21:58 +00:00
type probe struct {
2017-09-29 12:30:27 +00:00
probeEntryURL string
fileEntryURL string
metricEntryURL string
2017-09-27 13:55:29 +00:00
2017-09-29 12:30:27 +00:00
probeClient client.Client
fileClient client.Client
metricClient client.Client
2017-09-27 13:55:29 +00:00
2017-10-12 10:02:38 +00:00
shutdown chan bool
2017-09-27 13:55:29 +00:00
}
2017-10-12 10:02:38 +00:00
func New() (Prober, error) {
p := &probe{
shutdown: make(chan bool),
}
2017-09-29 12:30:27 +00:00
2017-09-27 13:55:29 +00:00
var err error
2017-09-29 12:30:27 +00:00
if p.probeEntryURL, err = opuu.Join(config.CFG.Central.URL, probeEntryPoint); nil != err {
2017-09-27 13:55:29 +00:00
return nil, err
}
2017-09-29 12:30:27 +00:00
if p.fileEntryURL, err = opuu.Join(config.CFG.Central.URL, fileEntryPoint); nil != err {
2017-09-27 13:55:29 +00:00
return nil, err
}
2017-10-12 10:02:38 +00:00
if p.metricEntryURL, err = opuu.Join(config.CFG.Central.URL, metricEntryPoint); nil != err {
2017-09-29 12:30:27 +00:00
return nil, err
2017-09-27 13:55:29 +00:00
}
2017-09-29 12:30:27 +00:00
p.probeClient = client.New()
p.fileClient = client.New()
2017-10-12 10:02:38 +00:00
p.metricClient = client.New()
2017-09-27 13:55:29 +00:00
2017-10-12 10:02:38 +00:00
return p, nil
}
func (p *probe) Start() error {
return p.start()
}
func (p *probe) start() error {
if err := p.connectToCentral(); nil != err {
return err
2017-09-29 12:30:27 +00:00
}
2017-09-18 09:21:58 +00:00
2017-10-12 10:02:38 +00:00
p.listen()
return nil
}
func (p *probe) listen() {
go func() {
for {
select {
case <-p.shutdown:
break
}
}
}()
2017-09-27 13:55:29 +00:00
}
2017-10-12 10:02:38 +00:00
func (p *probe) connectToCentral() error {
2017-09-27 13:55:29 +00:00
var err error
2017-09-29 12:30:27 +00:00
var res *http.Response
2017-10-12 10:02:38 +00:00
if res, err = client.ConnectToCentralAsProbe(p.probeClient, p.probeEntryURL); nil != err {
2017-09-27 13:55:29 +00:00
return err
}
2017-09-29 12:30:27 +00:00
encryptionKey := res.Header.Get(module.ProbeHeader_Probe_EncryptionKey)
config.EncryptionKey = &encryptionKey
2017-09-28 06:08:43 +00:00
p.probeClient.OnNotify(p.onNotify)
2017-09-27 13:55:29 +00:00
2017-10-12 10:02:38 +00:00
if _, err = client.ConnectToCentralAsProbe(p.metricClient, p.metricEntryURL); nil != err {
2017-09-27 13:55:29 +00:00
return err
}
2017-09-18 09:21:58 +00:00
return nil
}
2017-09-29 12:30:27 +00:00
func (p *probe) sendNotifyToCentral(method string, params ...string) {
if err := p.probeClient.Notify(method, params); nil != err {
2017-10-12 10:02:38 +00:00
logging.Logger.Error(fmt.Sprintf("Probe notify: %v", err))
2017-09-29 12:30:27 +00:00
}
}
2017-10-12 10:02:38 +00:00
func (p *probe) Shutdown(ctx context.Context) error {
for {
p.stop(fmt.Errorf("Shutdown"))
select {
case <-ctx.Done():
return ctx.Err()
}
}
}
func (p *probe) stop(err error) {
defer close(p.shutdown)
}