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

View File

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

View File

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