github.com/safing/portbase@v0.19.5/rng/fullfeed.go (about)

     1  package rng
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  func getFullFeedDuration() time.Duration {
     9  	// full feed every 5x time of reseedAfterSeconds
    10  	secsUntilFullFeed := reseedAfterSeconds * 5
    11  
    12  	// full feed at most once every ten minutes
    13  	if secsUntilFullFeed < 600 {
    14  		secsUntilFullFeed = 600
    15  	}
    16  
    17  	return time.Duration(secsUntilFullFeed) * time.Second
    18  }
    19  
    20  func fullFeeder(ctx context.Context) error {
    21  	fullFeedDuration := getFullFeedDuration()
    22  
    23  	for {
    24  		select {
    25  		case <-time.After(fullFeedDuration):
    26  
    27  			rngLock.Lock()
    28  		feedAll:
    29  			for {
    30  				select {
    31  				case data := <-rngFeeder:
    32  					rng.Reseed(data)
    33  				default:
    34  					break feedAll
    35  				}
    36  			}
    37  			rngLock.Unlock()
    38  
    39  		case <-ctx.Done():
    40  			return nil
    41  		}
    42  	}
    43  }