2020-04-11 09:33:10 +01:00
|
|
|
package sync
|
2020-03-18 16:39:36 +00:00
|
|
|
|
|
|
|
import (
|
2020-08-25 13:44:41 +03:00
|
|
|
"fmt"
|
2020-03-18 16:39:36 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-20 15:23:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/store"
|
2020-03-18 16:39:36 +00:00
|
|
|
)
|
|
|
|
|
2020-04-11 09:33:10 +01:00
|
|
|
func (c *syncStore) syncManager() {
|
2020-03-18 16:39:36 +00:00
|
|
|
tickerAggregator := make(chan struct{ index int })
|
|
|
|
for i, ticker := range c.pendingWriteTickers {
|
|
|
|
go func(index int, c chan struct{ index int }, t *time.Ticker) {
|
|
|
|
for range t.C {
|
|
|
|
c <- struct{ index int }{index: index}
|
|
|
|
}
|
|
|
|
}(i, tickerAggregator, ticker)
|
|
|
|
}
|
2021-02-13 01:46:16 +03:00
|
|
|
for i := range tickerAggregator {
|
|
|
|
println(i.index, "ticked")
|
|
|
|
c.processQueue(i.index)
|
2020-03-18 16:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 09:33:10 +01:00
|
|
|
func (c *syncStore) processQueue(index int) {
|
2020-03-18 16:39:36 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
q := c.pendingWrites[index]
|
|
|
|
for i := 0; i < q.Len(); i++ {
|
|
|
|
r, ok := q.PopFront()
|
|
|
|
if !ok {
|
2020-08-25 13:44:41 +03:00
|
|
|
panic(fmt.Errorf("retrieved an invalid value from the L%d sync queue", index+1))
|
2020-03-18 16:39:36 +00:00
|
|
|
}
|
|
|
|
ir, ok := r.(*internalRecord)
|
|
|
|
if !ok {
|
2020-08-25 13:44:41 +03:00
|
|
|
panic(fmt.Errorf("retrieved a non-internal record from the L%d sync queue", index+1))
|
2020-03-18 16:39:36 +00:00
|
|
|
}
|
|
|
|
if !ir.expiresAt.IsZero() && time.Now().After(ir.expiresAt) {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-10 22:08:56 +03:00
|
|
|
var opts []store.WriteOption
|
2020-03-18 16:39:36 +00:00
|
|
|
if !ir.expiresAt.IsZero() {
|
2020-12-10 22:08:56 +03:00
|
|
|
opts = append(opts, store.WriteTTL(ir.expiresAt.Sub(time.Now())))
|
2020-03-18 16:39:36 +00:00
|
|
|
}
|
|
|
|
// Todo = internal queue also has to hold the corresponding store.WriteOptions
|
2020-12-10 22:08:56 +03:00
|
|
|
if err := c.syncOpts.Stores[index+1].Write(c.storeOpts.Context, ir.key, ir.value, opts...); err != nil {
|
2020-03-18 16:39:36 +00:00
|
|
|
// some error, so queue for retry and bail
|
|
|
|
q.PushBack(ir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func intpow(x, y int64) int64 {
|
|
|
|
result := int64(1)
|
|
|
|
for 0 != y {
|
|
|
|
if 0 != (y & 1) {
|
|
|
|
result *= x
|
|
|
|
}
|
|
|
|
y >>= 1
|
|
|
|
x *= x
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|