Skip to content

Commit 8a80576

Browse files
committed
Remove as many init() functions as is reasonable
1 parent ac5a25a commit 8a80576

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

dashboard.go

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func index(w http.ResponseWriter, r *http.Request) {
4343
}
4444

4545
func Listen(bindAddr string) error {
46+
// Replace init()'s with explicit inline initializations
47+
githubClient = newGitHubClient()
48+
initProjects()
49+
4650
http.HandleFunc("/reset.json", reset)
4751
http.HandleFunc("/show.json", show)
4852
http.Handle("/triage", triage.New(githubClient, []string{"documentation", "bug", "enhancement"}))

github.go

-4
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,6 @@ type githubCIContext struct {
176176
TypeName string `json:"__typename"`
177177
}
178178

179-
func init() {
180-
githubClient = newGitHubClient()
181-
}
182-
183179
func gitHubToken() string {
184180
return os.Getenv(accessTokenEnvVar)
185181
}

http.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ import (
1010
gh "github.com/google/go-github/v37/github"
1111
)
1212

13-
var throttle <-chan time.Time
14-
15-
func init() {
16-
rate := time.Second / 30
17-
throttle = time.Tick(rate)
18-
}
13+
var allowRequestEvery = time.Second / 30
14+
var throttle <-chan time.Time = time.Tick(allowRequestEvery)
1915

2016
func logHTTP(method, url string, f func()) {
2117
log.Println("==> ", method, url)

project.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
var defaultProjectMap = sync.Map{}
1010

11-
func init() {
11+
func initProjects() {
1212
for _, p := range defaultProjects {
1313
defaultProjectMap.Store(p.Name, p)
1414
}

triage/template.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Filter by using the <b>type</b> (issue/pr/all), <b>label</b> (see below), or <b>
9696
`
9797
)
9898

99-
func init() {
99+
func initTemplates() {
100100
var err error
101101
triageTmpl, err = template.New("triage").Funcs(template.FuncMap{
102102
"daysAgo": daysAgo,

triage/triage.go

+4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import (
55
"log"
66
"net/http"
77
"sort"
8+
"sync"
89
"time"
910

1011
"github.com/google/go-github/v37/github"
1112
)
1213

14+
var once sync.Once
15+
1316
func New(client *github.Client, labelsofInterest []string) *Triager {
17+
once.Do(initTemplates)
1418
return &Triager{
1519
Client: client,
1620
LabelsOfInterest: labelsofInterest,

0 commit comments

Comments
 (0)