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
|
|
|
"os"
|
|
|
|
|
2020-04-29 10:11:13 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-07-28 07:42:21 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
2020-05-28 11:28:36 +00:00
|
|
|
"github.com/caos/zitadel/internal/id"
|
2020-04-29 10:11:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2020-07-28 07:42:21 +00:00
|
|
|
Eventstore eventstore.Eventstore
|
|
|
|
Locker Locker
|
|
|
|
ViewHandlers []query.Handler
|
|
|
|
ConcurrentWorkers int
|
2020-04-29 10:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) New() *Spooler {
|
2020-07-28 07:42:21 +00:00
|
|
|
lockID, err := os.Hostname()
|
|
|
|
if err != nil || lockID == "" {
|
|
|
|
lockID, err = id.SonyFlakeGenerator.Next()
|
|
|
|
logging.Log("SPOOL-bdO56").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
|
|
|
}
|
|
|
|
}
|