2020-04-29 12:11:13 +02:00
|
|
|
package spooler
|
|
|
|
|
|
|
|
import (
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1"
|
2020-12-17 08:55:11 +01:00
|
|
|
"math/rand"
|
2020-07-28 09:42:21 +02:00
|
|
|
"os"
|
|
|
|
|
2020-04-29 12:11:13 +02:00
|
|
|
"github.com/caos/logging"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/query"
|
2020-05-28 13:28:36 +02:00
|
|
|
"github.com/caos/zitadel/internal/id"
|
2020-04-29 12:11:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2021-02-23 15:13:04 +01:00
|
|
|
Eventstore v1.Eventstore
|
2020-07-28 09:42:21 +02:00
|
|
|
Locker Locker
|
|
|
|
ViewHandlers []query.Handler
|
|
|
|
ConcurrentWorkers int
|
2020-04-29 12:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) New() *Spooler {
|
2020-07-28 09:42:21 +02: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 12:11:13 +02:00
|
|
|
|
2020-12-17 08:55:11 +01: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 12:11:13 +02:00
|
|
|
return &Spooler{
|
2020-07-28 09:42:21 +02:00
|
|
|
handlers: c.ViewHandlers,
|
|
|
|
lockID: lockID,
|
|
|
|
eventstore: c.Eventstore,
|
|
|
|
locker: c.Locker,
|
2020-12-02 08:50:59 +01:00
|
|
|
queue: make(chan *spooledHandler, len(c.ViewHandlers)),
|
2020-07-28 09:42:21 +02:00
|
|
|
workers: c.ConcurrentWorkers,
|
2020-04-29 12:11:13 +02:00
|
|
|
}
|
|
|
|
}
|