Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

106 lines (96 sloc) 2.125 kb
package main
import (
"flag"
"github.com/temoto/oakmole/oakmole"
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"sync"
"syscall"
"time"
)
func main() {
bind := flag.String("bind", ":80", "Listen on address:port")
outPath := flag.String("out", "-", "Path to dump file. '-' for stdout")
cpuprofile := flag.String("cpuprofile", "", "Write CPU profile to file")
memprofile := flag.String("memprofile", "", "Write memory profile to file")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
defer f.Close()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
go func() {
for {
time.Sleep(5 * time.Second)
pprof.WriteHeapProfile(f)
}
}()
defer pprof.WriteHeapProfile(f)
defer f.Close()
}
// Set number of parallel threads to number of CPUs.
runtime.GOMAXPROCS(runtime.NumCPU())
// Tweak record channel buffer size.
rch := make(chan *oakmole.Record, 100)
stop := make(chan bool)
workers := new(sync.WaitGroup)
sigIntChan := make(chan os.Signal, 1)
signal.Notify(sigIntChan, syscall.SIGINT)
go func() {
<-sigIntChan
stop <- true
}()
// TODO: reload once in a while, also on SIGHUP
var outFile *os.File
var err error
if *outPath != "-" {
outFile, err = os.OpenFile(*outPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Println("Writer: Open:", err)
os.Exit(1)
}
} else {
outFile = os.Stdout
}
// Buffer
workers.Add(1)
go func() {
defer workers.Done()
for record := range rch {
b, err := record.Marshal()
if err != nil {
log.Println("Writer: Marshal:", err)
continue
}
_, err = outFile.Write(b)
if err != nil {
log.Println("Writer: Write:", err)
}
}
}()
listen := startListener(bind, rch)
defer listen.Close()
<-stop
close(rch)
workers.Wait()
if outFile != os.Stdout {
if err = outFile.Sync(); err != nil {
log.Println("Writer: Sync:", err)
os.Exit(1)
}
}
}
Jump to Line
Something went wrong with that request. Please try again.