Skip to content

Commit 49859ee

Browse files
committed
Southbound observation has TTL set according to which observation is about to expire
Possible to add safety margin to outgoing TTL
1 parent cb491f7 commit 49859ee

File tree

10 files changed

+292
-191
lines changed

10 files changed

+292
-191
lines changed

Makefile

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
1-
NAME:=observation-encoder
2-
3-
#######################################
4-
# VERSION SOURCE OF TRUTH FOR PROJECT #
5-
#######################################
6-
VERSION:=0.0.0
7-
8-
OUT:=./out
9-
DEFAULT_INSTALLDIR:=/usr/bin
10-
INSTALL:=install -p -m 0755
11-
COMMIT:=$$(cat COMMIT 2> /dev/null || git describe --dirty=+WiP --always 2> /dev/null)
12-
13-
.PHONY: build install clean fmt vet coverage
1+
VERSION:=$(shell cat ./VERSION)
2+
COMMIT:=$(shell git describe --dirty=+WiP --always 2> /dev/null || echo "no-vcs")
3+
OUT:=$(CURDIR)/out
144

5+
.PHONY: all build outdir test coverage fmt vet clean ko
156

167
all: build
178

189
build: outdir
19-
go build -v -ldflags "-X 'main.version=$(VERSION)' -X 'main.commit=$(COMMIT)'" -o $(OUT)/ ./cmd/...
10+
go build -v -ldflags "-X main.commit=$(COMMIT)" -o $(OUT)/ ./cmd/...
2011

2112
outdir:
2213
-mkdir -p $(OUT)
2314

24-
clean:
25-
-rm -rf $(OUT)
26-
27-
install:
28-
test -z "$(DESTDIR)" && $(INSTALL) $(OUT)/$(PROG) $(DEFAULT_INSTALLDIR) || $(INSTALL) $(OUT)/$(PROG) $(DESTDIR)$(prefix)/bin/
29-
3015
test:
3116
go test ./...
3217

@@ -40,10 +25,8 @@ fmt:
4025
vet:
4126
go vet ./...
4227

43-
tarball: outdir
44-
@test ! -f COMMIT || (echo "Trying to make tarball from extracted tarball?" && false)
45-
@test ! -z $(COMMIT) || (echo "Not tracked by git?" && false)
46-
@test -z $$(git status --porcelain) || (echo "won't make tarball from dirty history" && false)
47-
echo "$(COMMIT)" > $(OUT)/COMMIT
48-
git archive --format=tar.gz --prefix=$(NAME)/ -o $(OUT)/$(NAME)-$(VERSION).tar.gz --add-file $(OUT)/COMMIT HEAD
49-
cd $(OUT) && sha256sum -b $(NAME)-$(VERSION).tar.gz > $(NAME)-$(VERSION).sha256.txt
28+
clean:
29+
-rm -rf $(OUT)
30+
31+
ko:
32+
ko build -L -B ./cmd/observation-encoder

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,33 @@ sudo make install
1313
# Run
1414

1515
```
16-
observation-encoder -config /path/to/config/file
16+
observation-encoder
1717
```
1818

1919
# Sample config file
2020

