Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
gddo-server: centralize config and add log package
Browse files Browse the repository at this point in the history
This change starts centralizing the configuration for godoc.org. In the
future all configuration, including an environment variable and metadata
server check will go through viper.

This change also adds the new logging library I am writing. In the future
all logging will go through this library.

Change-Id: Id0bed0f3a4dc9acb109545db89d6221a182c37fe
Reviewed-on: https://go-review.googlesource.com/34618
Reviewed-by: Tuo Shan <[email protected]>
  • Loading branch information
stephenmw authored and shantuo committed Mar 13, 2017
1 parent 4544b88 commit 72302b9
Show file tree
Hide file tree
Showing 417 changed files with 162,341 additions and 75 deletions.
123 changes: 123 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 22 additions & 20 deletions gddo-server/background.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2017 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
Expand All @@ -7,52 +7,54 @@
package main

import (
"flag"
"log"
"time"

"github.com/spf13/viper"
"google.golang.org/appengine"

"github.com/golang/gddo/database"
"github.com/golang/gddo/gosrc"
)

var backgroundTasks = []*struct {
type BackgroundTask struct {
name string
fn func() error
interval *time.Duration
interval time.Duration
next time.Time
}{
{
name: "GitHub updates",
fn: readGitHubUpdates,
interval: flag.Duration("github_interval", 0, "Github updates crawler sleeps for this duration between fetches. Zero disables the crawler."),
},
{
name: "Crawl",
fn: doCrawl,
interval: flag.Duration("crawl_interval", 0, "Package updater sleeps for this duration between package updates. Zero disables updates."),
},
}

func runBackgroundTasks() {
defer log.Println("ERROR: Background exiting!")

var backgroundTasks = []BackgroundTask{
{
name: "GitHub updates",
fn: readGitHubUpdates,
interval: viper.GetDuration(ConfigGithubInterval),
},
{
name: "Crawl",
fn: doCrawl,
interval: viper.GetDuration(ConfigCrawlInterval),
},
}

sleep := time.Minute
for _, task := range backgroundTasks {
if *task.interval > 0 && sleep > *task.interval {
sleep = *task.interval
if task.interval > 0 && sleep > task.interval {
sleep = task.interval
}
}

for {
for _, task := range backgroundTasks {
start := time.Now()
if *task.interval > 0 && start.After(task.next) {
if task.interval > 0 && start.After(task.next) {
if err := task.fn(); err != nil {
log.Printf("Task %s: %v", task.name, err)
}
task.next = time.Now().Add(*task.interval)
task.next = time.Now().Add(task.interval)
}
}
time.Sleep(sleep)
Expand Down Expand Up @@ -86,7 +88,7 @@ func doCrawl() error {
}
if _, err = crawlDoc("crawl", pdoc.ImportPath, pdoc, len(pkgs) > 0, nextCrawl); err != nil {
// Touch package so that crawl advances to next package.
if err := db.SetNextCrawl(pdoc.ImportPath, time.Now().Add(*maxAge/3)); err != nil {
if err := db.SetNextCrawl(pdoc.ImportPath, time.Now().Add(viper.GetDuration(ConfigMaxAge)/3)); err != nil {
log.Printf("ERROR db.SetNextCrawl(%q): %v", pdoc.ImportPath, err)
}
}
Expand Down
22 changes: 9 additions & 13 deletions gddo-server/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2017 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
Expand All @@ -10,39 +10,35 @@
package main

import (
"flag"
"fmt"
"net"
"net/http"
"os"
"time"

"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/memcache"
"github.com/spf13/viper"

"github.com/golang/gddo/httputil"
)

var (
dialTimeout = flag.Duration("dial_timeout", 5*time.Second, "Timeout for dialing an HTTP connection.")
requestTimeout = flag.Duration("request_timeout", 20*time.Second, "Time out for roundtripping an HTTP request.")
)

func newHTTPClient() *http.Client {
t := newCacheTransport()

requestTimeout := viper.GetDuration(ConfigRequestTimeout)
t.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: *dialTimeout,
KeepAlive: *requestTimeout / 2,
Timeout: viper.GetDuration(ConfigDialTimeout),
KeepAlive: requestTimeout / 2,
}).Dial,
ResponseHeaderTimeout: *requestTimeout / 2,
TLSHandshakeTimeout: *requestTimeout / 2,
ResponseHeaderTimeout: requestTimeout / 2,
TLSHandshakeTimeout: requestTimeout / 2,
}
return &http.Client{
// Wrap the cached transport with GitHub authentication.
Transport: httputil.NewAuthTransport(t),
Timeout: *requestTimeout,
Timeout: requestTimeout,
}
}

Expand Down
Loading

0 comments on commit 72302b9

Please sign in to comment.