1
1
// Copyright 2019 The Go Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
-
5
4
package main
6
5
7
6
import (
@@ -14,6 +13,7 @@ import (
14
13
"strings"
15
14
"time"
16
15
16
+ cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
17
17
"cloud.google.com/go/profiler"
18
18
"contrib.go.opencensus.io/integrations/ocsql"
19
19
"github.com/go-redis/redis/v7"
@@ -28,42 +28,44 @@ import (
28
28
"golang.org/x/pkgsite/internal/postgres"
29
29
"golang.org/x/pkgsite/internal/proxy"
30
30
"golang.org/x/pkgsite/internal/proxydatasource"
31
+ "golang.org/x/pkgsite/internal/queue"
32
+ "golang.org/x/pkgsite/internal/source"
31
33
)
32
34
33
35
var (
36
+ queueName = config .GetEnv ("GO_DISCOVERY_FRONTEND_TASK_QUEUE" , "" )
34
37
staticPath = flag .String ("static" , "content/static" , "path to folder containing static files served" )
35
38
thirdPartyPath = flag .String ("third_party" , "third_party" , "path to folder containing third-party libraries" )
36
39
reloadTemplates = flag .Bool ("reload_templates" , false , "reload templates on each page load (to be used during development)" )
37
- directProxy = flag .String ("direct_proxy" , "" , "if set to a valid URL, uses the module proxy referred to by this URL " +
40
+ proxyURL = flag .String ("proxy_url" , "https://proxy.golang.org" , "Uses the module proxy referred to by this URL " +
41
+ "for direct proxy mode and frontend fetches" )
42
+ directProxy = flag .Bool ("direct_proxy" , false , "if set to true, uses the module proxy referred to by this URL " +
38
43
"as a direct backend, bypassing the database" )
39
44
)
40
45
41
46
func main () {
42
47
flag .Parse ()
43
-
44
48
ctx := context .Background ()
45
-
46
49
cfg , err := config .Init (ctx )
47
50
if err != nil {
48
51
log .Fatal (ctx , err )
49
52
}
50
53
cfg .Dump (os .Stderr )
51
-
52
54
if cfg .UseProfiler {
53
55
if err := profiler .Start (profiler.Config {}); err != nil {
54
56
log .Fatalf (ctx , "profiler.Start: %v" , err )
55
57
}
56
58
}
57
-
58
59
var (
59
- ds internal.DataSource
60
- exp internal.ExperimentSource
60
+ ds internal.DataSource
61
+ exp internal.ExperimentSource
62
+ fetchQueue queue.Queue
61
63
)
62
- if * directProxy != "" {
63
- proxyClient , err := proxy . New ( * directProxy )
64
- if err != nil {
65
- log . Fatal ( ctx , err )
66
- }
64
+ proxyClient , err := proxy . New ( * proxyURL )
65
+ if err != nil {
66
+ log . Fatal ( ctx , err )
67
+ }
68
+ if * directProxy {
67
69
ds = proxydatasource .New (proxyClient )
68
70
exp = internal .NewLocalExperimentSource (readLocalExperiments (ctx ))
69
71
} else {
@@ -80,14 +82,16 @@ func main() {
80
82
defer db .Close ()
81
83
ds = db
82
84
exp = db
85
+ sourceClient := source .NewClient (config .SourceTimeout )
86
+ fetchQueue = newQueue (ctx , cfg , proxyClient , sourceClient , db )
83
87
}
84
88
var haClient * redis.Client
85
89
if cfg .RedisHAHost != "" {
86
90
haClient = redis .NewClient (& redis.Options {
87
91
Addr : cfg .RedisHAHost + ":" + cfg .RedisHAPort ,
88
92
})
89
93
}
90
- server , err := frontend .NewServer (ds , haClient , * staticPath , * thirdPartyPath , * reloadTemplates )
94
+ server , err := frontend .NewServer (ds , fetchQueue , haClient , * staticPath , * thirdPartyPath , * reloadTemplates )
91
95
if err != nil {
92
96
log .Fatalf (ctx , "frontend.NewServer: %v" , err )
93
97
}
@@ -99,7 +103,6 @@ func main() {
99
103
})
100
104
}
101
105
server .Install (router .Handle , cacheClient )
102
-
103
106
views := append (dcensus .ServerViews ,
104
107
postgres .SearchLatencyDistribution ,
105
108
postgres .SearchResponseCount ,
@@ -119,7 +122,6 @@ func main() {
119
122
}
120
123
go http .ListenAndServe (cfg .DebugAddr ("localhost:8081" ), dcensusServer )
121
124
}
122
-
123
125
panicHandler , err := server .PanicHandler ()
124
126
if err != nil {
125
127
log .Fatal (ctx , err )
@@ -129,7 +131,6 @@ func main() {
129
131
if err != nil {
130
132
log .Fatal (ctx , err )
131
133
}
132
-
133
134
mw := middleware .Chain (
134
135
middleware .RequestLog (requestLogger ),
135
136
middleware .Quota (cfg .Quota ),
@@ -140,19 +141,31 @@ func main() {
140
141
middleware .Timeout (54 * time .Second ),
141
142
middleware .Experiment (experimenter ),
142
143
)
143
-
144
144
addr := cfg .HostAddr ("localhost:8080" )
145
145
log .Infof (ctx , "Listening on addr %s" , addr )
146
146
log .Fatal (ctx , http .ListenAndServe (addr , mw (router )))
147
147
}
148
148
149
+ func newQueue (ctx context.Context , cfg * config.Config , proxyClient * proxy.Client , sourceClient * source.Client , db * postgres.DB ) queue.Queue {
150
+ if ! cfg .OnAppEngine () {
151
+ return queue .NewInMemory (ctx , proxyClient , sourceClient , db , 10 , frontend .FetchAndUpdateState )
152
+ }
153
+ client , err := cloudtasks .NewClient (ctx )
154
+ if err != nil {
155
+ log .Fatal (ctx , err )
156
+ }
157
+ if queueName == "" {
158
+ log .Fatalf (ctx , "queueName cannot be empty" )
159
+ }
160
+ return queue .NewGCP (cfg , client , queueName )
161
+ }
162
+
149
163
// openDB opens a connection to a database with the given driver, using connection info from
150
164
// the given config.
151
165
// It first tries the main connection info (DBConnInfo), and if that fails, it uses backup
152
166
// connection info it if exists (DBSecondaryConnInfo).
153
167
func openDB (ctx context.Context , cfg * config.Config , driver string ) (_ * database.DB , err error ) {
154
168
derrors .Wrap (& err , "openDB(ctx, cfg, %q)" , driver )
155
-
156
169
log .Infof (ctx , "opening database on host %s" , cfg .DBHost )
157
170
ddb , err := database .Open (driver , cfg .DBConnInfo ())
158
171
if err == nil {
@@ -167,7 +180,6 @@ func openDB(ctx context.Context, cfg *config.Config, driver string) (_ *database
167
180
cfg .DBHost , err , cfg .DBSecondaryHost )
168
181
return database .Open (driver , ci )
169
182
}
170
-
171
183
func getLogger (ctx context.Context , cfg * config.Config ) middleware.Logger {
172
184
if cfg .OnAppEngine () {
173
185
logger , err := log .UseStackdriver (ctx , cfg , "frontend-log" )
0 commit comments