3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

remove more indirections

This commit is contained in:
Shivaram Lingamneni 2019-05-12 04:30:48 -04:00
parent 353aeb0389
commit 80a594802f
6 changed files with 25 additions and 48 deletions

View File

@ -98,17 +98,6 @@ func (cl *Limiter) RemoveClient(addr net.IP) {
}
}
// NewLimiter returns a new connection limit handler.
// The handler is functional, but disabled; it can be enabled via `ApplyConfig`.
func NewLimiter() *Limiter {
var cl Limiter
// initialize empty population; all other state is configurable
cl.population = make(map[string]int)
return &cl
}
// ApplyConfig atomically applies a config update to a connection limit handler
func (cl *Limiter) ApplyConfig(config LimiterConfig) error {
// assemble exempted nets
@ -120,6 +109,10 @@ func (cl *Limiter) ApplyConfig(config LimiterConfig) error {
cl.Lock()
defer cl.Unlock()
if cl.population == nil {
cl.population = make(map[string]int)
}
cl.enabled = config.Enabled
cl.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32)
cl.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128)

View File

@ -150,17 +150,6 @@ func (ct *Throttler) BanMessage() string {
return ct.banMessage
}
// NewThrottler returns a new client connection throttler.
// The throttler is functional, but disabled; it can be enabled via `ApplyConfig`.
func NewThrottler() *Throttler {
var ct Throttler
// initialize empty population; all other state is configurable
ct.population = make(map[string]ThrottleDetails)
return &ct
}
// ApplyConfig atomically applies a config update to a throttler
func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error {
// assemble exempted nets
@ -172,6 +161,10 @@ func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error {
ct.Lock()
defer ct.Unlock()
if ct.population == nil {
ct.population = make(map[string]ThrottleDetails)
}
ct.enabled = config.Enabled
ct.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32)
ct.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128)

View File

@ -72,9 +72,9 @@ func makeTestThrottler(v4len, v6len int) *Throttler {
ConnectionsPerCidr: maxConnections,
Duration: minute,
}
throttler := NewThrottler()
var throttler Throttler
throttler.ApplyConfig(config)
return throttler
return &throttler
}
func TestConnectionThrottle(t *testing.T) {

View File

@ -19,13 +19,9 @@ type MonitorManager struct {
// (all nicks must be normalized externally by casefolding)
}
// NewMonitorManager returns a new MonitorManager.
func NewMonitorManager() *MonitorManager {
mm := MonitorManager{
watching: make(map[*Client]map[string]bool),
watchedby: make(map[string]map[*Client]bool),
}
return &mm
func (mm *MonitorManager) Initialize() {
mm.watching = make(map[*Client]map[string]bool)
mm.watchedby = make(map[string]map[*Client]bool)
}
// AlertAbout alerts everyone monitoring `client`'s nick that `client` is now {on,off}line.

View File

@ -67,15 +67,15 @@ type Server struct {
clients ClientManager
config unsafe.Pointer
configFilename string
connectionLimiter *connection_limits.Limiter
connectionThrottler *connection_limits.Throttler
connectionLimiter connection_limits.Limiter
connectionThrottler connection_limits.Throttler
ctime time.Time
dlines *DLineManager
helpIndexManager HelpIndexManager
klines *KLineManager
listeners map[string]*ListenerWrapper
logger *logger.Manager
monitorManager *MonitorManager
monitorManager MonitorManager
name string
nameCasefolded string
rehashMutex sync.Mutex // tier 4
@ -83,7 +83,7 @@ type Server struct {
pprofServer *http.Server
resumeManager ResumeManager
signals chan os.Signal
snomasks *SnoManager
snomasks SnoManager
store *buntdb.DB
torLimiter connection_limits.TorLimiter
whoWas WhoWasList
@ -110,21 +110,19 @@ type clientConn struct {
func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
// initialize data structures
server := &Server{
ctime: time.Now().UTC(),
connectionLimiter: connection_limits.NewLimiter(),
connectionThrottler: connection_limits.NewThrottler(),
listeners: make(map[string]*ListenerWrapper),
logger: logger,
monitorManager: NewMonitorManager(),
rehashSignal: make(chan os.Signal, 1),
signals: make(chan os.Signal, len(ServerExitSignals)),
snomasks: NewSnoManager(),
ctime: time.Now().UTC(),
listeners: make(map[string]*ListenerWrapper),
logger: logger,
rehashSignal: make(chan os.Signal, 1),
signals: make(chan os.Signal, len(ServerExitSignals)),
}
server.clients.Initialize()
server.semaphores.Initialize()
server.resumeManager.Initialize(server)
server.whoWas.Initialize(config.Limits.WhowasEntries)
server.monitorManager.Initialize()
server.snomasks.Initialize()
if err := server.applyConfig(config, true); err != nil {
return nil, err

View File

@ -14,11 +14,8 @@ type SnoManager struct {
sendLists map[sno.Mask]map[*Client]bool
}
// NewSnoManager returns a new SnoManager
func NewSnoManager() *SnoManager {
var m SnoManager
func (m *SnoManager) Initialize() {
m.sendLists = make(map[sno.Mask]map[*Client]bool)
return &m
}
// AddMasks adds the given snomasks to the client.