syntax refactor: use shorter object name in methods

Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
Luca Bigliardi 2021-03-21 19:48:11 +01:00
parent 9f86b35aa5
commit 9c53b88cd9
2 changed files with 71 additions and 71 deletions

14
http.go
View File

@ -80,7 +80,7 @@ func NewHTTPServerForTesting(config *Config, alertMsgs chan AlertMsg,
return server, nil return server, nil
} }
func (server *HTTPServer) RelayAlert(w http.ResponseWriter, r *http.Request) { func (s *HTTPServer) RelayAlert(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
ircChannel := "#" + vars["IRCChannel"] ircChannel := "#" + vars["IRCChannel"]
@ -104,10 +104,10 @@ func (server *HTTPServer) RelayAlert(w http.ResponseWriter, r *http.Request) {
return return
} }
handledAlertGroups.WithLabelValues(ircChannel).Inc() handledAlertGroups.WithLabelValues(ircChannel).Inc()
for _, alertMsg := range server.formatter.GetMsgsFromAlertMessage( for _, alertMsg := range s.formatter.GetMsgsFromAlertMessage(
ircChannel, &alertMessage) { ircChannel, &alertMessage) {
select { select {
case server.AlertMsgs <- alertMsg: case s.AlertMsgs <- alertMsg:
handledAlerts.WithLabelValues(ircChannel).Inc() handledAlerts.WithLabelValues(ircChannel).Inc()
default: default:
log.Printf("Could not send this alert to the IRC routine: %s", log.Printf("Could not send this alert to the IRC routine: %s",
@ -117,20 +117,20 @@ func (server *HTTPServer) RelayAlert(w http.ResponseWriter, r *http.Request) {
} }
} }
func (server *HTTPServer) Run() { func (s *HTTPServer) Run() {
router := mux.NewRouter().StrictSlash(true) router := mux.NewRouter().StrictSlash(true)
router.Path("/metrics").Handler(promhttp.Handler()) router.Path("/metrics").Handler(promhttp.Handler())
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server.RelayAlert(w, r) s.RelayAlert(w, r)
}) })
router.Path("/{IRCChannel}").Handler(handler).Methods("POST") router.Path("/{IRCChannel}").Handler(handler).Methods("POST")
listenAddr := strings.Join( listenAddr := strings.Join(
[]string{server.Addr, strconv.Itoa(server.Port)}, ":") []string{s.Addr, strconv.Itoa(s.Port)}, ":")
log.Printf("Starting HTTP server") log.Printf("Starting HTTP server")
if err := server.httpListener(listenAddr, router); err != nil { if err := s.httpListener(listenAddr, router); err != nil {
log.Printf("Could not start http server: %s", err) log.Printf("Could not start http server: %s", err)
} }
} }

128
irc.go
View File

