-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch remote configuration every 10 minutes
Gobrake needs to be able to fetch remote configuration at certain intervals. This commit prepares gobrake for the change. A newly created notifier will start polling S3 for the config immediately. The config values are not used at the moment, we just make reads and discard the config to keep the change as simple as possible.
- Loading branch information
Showing
6 changed files
with
233 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package gobrake | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type remoteConfig struct { | ||
opt *NotifierOptions | ||
ticker *time.Ticker | ||
} | ||
|
||
func newRemoteConfig(opt *NotifierOptions) *remoteConfig { | ||
return &remoteConfig{ | ||
opt: opt, | ||
} | ||
} | ||
|
||
func (rc *remoteConfig) Poll() { | ||
err := rc.fetchConfig() | ||
if err != nil { | ||
logger.Printf(fmt.Sprintf("fetchConfig failed: %s", err)) | ||
} | ||
|
||
rc.ticker = time.NewTicker(10 * time.Minute) | ||
go func() { | ||
for { | ||
<-rc.ticker.C | ||
err := rc.fetchConfig() | ||
if err != nil { | ||
logger.Printf(fmt.Sprintf("fetchConfig failed: %s", err)) | ||
continue | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (rc *remoteConfig) StopPolling() { | ||
rc.ticker.Stop() | ||
} | ||
|
||
func (rc *remoteConfig) fetchConfig() error { | ||
url := fmt.Sprintf("%s/2020-06-18/config/%d/config.json", | ||
rc.opt.RemoteConfigHost, rc.opt.ProjectId) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch resp.StatusCode { | ||
case http.StatusForbidden, http.StatusNotFound: | ||
return errors.New(string(body)) | ||
case http.StatusOK: | ||
return nil | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package gobrake | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("newRemoteConfig", func() { | ||
var rc *remoteConfig | ||
var opt *NotifierOptions | ||
var origLogger *log.Logger | ||
var logBuf *bytes.Buffer | ||
|
||
BeforeEach(func() { | ||
opt = &NotifierOptions{ | ||
ProjectId: 1, | ||
ProjectKey: "key", | ||
} | ||
|
||
origLogger = GetLogger() | ||
logBuf = new(bytes.Buffer) | ||
SetLogger(log.New(logBuf, "", 0)) | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
rc = newRemoteConfig(opt) | ||
}) | ||
|
||
AfterEach(func() { | ||
SetLogger(origLogger) | ||
rc.StopPolling() | ||
}) | ||
|
||
Describe("Poll", func() { | ||
Context("when the server returns 404", func() { | ||
BeforeEach(func() { | ||
handler := func(w http.ResponseWriter, req *http.Request) { | ||
w.WriteHeader(http.StatusNotFound) | ||
_, err := w.Write([]byte("not found")) | ||
Expect(err).To(BeNil()) | ||
} | ||
server := httptest.NewServer(http.HandlerFunc(handler)) | ||
|
||
opt.RemoteConfigHost = server.URL | ||
}) | ||
|
||
It("logs the error", func() { | ||
rc.Poll() | ||
Expect(logBuf.String()).To( | ||
ContainSubstring("fetchConfig failed: not found"), | ||
) | ||
}) | ||
}) | ||
|
||
Context("when the server returns 403", func() { | ||
BeforeEach(func() { | ||
handler := func(w http.ResponseWriter, req *http.Request) { | ||
w.WriteHeader(http.StatusForbidden) | ||
_, err := w.Write([]byte("forbidden")) | ||
Expect(err).To(BeNil()) | ||
} | ||
server := httptest.NewServer(http.HandlerFunc(handler)) | ||
|
||
opt.RemoteConfigHost = server.URL | ||
}) | ||
|
||
It("logs the error", func() { | ||
rc.Poll() | ||
Expect(logBuf.String()).To( | ||
ContainSubstring("fetchConfig failed: forbidden"), | ||
) | ||
}) | ||
}) | ||
|
||
Context("when the server returns 200", func() { | ||
BeforeEach(func() { | ||
handler := func(w http.ResponseWriter, req *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte("{}")) | ||
Expect(err).To(BeNil()) | ||
} | ||
server := httptest.NewServer(http.HandlerFunc(handler)) | ||
|
||
opt.RemoteConfigHost = server.URL | ||
}) | ||
|
||
It("doesn't log any errors", func() { | ||
rc.Poll() | ||
Expect(logBuf.String()).To(BeEmpty()) | ||
}) | ||
}) | ||
}) | ||
}) |