From 80a594802f74872b8c50c61c7c4cd82a68d96b52 Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Sun, 12 May 2019 04:30:48 -0400 Subject: [PATCH] remove more indirections --- irc/connection_limits/limiter.go | 15 ++++----------- irc/connection_limits/throttler.go | 15 ++++----------- irc/connection_limits/throttler_test.go | 4 ++-- irc/monitor.go | 10 +++------- irc/server.go | 24 +++++++++++------------- irc/snomanager.go | 5 +---- 6 files changed, 25 insertions(+), 48 deletions(-) diff --git a/irc/connection_limits/limiter.go b/irc/connection_limits/limiter.go index d7572a94..8ce15a39 100644 --- a/irc/connection_limits/limiter.go +++ b/irc/connection_limits/limiter.go @@ -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) diff --git a/irc/connection_limits/throttler.go b/irc/connection_limits/throttler.go index 19f589c2..99a785ba 100644 --- a/irc/connection_limits/throttler.go +++ b/irc/connection_limits/throttler.go @@ -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) diff --git a/irc/connection_limits/throttler_test.go b/irc/connection_limits/throttler_test.go index c31c4276..08a79f89 100644 --- a/irc/connection_limits/throttler_test.go +++ b/irc/connection_limits/throttler_test.go @@ -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) { diff --git a/irc/monitor.go b/irc/monitor.go index bf92b969..801d2202 100644 --- a/irc/monitor.go +++ b/irc/monitor.go @@ -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. diff --git a/irc/server.go b/irc/server.go index 6944973a..b8830a50 100644 --- a/irc/server.go +++ b/irc/server.go @@ -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 diff --git a/irc/snomanager.go b/irc/snomanager.go index d6f3b657..b7e48107 100644 --- a/irc/snomanager.go +++ b/irc/snomanager.go @@ -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.