@@ -248,41 +248,12 @@ func (e IdenticalFile) Error() string {
248248 return fmt .Sprintf ("File %s is identical to the previous version" , e .File )
249249}
250250
251- func (s * state ) updateFile (file string ) error {
251+ // Update the state based on the current slurm file and data.
252+ func (s * state ) updateFromNewState () error {
252253 sessid , _ := s .server .GetSessionId (nil )
253254
254- log .Debugf ("Refreshing cache from %s" , file )
255-
256- s .lastts = time .Now ().UTC ()
257- data , code , lastrefresh , err := s .fetchConfig .FetchFile (file )
258- if err != nil {
259- return err
260- }
261- if lastrefresh {
262- LastRefresh .WithLabelValues (file ).Set (float64 (s .lastts .UnixNano () / 1e9 ))
263- }
264- if code != - 1 {
265- RefreshStatusCode .WithLabelValues (file , fmt .Sprintf ("%d" , code )).Inc ()
266- }
267-
268- hsum , _ := checkFile (data )
269- if s .lasthash != nil {
270- cres := bytes .Compare (s .lasthash , hsum )
271- if cres == 0 {
272- return IdenticalFile {File : file }
273- }
274- }
275-
276- s .lastchange = time .Now ().UTC ()
277- s .lastdata = data
278-
279- vrplistjson , err := decodeJSON (s .lastdata )
280- if err != nil {
281- return err
282- }
283-
284255 if s .checktime {
285- buildtime , err := time .Parse (time .RFC3339 , vrplistjson .Metadata .Buildtime )
256+ buildtime , err := time .Parse (time .RFC3339 , s . lastdata .Metadata .Buildtime )
286257 if err != nil {
287258 return err
288259 }
@@ -292,7 +263,7 @@ func (s *state) updateFile(file string) error {
292263 }
293264 }
294265
295- vrpsjson := vrplistjson .Data
266+ vrpsjson := s . lastdata .Data
296267 if s .slurm != nil {
297268 kept , removed := s .slurm .FilterOnVRPs (vrpsjson )
298269 asserted := s .slurm .AssertVRPs ()
@@ -301,13 +272,9 @@ func (s *state) updateFile(file string) error {
301272 }
302273
303274 vrps , count , countv4 , countv6 := processData (vrpsjson )
304- if err != nil {
305- return err
306- }
307275
308- log .Infof ("New update (%v uniques, %v total prefixes). %v bytes. Updating sha256 hash %x -> %x" ,
309- len (vrps ), count , len (s .lastconverted ), s .lasthash , hsum )
310- s .lasthash = hsum
276+ log .Infof ("New update (%v uniques, %v total prefixes)." ,
277+ len (vrps ), count )
311278
312279 s .server .AddVRPs (vrps )
313280
@@ -322,7 +289,7 @@ func (s *state) updateFile(file string) error {
322289 s .exported = prefixfile.VRPList {
323290 Metadata : prefixfile.MetaData {
324291 Counts : len (vrpsjson ),
325- Buildtime : vrplistjson .Metadata .Buildtime ,
292+ Buildtime : s . lastdata .Metadata .Buildtime ,
326293 },
327294 Data : vrpsjson ,
328295 }
@@ -339,16 +306,54 @@ func (s *state) updateFile(file string) error {
339306 countv6_dup ++
340307 }
341308 }
342- s .metricsEvent .UpdateMetrics (countv4 , countv6 , countv4_dup , countv6_dup , s .lastchange , s .lastts , file )
309+ s .metricsEvent .UpdateMetrics (countv4 , countv6 , countv4_dup , countv6_dup , s .lastchange , s .lastts , * CacheBin )
343310 }
311+
344312 return nil
345313}
346314
347- func (s * state ) updateSlurm (file string ) error {
315+ func (s * state ) updateFile (file string ) (bool , error ) {
316+ log .Debugf ("Refreshing cache from %s" , file )
317+
318+ s .lastts = time .Now ().UTC ()
319+ data , code , lastrefresh , err := s .fetchConfig .FetchFile (file )
320+ if err != nil {
321+ return false , err
322+ }
323+ if lastrefresh {
324+ LastRefresh .WithLabelValues (file ).Set (float64 (s .lastts .UnixNano () / 1e9 ))
325+ }
326+ if code != - 1 {
327+ RefreshStatusCode .WithLabelValues (file , fmt .Sprintf ("%d" , code )).Inc ()
328+ }
329+
330+ hsum , _ := checkFile (data )
331+ if s .lasthash != nil {
332+ cres := bytes .Compare (s .lasthash , hsum )
333+ if cres == 0 {
334+ return false , IdenticalFile {File : file }
335+ }
336+ }
337+
338+ log .Infof ("new cache file: Updating sha256 hash %x -> %x" , s .lasthash , hsum )
339+
340+ vrplistjson , err := decodeJSON (data )
341+ if err != nil {
342+ return false , err
343+ }
344+
345+ s .lasthash = hsum
346+ s .lastchange = time .Now ().UTC ()
347+ s .lastdata = vrplistjson
348+
349+ return true , nil
350+ }
351+
352+ func (s * state ) updateSlurm (file string ) (bool , error ) {
348353 log .Debugf ("Refreshing slurm from %v" , file )
349354 data , code , lastrefresh , err := s .fetchConfig .FetchFile (file )
350355 if err != nil {
351- return err
356+ return false , err
352357 }
353358 if lastrefresh {
354359 LastRefresh .WithLabelValues (file ).Set (float64 (s .lastts .UnixNano () / 1e9 ))
@@ -361,10 +366,10 @@ func (s *state) updateSlurm(file string) error {
361366
362367 slurm , err := prefixfile .DecodeJSONSlurm (buf )
363368 if err != nil {
364- return err
369+ return false , err
365370 }
366371 s .slurm = slurm
367- return nil
372+ return true , nil
368373}
369374
370375func (s * state ) routineUpdate (file string , interval int , slurmFile string ) {
@@ -379,8 +384,10 @@ func (s *state) routineUpdate(file string, interval int, slurmFile string) {
379384 log .Debug ("Received HUP signal" )
380385 }
381386 delay .Stop ()
387+ slurmNotPresentOrUpdated := false
382388 if slurmFile != "" {
383- err := s .updateSlurm (slurmFile )
389+ var err error
390+ slurmNotPresentOrUpdated , err = s .updateSlurm (slurmFile )
384391 if err != nil {
385392 switch err .(type ) {
386393 case utils.HttpNotModified :
@@ -392,7 +399,7 @@ func (s *state) routineUpdate(file string, interval int, slurmFile string) {
392399 }
393400 }
394401 }
395- err := s .updateFile (file )
402+ cacheUpdated , err := s .updateFile (file )
396403 if err != nil {
397404 switch err .(type ) {
398405 case utils.HttpNotModified :
@@ -405,6 +412,15 @@ func (s *state) routineUpdate(file string, interval int, slurmFile string) {
405412 log .Errorf ("Error updating: %v" , err )
406413 }
407414 }
415+
416+ // Only process the first time after there is either a cache or SLURM
417+ // update.
418+ if cacheUpdated || slurmNotPresentOrUpdated {
419+ err := s .updateFromNewState ()
420+ if err != nil {
421+ log .Errorf ("Error updating from new state: %v" , err )
422+ }
423+ }
408424 }
409425}
410426
@@ -417,13 +433,12 @@ func (s *state) exporter(wr http.ResponseWriter, r *http.Request) {
417433}
418434
419435type state struct {
420- lastdata []byte
421- lastconverted []byte
422- lasthash []byte
423- lastchange time.Time
424- lastts time.Time
425- sendNotifs bool
426- useSerial int
436+ lastdata * prefixfile.VRPList
437+ lasthash []byte
438+ lastchange time.Time
439+ lastts time.Time
440+ sendNotifs bool
441+ useSerial int
427442
428443 fetchConfig * utils.FetchConfig
429444
@@ -532,7 +547,7 @@ func main() {
532547 log .Fatalf ("Specify at least a bind address" )
533548 }
534549
535- err := s .updateFile (* CacheBin )
550+ _ , err := s .updateFile (* CacheBin )
536551 if err != nil {
537552 switch err .(type ) {
538553 case utils.HttpNotModified :
@@ -548,7 +563,7 @@ func main() {
548563
549564 slurmFile := * Slurm
550565 if slurmFile != "" {
551- err := s .updateSlurm (slurmFile )
566+ _ , err := s .updateSlurm (slurmFile )
552567 if err != nil {
553568 switch err .(type ) {
554569 case utils.HttpNotModified :
@@ -564,6 +579,9 @@ func main() {
564579 }
565580 }
566581
582+ // Initial calculation of state (after fetching cache + slurm)
583+ s .updateFromNewState ()
584+
567585 if * Bind != "" {
568586 go func () {
569587 sessid , _ := server .GetSessionId (nil )
0 commit comments