2121
```toml
22-
[cert]
23-
interval = 100
24-
cert_dir = "/path/to/certs/dir"
25-
26-
[api]
27-
active = true
28-
address = "127.0.0.1"
29-
port = "10001"
22+
debug = true
23+
ttl_margin = 5 # five seconds added to outgoing observations TTL
3024

3125
[nats]
3226
url = "nats://127.0.0.1:4222"
33-
subject_prefix = "observations"
34-
subject_southbound = "test.subject"
35-
ttl = 3600
27+
subject_southbound = "leontest.down.tapir-pop"
28+
observation_subject_prefix = "leontest.observations"
29+
30+
[[nats.buckets]]
31+
name = "globally_new"
32+
ttl = 30
33+
34+
[[nats.buckets]]
35+
name = "looptest"
36+
ttl = 10
37+
38+
[cert]
39+
active = false
40+
41+
[api]
42+
active = false
3643

3744
[libtapir]
3845
debug = true

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.0

cmd/observation-encoder/main.go

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
"github.com/dnstapir/observation-encoder/internal/nats"
2222
)
2323

24-
const c_ENVVAR_OVERRIDE_NATS_URL = "DNSTAPIR_NATS_URL"
24+
const env_DNSTAPIR_NATS_URL = "DNSTAPIR_NATS_URL"
2525

26+
/* Rewritten if building with make */
2627
var commit = "BAD-BUILD"
2728

2829
type conf struct {
@@ -84,13 +85,16 @@ func main() {
8485

8586
confDecoder := toml.NewDecoder(file)
8687
if confDecoder == nil {
87-
log.Error("Problem decoding config file '%s', exiting...", configFile)
88+
log.Error("Problem creating decoder for config file '%s', exiting...", configFile)
8889
os.Exit(-1)
8990
}
9091

9192
confDecoder.DisallowUnknownFields()
92-
confDecoder.Decode(&mainConf)
93-
file.Close() // TODO okay to close here while also using defer above?
93+
err = confDecoder.Decode(&mainConf)
94+
if err != nil {
95+
log.Error("Problem decoding config file '%s': %s", configFile, err)
96+
os.Exit(-1)
97+
}
9498

9599
debugFlag = debugFlag || mainConf.Debug
96100

@@ -105,12 +109,13 @@ func main() {
105109
})
106110
if err != nil {
107111
log.Error("Error creating nats log: %s", err)
112+
os.Exit(-1)
108113
}
109114

110-
envNatsUrl, overrideNatsUrl := os.LookupEnv(c_ENVVAR_OVERRIDE_NATS_URL)
115+
envNatsUrl, overrideNatsUrl := os.LookupEnv(env_DNSTAPIR_NATS_URL)
111116
if overrideNatsUrl {
112117
mainConf.Nats.Url = envNatsUrl
113-
log.Info("Overriding NATS url with environment variable '%s'", c_ENVVAR_OVERRIDE_NATS_URL)
118+
log.Info("Overriding NATS url with environment variable '%s'", env_DNSTAPIR_NATS_URL)
114119
}
115120

116121
mainConf.Nats.Log = natslog
@@ -131,6 +136,7 @@ func main() {
131136
})
132137
if err != nil {
133138
log.Error("Error creating libtapir log: %s", err)
139+
os.Exit(-1)
134140
}
135141

136142
mainConf.Libtapir.Log = libtapirlog
@@ -151,6 +157,7 @@ func main() {
151157
})
152158
if err != nil {
153159
log.Error("Error creating app log: %s", err)
160+
os.Exit(-1)
154161
}
155162

156163
mainConf.Log = applog
@@ -173,6 +180,7 @@ func main() {
173180
})
174181
if err != nil {
175182
log.Error("Error creating cert log: %s", err)
183+
os.Exit(-1)
176184
}
177185

178186
mainConf.Cert.Log = certlog
@@ -193,6 +201,7 @@ func main() {
193201
})
194202
if err != nil {
195203
log.Error("Error creating API log: %s", err)
204+
os.Exit(-1)
196205
}
197206
mainConf.Api.Log = apilog
198207
mainConf.Api.App = appHandle
@@ -214,20 +223,18 @@ func main() {
214223
defer signal.Stop(sigChan)
215224

216225
ctx, cancel := context.WithCancel(context.Background())
217-
exitCh := make(chan common.Exit)
226+
exitCh := make(chan common.Exit, 10) // TODO adjustable buffer?
218227

219228
log.Info("Starting threads...")
220229

221-
go appHandle.Run(ctx, exitCh)
222-
223-
go apiHandle.Run(ctx, exitCh)
224-
go certHandle.Run(ctx, exitCh)
230+
var wg sync.WaitGroup
231+
wg.Go(func() { appHandle.Run(ctx, exitCh) })
232+
wg.Go(func() { apiHandle.Run(ctx, exitCh) })
233+
wg.Go(func() { certHandle.Run(ctx, exitCh) })
225234

226235
log.Info("Threads started!")
227236

228237
exitLoop := false
229-
var wg sync.WaitGroup
230-
wg.Add(1)
231238
for {
232239
select {
233240
case s, ok := <-sigChan:
@@ -255,45 +262,29 @@ func main() {
255262
}
256263
if exitLoop || (sigChan == nil && exitCh == nil) {
257264
log.Info("Leaving toplevel loop")
258-
wg.Done()
259265
break
260266
}
261267
}
262268

263-
log.Info("Cancelling, giving threads some time to finish...")
269+
log.Info("Cancelling threads")
264270
cancel()
265-
timeout := make(chan bool, 1)
271+
272+
log.Info("Waiting for threads to finish")
273+
274+
done := make(chan struct{})
266275
go func() {
267-
time.Sleep(2 * time.Second)
268-
timeout <- true
276+
wg.Wait()
277+
close(done)
269278
}()
270279

271-
TIMEOUT_LOOP:
272-
for {
273-
select {
274-
case exit, ok := <-exitCh:
275-
if ok {
276-
if exit.Err != nil {
277-
log.Error("%s exited with error: '%s'", exit.ID, exit.Err)
278-
} else {
279-
log.Info("%s done!", exit.ID)
280-
}
281-
} else {
282-
log.Info("exit channel was closed during shutdown")
283-
exitCh = nil
284-
}
285-
case <-timeout:
286-
log.Debug("Time's up. Proceeding with shutdown.")
287-
break TIMEOUT_LOOP
288-
}
289-
if exitCh == nil {
290-
log.Warning("exit channel closed unexpectedly")
291-
break TIMEOUT_LOOP
292-
}
280+
const shutdownTimeout = 30 * time.Second
281+
select {
282+
case <-done:
283+
log.Info("Threads finished")
284+
case <-time.After(shutdownTimeout):
285+
log.Error("Timed out waiting for threads to finish after %s", shutdownTimeout)
293286
}
294287

295-
close(exitCh)
296-
wg.Wait()
297288
log.Info("Exiting...")
298289
os.Exit(0)
299290
}

internal/api/api.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ type certHandle interface {
4343

4444
func Create(conf Conf) (*apiHandle, error) {
4545
a := new(apiHandle)
46+
a.id = "api"
47+
a.active = conf.Active
48+
49+
if !a.active {
50+
return a, nil
51+
}
4652

4753
if conf.Log == nil {
4854
return nil, common.ErrBadHandle
@@ -69,7 +75,6 @@ func Create(conf Conf) (*apiHandle, error) {
6975
a.certs = conf.Certs
7076
a.listenInterface = net.JoinHostPort(conf.Address, conf.Port)
7177
a.active = conf.Active
72-
a.id = "api"
7378

7479
return a, nil
7580
}
@@ -82,7 +87,10 @@ func (a *apiHandle) Run(ctx context.Context, exitCh chan<- common.Exit) {
8287

8388
http.HandleFunc("/api/nats_in", a.apiHandleNatsIn)
8489

85-
cfg := &tls.Config{GetCertificate: a.certs.GetCertificate}
90+
cfg := &tls.Config{
91+
GetCertificate: a.certs.GetCertificate,
92+
MinVersion: tls.VersionTLS12,
93+
}
8694
srv := &http.Server{
8795
TLSConfig: cfg,
8896
Addr: a.listenInterface,

0 commit comments

Comments
 (0)