@ -152,156 +152,156 @@ func NewIRCNotifier(ctx context.Context, stopWg *sync.WaitGroup, config *Config,
return notifier, nil return notifier, nil
} }
func (notifier *IRCNotifier) HandleKick(nick string, channel string) { func (n *IRCNotifier) HandleKick(nick string, channel string) {
if nick != notifier.Client.Me().Nick { if nick != n.Client.Me().Nick {
// received kick info for somebody else // received kick info for somebody else
return return
} }
state, ok := notifier.JoinedChannels[channel] state, ok := n.JoinedChannels[channel]
if !ok { if !ok {
log.Printf("Being kicked out of non-joined channel (%s), ignoring", channel) log.Printf("Being kicked out of non-joined channel (%s), ignoring", channel)
return return
} }
log.Printf("Being kicked out of %s, re-joining", channel) log.Printf("Being kicked out of %s, re-joining", channel)
go func() { go func() {
if ok := state.BackoffCounter.DelayContext(notifier.ctx); !ok { if ok := state.BackoffCounter.DelayContext(n.ctx); !ok {
return return
} }
notifier.Client.Join(state.Channel.Name, state.Channel.Password) n.Client.Join(state.Channel.Name, state.Channel.Password)
}() }()
} }
func (notifier *IRCNotifier) CleanupChannels() { func (n *IRCNotifier) CleanupChannels() {
log.Printf("Deregistering all channels.") log.Printf("Deregistering all channels.")
notifier.JoinedChannels = make(map[string]ChannelState) n.JoinedChannels = make(map[string]ChannelState)
} }
func (notifier *IRCNotifier) JoinChannel(channel *IRCChannel) { func (n *IRCNotifier) JoinChannel(channel *IRCChannel) {
if _, joined := notifier.JoinedChannels[channel.Name]; joined { if _, joined := n.JoinedChannels[channel.Name]; joined {
return return
} }
log.Printf("Joining %s", channel.Name) log.Printf("Joining %s", channel.Name)
notifier.Client.Join(channel.Name, channel.Password) n.Client.Join(channel.Name, channel.Password)
state := ChannelState{ state := ChannelState{
Channel: *channel, Channel: *channel,
BackoffCounter: NewBackoff( BackoffCounter: NewBackoff(
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs, ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
time.Second), time.Second),
} }
notifier.JoinedChannels[channel.Name] = state n.JoinedChannels[channel.Name] = state
} }
func (notifier *IRCNotifier) JoinChannels() { func (n *IRCNotifier) JoinChannels() {
for _, channel := range notifier.PreJoinChannels { for _, channel := range n.PreJoinChannels {
notifier.JoinChannel(&channel) n.JoinChannel(&channel)
} }
} }
func (notifier *IRCNotifier) MaybeIdentifyNick() { func (n *IRCNotifier) MaybeIdentifyNick() {
if notifier.NickPassword == "" { if n.NickPassword == "" {
return return
} }
// Very lazy/optimistic, but this is good enough for my irssi config, // Very lazy/optimistic, but this is good enough for my irssi config,
// so it should work here as well. // so it should work here as well.
currentNick := notifier.Client.Me().Nick currentNick := n.Client.Me().Nick
if currentNick != notifier.Nick { if currentNick != n.Nick {
log.Printf("My nick is '%s', sending GHOST to NickServ to get '%s'", log.Printf("My nick is '%s', sending GHOST to NickServ to get '%s'",
currentNick, notifier.Nick) currentNick, n.Nick)
notifier.Client.Privmsgf("NickServ", "GHOST %s %s", notifier.Nick, n.Client.Privmsgf("NickServ", "GHOST %s %s", n.Nick,
notifier.NickPassword) n.NickPassword)
time.Sleep(notifier.NickservDelayWait) time.Sleep(n.NickservDelayWait)
log.Printf("Changing nick to '%s'", notifier.Nick) log.Printf("Changing nick to '%s'", n.Nick)
notifier.Client.Nick(notifier.Nick) n.Client.Nick(n.Nick)
} }
log.Printf("Sending IDENTIFY to NickServ") log.Printf("Sending IDENTIFY to NickServ")
notifier.Client.Privmsgf("NickServ", "IDENTIFY %s", notifier.NickPassword) n.Client.Privmsgf("NickServ", "IDENTIFY %s", n.NickPassword)
time.Sleep(notifier.NickservDelayWait) time.Sleep(n.NickservDelayWait)
} }
func (notifier *IRCNotifier) MaybeSendAlertMsg(alertMsg *AlertMsg) { func (n *IRCNotifier) MaybeSendAlertMsg(alertMsg *AlertMsg) {
if !notifier.sessionUp { if !n.sessionUp {
log.Printf("Cannot send alert to %s : IRC not connected", log.Printf("Cannot send alert to %s : IRC not connected",
alertMsg.Channel) alertMsg.Channel)
ircSendMsgErrors.WithLabelValues(alertMsg.Channel, "not_connected").Inc() ircSendMsgErrors.WithLabelValues(alertMsg.Channel, "not_connected").Inc()
return return
} }
notifier.JoinChannel(&IRCChannel{Name: alertMsg.Channel}) n.JoinChannel(&IRCChannel{Name: alertMsg.Channel})
if notifier.UsePrivmsg { if n.UsePrivmsg {
notifier.Client.Privmsg(alertMsg.Channel, alertMsg.Alert) n.Client.Privmsg(alertMsg.Channel, alertMsg.Alert)
} else { } else {
notifier.Client.Notice(alertMsg.Channel, alertMsg.Alert) n.Client.Notice(alertMsg.Channel, alertMsg.Alert)
} }
ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc() ircSentMsgs.WithLabelValues(alertMsg.Channel).Inc()
} }
func (notifier *IRCNotifier) ShutdownPhase() { func (n *IRCNotifier) ShutdownPhase() {
if notifier.Client.Connected() { if n.Client.Connected() {
log.Printf("IRC client connected, quitting") log.Printf("IRC client connected, quitting")
notifier.Client.Quit("see ya") n.Client.Quit("see ya")
if notifier.sessionUp { if n.sessionUp {
log.Printf("Session is up, wait for IRC disconnect to complete") log.Printf("Session is up, wait for IRC disconnect to complete")
select { select {
case <-notifier.sessionDownSignal: case <-n.sessionDownSignal:
case <-time.After(notifier.Client.Config().Timeout): case <-time.After(n.Client.Config().Timeout):
log.Printf("Timeout while waiting for IRC disconnect to complete, stopping anyway") log.Printf("Timeout while waiting for IRC disconnect to complete, stopping anyway")
} }
} }
} }
} }
func (notifier *IRCNotifier) ConnectedPhase() { func (n *IRCNotifier) ConnectedPhase() {
select { select {
case alertMsg := <-notifier.AlertMsgs: case alertMsg := <-n.AlertMsgs:
notifier.MaybeSendAlertMsg(&alertMsg) n.MaybeSendAlertMsg(&alertMsg)
case <-notifier.sessionDownSignal: case <-n.sessionDownSignal:
notifier.sessionUp = false n.sessionUp = false
notifier.CleanupChannels() n.CleanupChannels()
notifier.Client.Quit("see ya") n.Client.Quit("see ya")
ircConnectedGauge.Set(0) ircConnectedGauge.Set(0)
case <-notifier.ctx.Done(): case <-n.ctx.Done():
log.Printf("IRC routine asked to terminate") log.Printf("IRC routine asked to terminate")
} }
} }
func (notifier *IRCNotifier) SetupPhase() { func (n *IRCNotifier) SetupPhase() {
if !notifier.Client.Connected() { if !n.Client.Connected() {
log.Printf("Connecting to IRC %s", notifier.Client.Config().Server) log.Printf("Connecting to IRC %s", n.Client.Config().Server)
if ok := notifier.BackoffCounter.DelayContext(notifier.ctx); !ok { if ok := n.BackoffCounter.DelayContext(n.ctx); !ok {
return return
} }
if err := notifier.Client.ConnectContext(notifier.ctx); err != nil { if err := n.Client.ConnectContext(n.ctx); err != nil {
log.Printf("Could not connect to IRC: %s", err) log.Printf("Could not connect to IRC: %s", err)
return return
} }
log.Printf("Connected to IRC server, waiting to establish session") log.Printf("Connected to IRC server, waiting to establish session")
} }
select { select {
case <-notifier.sessionUpSignal: case <-n.sessionUpSignal:
notifier.sessionUp = true n.sessionUp = true
notifier.MaybeIdentifyNick() n.MaybeIdentifyNick()
notifier.JoinChannels() n.JoinChannels()
ircConnectedGauge.Set(1) ircConnectedGauge.Set(1)
case <-notifier.sessionDownSignal: case <-n.sessionDownSignal:
log.Printf("Receiving a session down before the session is up, this is odd") log.Printf("Receiving a session down before the session is up, this is odd")
case <-notifier.ctx.Done(): case <-n.ctx.Done():
log.Printf("IRC routine asked to terminate") log.Printf("IRC routine asked to terminate")
} }
} }
func (notifier *IRCNotifier) Run() { func (n *IRCNotifier) Run() {
defer notifier.stopWg.Done() defer n.stopWg.Done()
for notifier.ctx.Err() != context.Canceled { for n.ctx.Err() != context.Canceled {
if !notifier.sessionUp { if !n.sessionUp {
notifier.SetupPhase() n.SetupPhase()
} else { } else {
notifier.ConnectedPhase() n.ConnectedPhase()
} }
} }
notifier.ShutdownPhase() n.ShutdownPhase()
} }