wheat-cache/pkg/middle/middleware.go

75 lines
1.8 KiB
Go
Raw Normal View History

package middle
import (
2021-11-02 14:45:08 +08:00
_ "gitee.com/wheat-os/wheatCache/conf"
"gitee.com/wheat-os/wheatCache/pkg/event"
"gitee.com/wheat-os/wheatCache/plugins"
"gitee.com/wheat-os/wheatCache/plugins/config"
"github.com/spf13/viper"
)
type MiddleWare struct {
eventDriver event.DriverInterface
eventConsumer event.ConsumerInterface
eventProduce event.ProduceInterface
plugins map[string][]plugins.PluginInterface
consumerCount int
2021-10-10 14:09:09 +08:00
driverCount int
}
func NewMiddleWare() *MiddleWare {
oneMiddle.Do(func() {
2021-10-10 14:09:09 +08:00
consumerCount, driverCount := loadConfigAndDefault()
driver := event.NewDriver(driverCount)
2021-10-10 14:09:09 +08:00
middleWareDriver = &MiddleWare{
eventDriver: driver,
eventConsumer: event.NewConsumer(driver),
eventProduce: event.NewProduce(driver),
2021-10-10 14:09:09 +08:00
driverCount: driverCount,
consumerCount: consumerCount,
}
2021-10-10 14:09:09 +08:00
middleWareDriver.loadPlugins()
// 多消费 middle
2021-10-10 14:09:09 +08:00
middleWareDriver.startWork()
})
2021-10-10 14:09:09 +08:00
return middleWareDriver
}
func (m *MiddleWare) GetEventDriver() event.DriverInterface {
return m.eventDriver
}
func (m *MiddleWare) loadPlugins() {
plug := viper.GetStringMapStringSlice("plugins-control")
pluginsMap := config.GetMiddlewareMap()
pluginsContext := make(map[string][]plugins.PluginInterface)
2021-10-12 15:10:53 +08:00
for msg, pluNames := range plug {
pulgSingle := make([]plugins.PluginInterface, 0)
for _, name := range pluNames {
pulgSingle = append(pulgSingle, pluginsMap[name])
}
2021-10-12 15:10:53 +08:00
pluginsContext[msg] = pulgSingle
}
m.plugins = pluginsContext
}
func loadConfigAndDefault() (int, int) {
// 加载 consumerCount
2021-10-10 14:09:09 +08:00
consumerCount := viper.GetInt("middle-driver.middleConsumerCount")
if consumerCount == 0 {
consumerCount = defaultConsumerCount
}
2021-10-10 14:09:09 +08:00
driverCount := viper.GetInt("middle-driver.driverCount")
if driverCount == 0 {
driverCount = defaultDriverCount
}
return consumerCount, driverCount
}