feat: update init

This commit is contained in:
Kevin Franklin Kim 2024-03-22 15:48:01 +01:00
parent abb6f692c7
commit d9a8248c0a
No known key found for this signature in database
3 changed files with 16 additions and 15 deletions

View File

@ -43,8 +43,6 @@ func NewHTTPCommand() *cobra.Command {
l := svr.Logger()
l.Error("test")
r := repo.New(l,
args[0],
repo.NewHistory(l,
@ -69,15 +67,15 @@ func NewHTTPCommand() *cobra.Command {
}))
svr.AddServices(
service.NewGoRoutine(l, "repo", func(ctx context.Context, l *zap.Logger) error {
return r.Start(ctx)
}),
service.NewHTTP(l, "http", addressFlag(v),
handler.NewHTTP(l, r, handler.WithBasePath(basePathFlag(v))),
middleware.Telemetry(),
middleware.Logger(),
middleware.Recover(),
),
service.NewGoRoutine(l, "repo", func(ctx context.Context, l *zap.Logger) error {
return r.Start(ctx)
}),
)
svr.Run()

View File

@ -18,8 +18,8 @@ import (
type (
HTTP struct {
l *zap.Logger
basePath string
repo *repo.Repo
basePath string
}
HTTPOption func(*HTTP)
)
@ -92,22 +92,22 @@ func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// ~ Private methods
// ------------------------------------------------------------------------------------------------
func (h *HTTP) handleRequest(r *repo.Repo, handler Route, jsonBytes []byte, source string) ([]byte, error) {
func (h *HTTP) handleRequest(r *repo.Repo, route Route, jsonBytes []byte, source string) ([]byte, error) {
start := time.Now()
reply, err := h.executeRequest(r, handler, jsonBytes, source)
reply, err := h.executeRequest(r, route, jsonBytes, source)
result := "success"
if err != nil {
result = "error"
}
metrics.ServiceRequestCounter.WithLabelValues(string(handler), result, source).Inc()
metrics.ServiceRequestDuration.WithLabelValues(string(handler), result, source).Observe(time.Since(start).Seconds())
metrics.ServiceRequestCounter.WithLabelValues(string(route), result, source).Inc()
metrics.ServiceRequestDuration.WithLabelValues(string(route), result, source).Observe(time.Since(start).Seconds())
return reply, err
}
func (h *HTTP) executeRequest(r *repo.Repo, handler Route, jsonBytes []byte, source string) (replyBytes []byte, err error) {
func (h *HTTP) executeRequest(r *repo.Repo, route Route, jsonBytes []byte, source string) (replyBytes []byte, err error) {
var (
reply interface{}
apiErr error
@ -123,7 +123,7 @@ func (h *HTTP) executeRequest(r *repo.Repo, handler Route, jsonBytes []byte, sou
metrics.ContentRequestCounter.WithLabelValues(source).Inc()
// handle and process
switch handler {
switch route {
// case HandlerGetRepo: // This case is handled prior to handleRequest being called.
// since the resulting bytes are written directly in to the http.ResponseWriter / net.Connection
case RouteGetURIs:
@ -147,7 +147,7 @@ func (h *HTTP) executeRequest(r *repo.Repo, handler Route, jsonBytes []byte, sou
reply = r.Update()
})
default:
reply = responses.NewError(1, "unknown handler: "+string(handler))
reply = responses.NewError(1, "unknown route: "+string(route))
}
// error handling

View File

@ -282,7 +282,6 @@ func (r *Repo) Update() (updateResponse *responses.Update) {
updateResponse.Stats.NumberOfNodes += len(dimension.Directory)
updateResponse.Stats.NumberOfURIs += len(dimension.URIDirectory)
}
r.loaded.Store(true)
}
updateResponse.Stats.OwnRuntime = floatSeconds(time.Since(start).Nanoseconds()) - updateResponse.Stats.RepoRuntime
return updateResponse
@ -325,7 +324,9 @@ func (r *Repo) Start(ctx context.Context) error {
l.Debug("starting poll routine")
return r.PollRoutine(gCtx)
})
} else if !r.Loaded() {
}
if !r.Loaded() {
l.Debug("trying to update initial state")
if resp := r.Update(); !resp.Success {
l.Error("failed to update initial state",
@ -335,6 +336,8 @@ func (r *Repo) Start(ctx context.Context) error {
zap.Float64("own_runtime", resp.Stats.OwnRuntime),
zap.Float64("repo_runtime", resp.Stats.RepoRuntime),
)
} else {
r.loaded.Store(true)
}
}