github.com/prebid/prebid-server/v2@v2.18.0/analytics/pubstack/configupdate.go (about)

     1  package pubstack
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"time"
     8  
     9  	"github.com/prebid/prebid-server/v2/util/task"
    10  )
    11  
    12  // ConfigUpdateTask publishes configurations until the stop channel is signaled.
    13  type ConfigUpdateTask interface {
    14  	Start(stop <-chan struct{}) <-chan *Configuration
    15  }
    16  
    17  // ConfigUpdateHttpTask polls an HTTP endpoint on a specified interval and publishes configurations until
    18  // the stop channel is signaled.
    19  type ConfigUpdateHttpTask struct {
    20  	task       *task.TickerTask
    21  	configChan chan *Configuration
    22  }
    23  
    24  func NewConfigUpdateHttpTask(httpClient *http.Client, scope, endpoint, refreshInterval string) (*ConfigUpdateHttpTask, error) {
    25  	refreshDuration, err := time.ParseDuration(refreshInterval)
    26  	if err != nil {
    27  		return nil, fmt.Errorf("fail to parse the module args, arg=analytics.pubstack.configuration_refresh_delay: %v", err)
    28  	}
    29  
    30  	endpointUrl, err := url.Parse(endpoint + "/bootstrap?scopeId=" + scope)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	configChan := make(chan *Configuration)
    36  
    37  	tr := task.NewTickerTaskFromFunc(refreshDuration, func() error {
    38  		config, err := fetchConfig(httpClient, endpointUrl)
    39  		if err != nil {
    40  			return fmt.Errorf("[pubstack] Fail to fetch remote configuration: %v", err)
    41  		}
    42  		configChan <- config
    43  		return nil
    44  	})
    45  
    46  	return &ConfigUpdateHttpTask{
    47  		task:       tr,
    48  		configChan: configChan,
    49  	}, nil
    50  }
    51  
    52  func (t *ConfigUpdateHttpTask) Start(stop <-chan struct{}) <-chan *Configuration {
    53  	go t.task.Start()
    54  
    55  	go func() {
    56  		<-stop
    57  		t.task.Stop()
    58  	}()
    59  
    60  	return t.configChan
    61  }