github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/events/events.go (about)

     1  package events
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	"github.com/prebid/prebid-server/v2/stored_requests"
     8  )
     9  
    10  // Save represents a bulk save
    11  type Save struct {
    12  	Requests  map[string]json.RawMessage `json:"requests"`
    13  	Imps      map[string]json.RawMessage `json:"imps"`
    14  	Accounts  map[string]json.RawMessage `json:"accounts"`
    15  	Responses map[string]json.RawMessage `json:"responses"`
    16  }
    17  
    18  // Invalidation represents a bulk invalidation
    19  type Invalidation struct {
    20  	Requests  []string `json:"requests"`
    21  	Imps      []string `json:"imps"`
    22  	Accounts  []string `json:"accounts"`
    23  	Responses []string `json:"responses"`
    24  }
    25  
    26  // EventProducer will produce cache update and invalidation events on its channels
    27  type EventProducer interface {
    28  	Saves() <-chan Save
    29  	Invalidations() <-chan Invalidation
    30  }
    31  
    32  // EventListener provides information about how many events a listener has processed
    33  // and a mechanism to stop the listener goroutine
    34  type EventListener struct {
    35  	stop         chan struct{}
    36  	onSave       func()
    37  	onInvalidate func()
    38  }
    39  
    40  // SimpleEventListener creates a new EventListener that solely propagates cache updates and invalidations
    41  func SimpleEventListener() *EventListener {
    42  	return &EventListener{
    43  		stop:         make(chan struct{}),
    44  		onSave:       nil,
    45  		onInvalidate: nil,
    46  	}
    47  }
    48  
    49  // NewEventListener creates a new EventListener that may perform additional work after propagating cache saves and invalidations
    50  func NewEventListener(onSave func(), onInvalidate func()) *EventListener {
    51  	return &EventListener{
    52  		stop:         make(chan struct{}),
    53  		onSave:       onSave,
    54  		onInvalidate: onInvalidate,
    55  	}
    56  }
    57  
    58  // Stop the event listener
    59  func (e *EventListener) Stop() {
    60  	e.stop <- struct{}{}
    61  }
    62  
    63  // Listen is meant to be run as a goroutine that updates/invalidates the cache when events occur
    64  func (e *EventListener) Listen(cache stored_requests.Cache, events EventProducer) {
    65  	for {
    66  		select {
    67  		case save := <-events.Saves():
    68  			cache.Requests.Save(context.Background(), save.Requests)
    69  			cache.Imps.Save(context.Background(), save.Imps)
    70  			cache.Accounts.Save(context.Background(), save.Accounts)
    71  			cache.Responses.Save(context.Background(), save.Responses)
    72  			if e.onSave != nil {
    73  				e.onSave()
    74  			}
    75  		case invalidation := <-events.Invalidations():
    76  			cache.Requests.Invalidate(context.Background(), invalidation.Requests)
    77  			cache.Imps.Invalidate(context.Background(), invalidation.Imps)
    78  			cache.Accounts.Invalidate(context.Background(), invalidation.Accounts)
    79  			cache.Responses.Invalidate(context.Background(), invalidation.Responses)
    80  			if e.onInvalidate != nil {
    81  				e.onInvalidate()
    82  			}
    83  		case <-e.stop:
    84  			return
    85  		}
    86  	}
    87  }