Skip to content

Commit

Permalink
Add an option to notify that a batch updated the cache store.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxki committed Sep 30, 2023
1 parent 95d0623 commit eb2fd2f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
30 changes: 22 additions & 8 deletions cache_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ type CacheBatch struct {
batchSerial int
interval time.Duration
// Options
intervalSec int
delay time.Duration
strict bool
expiration expBehavior
quite chan string
logger *zerolog.Logger
intervalSec int
delay time.Duration
strict bool
expiration expBehavior
quite chan string
updatedNotify chan struct{}
logger *zerolog.Logger
}

// Default values.
Expand Down Expand Up @@ -132,9 +133,18 @@ func WithLogger(logger *zerolog.Logger) func(*CacheBatch) {
// WithQuietChan sets a quiet message channel, which
// stops the loop of dyocsp.CacheBatch.Run(). It also sends a message immediately
// before quieting the loop.
func WithQuiteChan(quite chan string) func(*CacheBatch) {
func WithQuiteChan(ch chan string) func(*CacheBatch) {
return func(c *CacheBatch) {
c.quite = quite
c.quite = ch
}
}

// WithUpdatedNotifyChan sets a channel to notify when the cache store is updated.
// To ensure that the batch waits until a notify is received, the user should
// create a receiver.
func WithUpdatedNotifyChan(ch chan struct{}) func(*CacheBatch) {
return func(c *CacheBatch) {
c.updatedNotify = ch
}
}

Expand Down Expand Up @@ -349,6 +359,10 @@ func (c *CacheBatch) Run(ctx context.Context) {
}
logger.Info().Msg("Response cache updated.")

if c.updatedNotify != nil {
c.updatedNotify <- struct{}{}
}

// Summury of this loop batch
c.logBatchSummary(ctx, startTime)

Expand Down
33 changes: 26 additions & 7 deletions cache_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func TestNewCacheBatch_OptoinsDefaults(t *testing.T) {
if batch.quite != nil {
t.Error("value of quite is not default.")
}
if batch.updatedNotify != nil {
t.Error("value of updatedNotify is not default.")
}
}

func TestNewCacheBatch_OptoinsSet(t *testing.T) {
Expand All @@ -81,9 +84,17 @@ func TestNewCacheBatch_OptoinsSet(t *testing.T) {
responder := testCreateDelegatedResponder(t)
store := cache.NewResponseCacheStore()
logger := zerolog.New(os.Stdout).With().Logger()
ch := make(chan string)
quiteCh := make(chan string)
updatedNotifyCh := make(chan struct{})
batch, err := NewCacheBatch("test-ca", store, client, responder, date.NowGMT(),
WithIntervalSec(10), WithDelay(time.Second*5), WithStrict(true), WithExpiration(Warn), WithLogger(&logger), WithQuiteChan(ch))
WithIntervalSec(10),
WithDelay(time.Second*5),
WithStrict(true),
WithExpiration(Warn),
WithLogger(&logger),
WithQuiteChan(quiteCh),
WithUpdatedNotifyChan(updatedNotifyCh),
)
if err != nil {
t.Fatal(err)
}
Expand All @@ -106,6 +117,9 @@ func TestNewCacheBatch_OptoinsSet(t *testing.T) {
if batch.quite == nil {
t.Error("value of quite is not specified.")
}
if batch.updatedNotify == nil {
t.Error("value of updatedNotify is not specified.")
}
}

func TestNewCacheBatch_OptoinsMinus(t *testing.T) {
Expand Down Expand Up @@ -253,6 +267,7 @@ func TestCacheBatch_Run_DBChanged(t *testing.T) {
client := &StubChangebleCADBClient{"test-ca", currentDB, true}
responder := testCreateDelegatedResponder(t)
store := cache.NewResponseCacheStore()
notifyCh := make(chan struct{})
batch, err := NewCacheBatch(
"test-ca",
store,
Expand All @@ -262,16 +277,16 @@ func TestCacheBatch_Run_DBChanged(t *testing.T) {
WithIntervalSec(1),
WithDelay(0),
WithStrict(false),
WithUpdatedNotifyChan(notifyCh),
)
if err != nil {
t.Fatal(err)
}

go batch.Run(context.TODO())
time.Sleep(time.Second * 1)

// Interval
time.Sleep(time.Second * 2)
<-notifyCh
<-notifyCh

// Do Assertion
cache := testGetCache(t, targetSerial, store)
Expand Down Expand Up @@ -299,6 +314,7 @@ func TestCacheBatch_Run_DelegatedResponder(t *testing.T) {
client := StubCADBClient{"test-ca", fileDB}
responder := testCreateDelegatedResponder(t)
store := cache.NewResponseCacheStore()
notifyCh := make(chan struct{})
batch, err := NewCacheBatch(
"test-ca",
store,
Expand All @@ -308,13 +324,14 @@ func TestCacheBatch_Run_DelegatedResponder(t *testing.T) {
WithIntervalSec(1),
WithDelay(0),
WithStrict(false),
WithUpdatedNotifyChan(notifyCh),
)
if err != nil {
t.Fatal(err)
}

go batch.Run(context.TODO())
time.Sleep(time.Second * 1)
<-notifyCh

cache := testGetCache(t, targetSerialStr, store)

Expand All @@ -341,6 +358,7 @@ func TestCacheBatch_Run_DirectResponder(t *testing.T) {
client := StubCADBClient{"test-ca", fileDB}
responder := testCreateDirectResponder(t)
store := cache.NewResponseCacheStore()
notifyCh := make(chan struct{})
batch, err := NewCacheBatch(
"test-ca",
store,
Expand All @@ -350,13 +368,14 @@ func TestCacheBatch_Run_DirectResponder(t *testing.T) {
WithIntervalSec(1),
WithDelay(0),
WithStrict(false),
WithUpdatedNotifyChan(notifyCh),
)
if err != nil {
t.Fatal(err)
}

go batch.Run(context.TODO())
time.Sleep(time.Second * 1)
<-notifyCh

cache := testGetCache(t, targetSerialStr, store)

Expand Down

0 comments on commit eb2fd2f

Please sign in to comment.