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 // ApplyConfig atomically applies a config update to a connection limit handler
func (cl *Limiter) ApplyConfig(config LimiterConfig) error { func (cl *Limiter) ApplyConfig(config LimiterConfig) error {
// assemble exempted nets // assemble exempted nets
@ -120,6 +109,10 @@ func (cl *Limiter) ApplyConfig(config LimiterConfig) error {
cl.Lock() cl.Lock()
defer cl.Unlock() defer cl.Unlock()
if cl.population == nil {
cl.population = make(map[string]int)
}
cl.enabled = config.Enabled cl.enabled = config.Enabled
cl.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32) cl.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32)
cl.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128) cl.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128)

View File

@ -150,17 +150,6 @@ func (ct *Throttler) BanMessage() string {
return ct.banMessage 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 // ApplyConfig atomically applies a config update to a throttler
func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error { func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error {
// assemble exempted nets // assemble exempted nets
@ -172,6 +161,10 @@ func (ct *Throttler) ApplyConfig(config ThrottlerConfig) error {
ct.Lock() ct.Lock()
defer ct.Unlock() defer ct.Unlock()
if ct.population == nil {
ct.population = make(map[string]ThrottleDetails)
}
ct.enabled = config.Enabled ct.enabled = config.Enabled
ct.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32) ct.ipv4Mask = net.CIDRMask(config.CidrLenIPv4, 32)
ct.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128) ct.ipv6Mask = net.CIDRMask(config.CidrLenIPv6, 128)

View File

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

View File

@ -19,13 +19,9 @@ type MonitorManager struct {
// (all nicks must be normalized externally by casefolding) // (all nicks must be normalized externally by casefolding)
} }
// NewMonitorManager returns a new MonitorManager. func (mm *MonitorManager) Initialize() {
func NewMonitorManager() *MonitorManager { mm.watching = make(map[*Client]map[string]bool)
mm := MonitorManager{ mm.watchedby = make(map[string]map[*Client]bool)
watching: make(map[*Client]map[string]bool),
watchedby: make(map[string]map[*Client]bool),
}
return &mm
} }
// AlertAbout alerts everyone monitoring `client`'s nick that `client` is now {on,off}line. // 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 clients ClientManager
config unsafe.Pointer config unsafe.Pointer
configFilename string configFilename string
connectionLimiter *connection_limits.Limiter connectionLimiter connection_limits.Limiter
connectionThrottler *connection_limits.Throttler connectionThrottler connection_limits.Throttler
ctime time.Time ctime time.Time
dlines *DLineManager dlines *DLineManager
helpIndexManager HelpIndexManager helpIndexManager HelpIndexManager
klines *KLineManager klines *KLineManager
listeners map[string]*ListenerWrapper listeners map[string]*ListenerWrapper
logger *logger.Manager logger *logger.Manager
monitorManager *MonitorManager monitorManager MonitorManager
name string name string
nameCasefolded string nameCasefolded string
rehashMutex sync.Mutex // tier 4 rehashMutex sync.Mutex // tier 4
@ -83,7 +83,7 @@ type Server struct {
pprofServer *http.Server pprofServer *http.Server
resumeManager ResumeManager resumeManager ResumeManager
signals chan os.Signal signals chan os.Signal
snomasks *SnoManager snomasks SnoManager
store *buntdb.DB store *buntdb.DB
torLimiter connection_limits.TorLimiter torLimiter connection_limits.TorLimiter
whoWas WhoWasList whoWas WhoWasList
@ -111,20 +111,18 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
// initialize data structures // initialize data structures
server := &Server{ server := &Server{
ctime: time.Now().UTC(), ctime: time.Now().UTC(),
connectionLimiter: connection_limits.NewLimiter(),
connectionThrottler: connection_limits.NewThrottler(),
listeners: make(map[string]*ListenerWrapper), listeners: make(map[string]*ListenerWrapper),
logger: logger, logger: logger,
monitorManager: NewMonitorManager(),
rehashSignal: make(chan os.Signal, 1), rehashSignal: make(chan os.Signal, 1),
signals: make(chan os.Signal, len(ServerExitSignals)), signals: make(chan os.Signal, len(ServerExitSignals)),
snomasks: NewSnoManager(),
} }
server.clients.Initialize() server.clients.Initialize()
server.semaphores.Initialize() server.semaphores.Initialize()
server.resumeManager.Initialize(server) server.resumeManager.Initialize(server)
server.whoWas.Initialize(config.Limits.WhowasEntries) server.whoWas.Initialize(config.Limits.WhowasEntries)
server.monitorManager.Initialize()
server.snomasks.Initialize()
if err := server.applyConfig(config, true); err != nil { if err := server.applyConfig(config, true); err != nil {
return nil, err return nil, err

View File

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