alarm

package
v0.0.0-...-d31700d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 7 Imported by: 0

README

alarm

  • merge multiple same alarms into one

Documentation

Overview

Alarm合并报警邮件,防止在出错高峰,收到大量重复报警邮件,甚至因为邮件过多导致发送失败、接收失败。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLocation

func SetLocation(location *time.Location) option

SetLocation set the location of the `StartAt` and `EndAt` of `Context`

func SetPrefix

func SetPrefix(pre string) option

SetPrefix set the prefix of email title

Types

type Alarm

type Alarm struct {
	sync.Mutex
	// contains filtered or unexported fields
}
Example (Test)
package main

import (
	"fmt"
	"sort"
	"sync"
	"time"
)

const redundant = 100 * time.Millisecond

type testSenderT struct {
	msgs []string
	sync.Mutex
}

func (ts *testSenderT) Send(title, content string, ctx Context) {
	t := time.Now().UTC().Round(24 * time.Hour)
	ctx.StartedAt, ctx.EndedAt = t, t
	ts.Lock()
	ts.msgs = append(ts.msgs, fmt.Sprintf("%s 标题%s 内容%s", ctx.String(), title, content))
	ts.Unlock()
}

func (ts *testSenderT) printMsgs() {
	sort.Strings(ts.msgs)
	for _, msg := range ts.msgs {
		fmt.Println(msg)
	}
}

func (ts *testSenderT) empty() {
	ts.msgs = []string{}
}

func main() {
	sender := &testSenderT{}
	waits := []time.Duration{500 * time.Millisecond}
	alarm := New(sender, waits)

	sender.empty()
	sendAlarms(alarm, map[string]int{`a`: 1, `b`: 2, `c`: 3})
	time.Sleep(waits[0] + redundant)
	sender.printMsgs()

	sender.empty()
	sendAlarms(alarm, map[string]int{`a`: 1, `b`: 2, `c`: 3})
	time.Sleep(waits[0] + redundant)
	sender.printMsgs()
}

func sendAlarms(alarm *Alarm, alarms map[string]int) {
	var wg sync.WaitGroup
	for mergeKey, count := range alarms {
		wg.Add(count)
		for i := 0; i < count; i++ {
			go func(mergeKey string) {
				defer wg.Done()
				alarm.Alarm(mergeKey, mergeKey, mergeKey)
			}(mergeKey)
		}
	}
	wg.Wait()
}
Output:

[#1 00:00:00] 标题a 内容a
[#1 merged:2 00:00:00] 标题b 内容b
[#1 merged:3 00:00:00] 标题c 内容c
[#2 00:00:00] 标题a 内容a
[#2 merged:2 00:00:00] 标题b 内容b
[#2 merged:3 00:00:00] 标题c 内容c

func New

func New(sender Sender, waits []time.Duration, options ...option) *Alarm

func (*Alarm) Alarm

func (alm *Alarm) Alarm(title, content, mergeKey string)

func (*Alarm) Send

func (alm *Alarm) Send(title, content string)

Send sent alarm directly without waiting. logger package uses this method.

func (*Alarm) SetWaits

func (alm *Alarm) SetWaits(waits []time.Duration)

type Context

type Context struct {
	SentCount   int
	MergedCount int
	StartedAt   time.Time
	EndedAt     time.Time
}

func (Context) String

func (ctx Context) String() string

type MailSender

type MailSender struct {
	Receivers []string
	Mailer    *email.Client
}

func (MailSender) Send

func (m MailSender) Send(title, content string, ctx Context)

type Sender

type Sender interface {
	Send(title, content string, ctx Context)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL