@@ -13,14 +13,16 @@ const c_N_HANDLERS = 3
1313const c_NATS_DELIM = common .NATS_DELIM
1414
1515type 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
2223type 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 {
3941type 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
4749type libtapir interface {
48- GenerateObservationMsg (string , uint32 ) (string , error ) // TODO set ttl?
50+ GenerateObservationMsg (string , uint32 , int ) (string , error ) // TODO set ttl?
4951}
5052
5153func 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
0 commit comments