Skip to content

Commit 9573d94

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 9573d94

File tree

10 files changed

+271
-195
lines changed

10 files changed

+271
-195
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: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os/signal"
99
"sync"
1010
"syscall"
11-
"time"
1211

1312
"github.com/pelletier/go-toml/v2"
1413

@@ -21,8 +20,9 @@ import (
2120
"github.com/dnstapir/observation-encoder/internal/nats"
2221
)
2322

24-
const c_ENVVAR_OVERRIDE_NATS_URL = "DNSTAPIR_NATS_URL"
23+
const env_DNSTAPIR_NATS_URL = "DNSTAPIR_NATS_URL"
2524

25+
/* Rewritten if building with make */
2626
var commit = "BAD-BUILD"
2727

2828
type conf struct {
@@ -84,13 +84,16 @@ func main() {
8484

8585
confDecoder := toml.NewDecoder(file)
8686
if confDecoder == nil {
87-
log.Error("Problem decoding config file '%s', exiting...", configFile)
87+
log.Error("Problem creating decoder for config file '%s', exiting...", configFile)
8888
os.Exit(-1)
8989
}
9090

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

9598
debugFlag = debugFlag || mainConf.Debug
9699

@@ -105,12 +108,13 @@ func main() {
105108
})
106109
if err != nil {
107110
log.Error("Error creating nats log: %s", err)
111+
os.Exit(-1)
108112
}
109113

110-
envNatsUrl, overrideNatsUrl := os.LookupEnv(c_ENVVAR_OVERRIDE_NATS_URL)
114+
envNatsUrl, overrideNatsUrl := os.LookupEnv(env_DNSTAPIR_NATS_URL)
111115
if overrideNatsUrl {
112116
mainConf.Nats.Url = envNatsUrl
113-
log.Info("Overriding NATS url with environment variable '%s'", c_ENVVAR_OVERRIDE_NATS_URL)
117+
log.Info("Overriding NATS url with environment variable '%s'", env_DNSTAPIR_NATS_URL)
114118
}
115119

116120
mainConf.Nats.Log = natslog
@@ -131,6 +135,7 @@ func main() {
131135
})
132136
if err != nil {
133137
log.Error("Error creating libtapir log: %s", err)
138+
os.Exit(-1)
134139
}
135140

136141
mainConf.Libtapir.Log = libtapirlog
@@ -151,6 +156,7 @@ func main() {
151156
})
152157
if err != nil {
153158
log.Error("Error creating app log: %s", err)
159+
os.Exit(-1)
154160
}
155161

156162
mainConf.Log = applog
@@ -173,6 +179,7 @@ func main() {
173179
})
174180
if err != nil {
175181
log.Error("Error creating cert log: %s", err)
182+
os.Exit(-1)
176183
}
177184

178185
mainConf.Cert.Log = certlog
@@ -193,6 +200,7 @@ func main() {
193200
})
194201
if err != nil {
195202
log.Error("Error creating API log: %s", err)
203+
os.Exit(-1)
196204
}
197205
mainConf.Api.Log = apilog
198206
mainConf.Api.App = appHandle
@@ -214,20 +222,18 @@ func main() {
214222
defer signal.Stop(sigChan)
215223

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

219227
log.Info("Starting threads...")
220228

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

226234
log.Info("Threads started!")
227235

228236
exitLoop := false
229-
var wg sync.WaitGroup
230-
wg.Add(1)
231237
for {
232238
select {
233239
case s, ok := <-sigChan:
@@ -255,44 +261,14 @@ func main() {
255261
}
256262
if exitLoop || (sigChan == nil && exitCh == nil) {
257263
log.Info("Leaving toplevel loop")
258-
wg.Done()
259264
break
260265
}
261266
}
262267

263-
log.Info("Cancelling, giving threads some time to finish...")
268+
log.Info("Cancelling threads")
264269
cancel()
265-
timeout := make(chan bool, 1)
266-
go func() {
267-
time.Sleep(2 * time.Second)
268-
timeout <- true
269-
}()
270-
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-
}
293-
}
294270

295-
close(exitCh)
271+
log.Info("Waiting for threads to finish")
296272
wg.Wait()
297273
log.Info("Exiting...")
298274
os.Exit(0)

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)