Skip to content

Commit ebb2911

Browse files
committed
chg: [sshd] HTML compiler logic
1 parent 75a917b commit ebb2911

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

main.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/signal"
99
"strconv"
1010
"strings"
11+
"sync"
1112
"time"
1213

1314
"bufio"
@@ -33,16 +34,23 @@ type (
3334
httpHost string
3435
httpPort string
3536
}
37+
comutex struct {
38+
mu sync.Mutex
39+
compiling bool
40+
}
3641
)
3742

3843
// Setting up flags
3944
var (
40-
confdir = flag.String("c", "conf.sample", "configuration directory")
41-
all = flag.Bool("a", true, "run all parsers when set. Set by default")
42-
specific = flag.String("o", "", "run only a specific parser [sshd]")
43-
redisD4 redis.Conn
44-
redisParsers *redis.Pool
45-
parsers = [1]string{"sshd"}
45+
confdir = flag.String("c", "conf.sample", "configuration directory")
46+
all = flag.Bool("a", true, "run all parsers when set. Set by default")
47+
specific = flag.String("o", "", "run only a specific parser [sshd]")
48+
redisD4 redis.Conn
49+
redisParsers *redis.Pool
50+
parsers = [1]string{"sshd"}
51+
compilationTrigger = 10
52+
wg sync.WaitGroup
53+
compiling comutex
4654
)
4755

4856
func main() {
@@ -135,6 +143,9 @@ func main() {
135143
// Create a connection Pool
136144
redisParsers = newPool(rp.redisHost+":"+rp.redisPort, rp.redisDBCount)
137145

146+
// Line counter to trigger HTML compilation
147+
nblines := 0
148+
138149
var torun = []logparser.Parser{}
139150
// Init parser depending on the parser flags:
140151
if *all {
@@ -185,12 +196,32 @@ func main() {
185196
log.Fatal(err)
186197
}
187198
}
188-
199+
nblines++
200+
if nblines > compilationTrigger {
201+
nblines = 0
202+
// Non-blocking
203+
if !compiling.compiling {
204+
go compile()
205+
}
206+
}
189207
}
190208

209+
wg.Wait()
191210
log.Println("Exit")
192211
}
193212

213+
func compile() {
214+
compiling.mu.Lock()
215+
compiling.compiling = true
216+
wg.Add(1)
217+
log.Println("I should probably be writing")
218+
time.Sleep(500 * time.Millisecond)
219+
log.Println("Writing")
220+
compiling.compiling = false
221+
compiling.mu.Unlock()
222+
wg.Done()
223+
}
224+
194225
func newPool(addr string, maxconn int) *redis.Pool {
195226
return &redis.Pool{
196227
MaxActive: maxconn,

0 commit comments

Comments
 (0)