Skip to content

Commit eadcee5

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 eadcee5

File tree

7 files changed

+171
-113
lines changed

7 files changed

+171
-113
lines changed

Makefile

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
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:=$$(cat ./VERSION)
2+
COMMIT:=$$(git describe --dirty=+WiP --always 2> /dev/null || echo "no-vcs")
3+
OUT:=$$(pwd)/out
144

5+
.PHONY:
156

167
all: build
178

@@ -21,12 +12,6 @@ build: outdir
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: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,29 @@ observation-encoder -config /path/to/config/file
1919
# Sample config file
2020

2121
```toml
22+
debug = true
23+
2224
[cert]
23-
interval = 100
25+
interval = 86400
2426
cert_dir = "/path/to/certs/dir"
2527

2628
[api]
27-
active = true
29+
active = false
2830
address = "127.0.0.1"
2931
port = "10001"
3032

3133
[nats]
3234
url = "nats://127.0.0.1:4222"
33-
subject_prefix = "observations"
34-
subject_southbound = "test.subject"
35-
ttl = 3600
35+
subject_southbound = "test.observations.down.edge"
36+
observation_subject_prefix = "test.observations"
37+
38+
[[nats.buckets]]
39+
name = "foo"
40+
ttl = 60
41+
42+
[[nats.buckets]]
43+
name = "bar"
44+
ttl = 120
3645

3746
[libtapir]
3847
debug = true

VERSION

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

internal/app/app.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ const c_N_HANDLERS = 3
1313
const c_NATS_DELIM = common.NATS_DELIM
1414

1515
type Conf struct {
16-
Log common.Logger
1716
Debug bool `toml:"debug"`
17+
TtlMargin int `toml:"ttl_margin"`
18+
Log common.Logger
1819
NatsHandle nats
1920
LibtapirHandle libtapir
2021
}
2122

2223
type appHandle struct {
2324
id string
25+
ttlMargin int
2426
log common.Logger
2527
natsHandle nats
2628
libtapirHandle libtapir
@@ -39,13 +41,13 @@ type job struct {
3941
type nats interface {
4042
WatchObservations(context.Context) (<-chan common.NatsMsg, error)
4143
RemovePrefix(string) string
42-
GetObservations(context.Context, string) (uint32, error)
44+
GetObservations(context.Context, string) (uint32, int, error)
4345
SendSouthboundObservation(string) error
4446
Shutdown() error
4547
}
4648

4749
type libtapir interface {
48-
GenerateObservationMsg(string, uint32) (string, error) // TODO set ttl?
50+
GenerateObservationMsg(string, uint32, int) (string, error) // TODO set ttl?
4951
}
5052

5153
func Create(conf Conf) (*appHandle, error) {
@@ -63,8 +65,14 @@ func Create(conf Conf) (*appHandle, error) {
6365
return nil, common.ErrBadHandle
6466
}
6567

68+
// TODO what is a reasonale safety margin (in seconds) for adding to outgoing TTL?
69+
if conf.TtlMargin < 0 || conf.TtlMargin > 3600 {
70+
return nil, common.ErrBadParam
71+
}
72+
6673
a.log = conf.Log
6774
a.id = "main app"
75+
a.ttlMargin = conf.TtlMargin
6876
a.natsHandle = conf.NatsHandle
6977
a.libtapirHandle = conf.LibtapirHandle
7078

@@ -143,15 +151,20 @@ func (a *appHandle) handleJob(ctx context.Context, j job) {
143151

144152
a.log.Debug("Extracted domain '%s'", domain)
145153

146-
obs, err := a.natsHandle.GetObservations(ctx, domain)
154+
obs, ttl, err := a.natsHandle.GetObservations(ctx, domain)
147155
if err != nil {
148156
a.log.Error("Could not get observations for %s: %s", domain, err)
149157
return
150158
}
151159

160+
if obs == 0 || ttl == 0 {
161+
a.log.Debug("Observation for %s has value %d and ttl %d, won't bother sending", domain, obs, ttl)
162+
return
163+
}
164+
152165
a.log.Debug("%s has observation vector %d", domain, obs)
153166

154-
obsJSON, err := a.libtapirHandle.GenerateObservationMsg(domain, obs)
167+
obsJSON, err := a.libtapirHandle.GenerateObservationMsg(domain, obs, int(ttl)+a.ttlMargin)
155168
if err != nil {
156169
a.log.Error("Couldn't generate JSON observation: %s", err)
157170
return

internal/libtapir/libtapir.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ func Create(conf Conf) (*libtapir, error) {
2929
return lt, nil
3030
}
3131

32-
func (lt *libtapir) GenerateObservationMsg(domainStr string, flags uint32) (string, error) {
32+
func (lt *libtapir) GenerateObservationMsg(domainStr string, flags uint32, ttl int) (string, error) {
3333
domain := tapir.Domain{
3434
Name: domainStr,
3535
TimeAdded: time.Now(),
36-
TTL: 3600,
36+
TTL: ttl,
3737
TagMask: tapir.TagMask(flags),
3838
ExtendedTags: []string{},
3939
}
4040

4141
tapirMsg := tapir.TapirMsg{
4242
SrcName: "dns-tapir",
43-
Creator: "tapir-analyse-new-qname",
43+
Creator: "observation-encoder",
4444
MsgType: "observation",
4545
ListType: "doubtlist",
4646
Added: []tapir.Domain{domain},

0 commit comments

Comments
 (0)