2020-04-29 10:11:13 +00:00
|
|
|
package spooler
|
|
|
|
|
|
|
|
import (
|
2020-12-17 07:55:11 +00:00
|
|
|
"math/rand"
|
2020-07-28 07:42:21 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-15 06:19:02 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
2020-04-29 10:11:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2021-02-23 14:13:04 +00:00
|
|
|
Eventstore v1.Eventstore
|
2020-07-28 07:42:21 +00:00
|
|
|
Locker Locker
|
|
|
|
ViewHandlers []query.Handler
|
|
|
|
ConcurrentWorkers int
|
2020-04-29 10:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) New() *Spooler {
|
2022-05-04 15:09:49 +00:00
|
|
|
lockID, err := id.SonyFlakeGenerator.Next()
|
|
|
|
logging.OnError(err).Panic("unable to generate lockID")
|
2020-04-29 10:11:13 +00:00
|
|
|
|
2020-12-17 07:55:11 +00:00
|
|
|
//shuffle the handlers for better balance when running multiple pods
|
|
|
|
rand.Shuffle(len(c.ViewHandlers), func(i, j int) {
|
|
|
|
c.ViewHandlers[i], c.ViewHandlers[j] = c.ViewHandlers[j], c.ViewHandlers[i]
|
|
|
|
})
|
|
|
|
|
2020-04-29 10:11:13 +00:00
|
|
|
return &Spooler{
|
2020-07-28 07:42:21 +00:00
|
|
|
handlers: c.ViewHandlers,
|
|
|
|
lockID: lockID,
|
|
|
|
eventstore: c.Eventstore,
|
|
|
|
locker: c.Locker,
|
2020-12-02 07:50:59 +00:00
|
|
|
queue: make(chan *spooledHandler, len(c.ViewHandlers)),
|
2020-07-28 07:42:21 +00:00
|
|
|
workers: c.ConcurrentWorkers,
|
2020-04-29 10:11:13 +00:00
|
|
|
}
|
|
|
|
}
|