Skip to content

Commit

Permalink
Push reports to the app in the background and in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wilkie committed Sep 7, 2015
1 parent a3eed58 commit 888a080
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
9 changes: 8 additions & 1 deletion probe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,15 @@ func main() {
log.Printf("warning: process reporting enabled, but that requires root to find everything")
}

publisherFactory := func(target string) (xfer.Publisher, error) { return xfer.NewHTTPPublisher(target, *token, probeID) }
publisherFactory := func(target string) (xfer.Publisher, error) {
publisher, err := xfer.NewHTTPPublisher(target, *token, probeID)
if err != nil {
return nil, err
}
return xfer.NewBackgroundPublisher(publisher), nil
}
publishers := xfer.NewMultiPublisher(publisherFactory)
defer publishers.Stop()
resolver := newStaticResolver(targets, publishers.Add)
defer resolver.Stop()

Expand Down
48 changes: 48 additions & 0 deletions xfer/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// Publisher is something which can send a report to a remote collector.
type Publisher interface {
Publish(report.Report) error
Stop()
}

// HTTPPublisher publishes reports by POST to a fixed endpoint.
Expand Down Expand Up @@ -83,12 +84,50 @@ func (p HTTPPublisher) Publish(rpt report.Report) error {
return nil
}

func (p HTTPPublisher) Stop() {}

// AuthorizationHeader returns a value suitable for an HTTP Authorization
// header, based on the passed token string.
func AuthorizationHeader(token string) string {
return fmt.Sprintf("Scope-Probe token=%s", token)
}

type BackgroundPublisher struct {
publisher Publisher
reports chan report.Report
}

func NewBackgroundPublisher(p Publisher) *BackgroundPublisher {
result := &BackgroundPublisher{
publisher: p,
reports: make(chan report.Report),
}
go result.loop()
return result
}

func (b *BackgroundPublisher) loop() {
for r := range b.reports {
if err := b.publisher.Publish(r); err != nil {
log.Printf("Error publishing: %v", err)
}
}
}

func (b *BackgroundPublisher) Publish(r report.Report) error {
select {
case b.reports <- r:
return nil
default:
return fmt.Errorf("Dropping report - can't push fast enough")
}
}

func (b *BackgroundPublisher) Stop() {
close(b.reports)
b.publisher.Stop()
}

// MultiPublisher implements Publisher over a set of publishers.
type MultiPublisher struct {
mtx sync.RWMutex
Expand Down Expand Up @@ -142,3 +181,12 @@ func (p *MultiPublisher) Publish(rpt report.Report) error {
}
return nil
}

func (p *MultiPublisher) Stop() {
p.mtx.RLock()
defer p.mtx.RUnlock()

for _, publisher := range p.m {
publisher.Stop()
}
}

0 comments on commit 888a080

Please sign in to comment.