Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/parsec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func main() {
sigs := make(chan os.Signal, 1)
ctx, cancel := context.WithCancel(context.Background())

signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
go func() {
sig := <-sigs
log.Printf("Received %s signal - Stopping...\n", sig.String())
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ build:
# Check formatting, go fix, vet, and staticcheck without changing files
lint:
@test -z "$(gofumpt -l .)" || { echo "not formatted, run just format:"; gofumpt -l .; exit 1; }
go fix -diff ./...
@test -z "$(go fix -diff ./... 2>/dev/null)" || { go fix -diff ./... 2>/dev/null; echo "go fix changes pending, run just format"; exit 1; }
go vet ./...
staticcheck ./...

Expand Down
2 changes: 1 addition & 1 deletion pkg/db/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *PGClient) applyMigrations() error {
}()
log.WithField("dir", tmpDir).Debugln("Created temporary directory")

err = fs.WalkDir(migrations, ".", func(path string, d fs.DirEntry, err error) error {
err = fs.WalkDir(migrations, ".", func(path string, d fs.DirEntry, _ error) error {
join := filepath.Join(tmpDir, path)
if d.IsDir() {
return os.MkdirAll(join, 0o755)
Expand Down
13 changes: 2 additions & 11 deletions pkg/server/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type HostConfig struct {

type Host struct {
host.Host
conf *HostConfig
fhClient firehose.Submitter
idService identify.IDService

Expand Down Expand Up @@ -274,7 +273,7 @@ var codecs = []multicodec.Code{
}

func (h *Host) subscribeForEvents() error {
sub, err := h.EventBus().Subscribe([]interface{}{new(event.EvtLocalAddressesUpdated), new(event.EvtLocalReachabilityChanged)})
sub, err := h.EventBus().Subscribe([]any{new(event.EvtLocalAddressesUpdated), new(event.EvtLocalReachabilityChanged)})
if err != nil {
return fmt.Errorf("event bus subscription: %w", err)
}
Expand Down Expand Up @@ -341,7 +340,7 @@ func RoutingTableSize(dht routing.Routing) int {
func genProbes(start mh.Multihash, count int) ([]mh.Multihash, error) {
probes := make([]mh.Multihash, count)
hash := start
for i := 0; i < count; i++ {
for i := range count {
probe, err := mh.Sum(hash, mh.SHA2_256, -1)
if err != nil {
return nil, fmt.Errorf("gen probe digest: %w", err)
Expand All @@ -361,14 +360,6 @@ func fmtContextID(contextID []byte) string {
return str
}

func fmtMultihash(m mh.Multihash) string {
str := m.B58String()
if len(str) >= 16 {
return str[:16]
}
return str
}

func (h *Host) getPublicIP(ctx context.Context) error {
log.Infoln("Fetching public IP from AWS checkip service...")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://checkip.amazonaws.com", nil)
Expand Down
6 changes: 6 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ type IServer interface {

const headerSchedulerID = "x-scheduler-id"

// ctxKey is the type of context keys set by this package.
type ctxKey string

// schedulerIDKey holds the scheduler ID in a request context.
const schedulerIDKey ctxKey = headerSchedulerID

type Config struct {
Host string
Port int
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/server_dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (d *DHTServer) Provide(ctx context.Context, cid cid.Cid) (*ProvideResponse,
err := d.DHT.Provide(timeoutCtx, cid, true)
duration := time.Since(start)

latencies.WithLabelValues("provide_duration", strconv.FormatBool(err == nil), ctx.Value(headerSchedulerID).(string)).Observe(duration.Seconds())
latencies.WithLabelValues("provide_duration", strconv.FormatBool(err == nil), ctx.Value(schedulerIDKey).(string)).Observe(duration.Seconds())
logEntry := log.WithField("dur", duration.Seconds()).WithField("cid", cid.String())
if err != nil {
logEntry = logEntry.WithError(err)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (d *DHTServer) Retrieve(ctx context.Context, cid cid.Cid) (*RetrievalRespon
RoutingTableSize: RoutingTableSize(d.DHT),
}

latencies.WithLabelValues("retrieval_ttfpr", strconv.FormatBool(err == nil), ctx.Value(headerSchedulerID).(string)).Observe(duration.Seconds())
latencies.WithLabelValues("retrieval_ttfpr", strconv.FormatBool(err == nil), ctx.Value(schedulerIDKey).(string)).Observe(duration.Seconds())
return resp, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/server/server_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Server) provide(rw http.ResponseWriter, req *http.Request) {

log.WithField("cid", content.CID.String()).Infoln("Start providing content...")

ctx := context.WithValue(req.Context(), headerSchedulerID, req.Header.Get(headerSchedulerID))
ctx := context.WithValue(req.Context(), schedulerIDKey, req.Header.Get(headerSchedulerID))
resp, err := s.serverImpl.Provide(ctx, content.CID)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
Expand All @@ -66,7 +66,7 @@ func (s *Server) provide(rw http.ResponseWriter, req *http.Request) {
type RetrieveRequest struct{}

func (s *Server) retrieve(rw http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), headerSchedulerID, req.Header.Get(headerSchedulerID))
ctx := context.WithValue(req.Context(), schedulerIDKey, req.Header.Get(headerSchedulerID))
var rr RetrieveRequest
data, err := io.ReadAll(req.Body)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/server_ipni.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (i *IPNIServer) Provide(ctx context.Context, c cid.Cid) (pr *ProvideRespons
return
}

latencies.WithLabelValues("provide_duration", strconv.FormatBool(m.Error == ""), ctx.Value(headerSchedulerID).(string)).Observe(m.Duration.Seconds())
latencies.WithLabelValues("provide_duration", strconv.FormatBool(m.Error == ""), ctx.Value(schedulerIDKey).(string)).Observe(m.Duration.Seconds())
}()

// Generating multihash for probing after we have pushed out the advertisement.
Expand All @@ -206,7 +206,7 @@ func (i *IPNIServer) Provide(ctx context.Context, c cid.Cid) (pr *ProvideRespons
logEntry.Infoln(" Probes:", probeCount)

// The contextID just needs to be unique per multihash
contextID := []byte(fmt.Sprintf("parsec-%d", rand.Uint64()))
contextID := fmt.Appendf(nil, "parsec-%d", rand.Uint64())
logEntry.Infoln(" CtxID:", base64.StdEncoding.EncodeToString(contextID))
logEntry = logEntry.WithField("ctxID", fmtContextID(contextID))

Expand Down Expand Up @@ -425,7 +425,7 @@ func (i *IPNIServer) Retrieve(ctx context.Context, cid cid.Cid) (*RetrievalRespo
RoutingTableSize: 0,
}

latencies.WithLabelValues("retrieval_ttfpr", strconv.FormatBool(err == nil), ctx.Value(headerSchedulerID).(string)).Observe(duration.Seconds())
latencies.WithLabelValues("retrieval_ttfpr", strconv.FormatBool(err == nil), ctx.Value(schedulerIDKey).(string)).Observe(duration.Seconds())
return retResp, nil
}

Expand Down
11 changes: 4 additions & 7 deletions staticcheck.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Existing findings, left for follow-up changes so that `just lint` passes:
# SA1016 os.Kill in signal.Notify (cmd/parsec/cmd.go)
# SA1019 deprecated aws-sdk-go v1 and go-ipfs-util
# SA1029 string context keys (pkg/server/server_handlers.go)
# SA4009 overwritten err argument (pkg/db/client.go)
# U1000 unused field conf and func fmtMultihash (pkg/server/host.go)
checks = ["inherit", "-SA1016", "-SA1019", "-SA1029", "-SA4009", "-U1000"]
# SA1019 stays ignored: aws-sdk-go v1 (pkg/firehose) and go-ipfs-util
# (pkg/util) are deprecated. Moving to aws-sdk-go v2 is a larger change,
# so it is tracked separately. Remove this file once it is done.
checks = ["inherit", "-SA1019"]
Loading