From 0a4c03546f83258bee4127c1e08994430b961838 Mon Sep 17 00:00:00 2001 From: Sune Kirkeby Date: Tue, 7 Jun 2022 12:33:37 +0200 Subject: [PATCH] Fix golangci-lint errors. (#23) * Fix CI-errors. * Use SHA-hashes for non-official actions. * Update actions. --- .github/workflows/ci.yaml | 14 +++++++------- api/aws.go | 2 -- api/backup_test.go | 4 ++-- api/health.go | 31 ++++++++++++++----------------- api/lists.go | 6 ++---- api/main_test.go | 8 +++----- api/multilist.go | 2 +- api/multilist_test.go | 2 +- api/newrelic.go | 26 +++++++++++++------------- api/rankdb.go | 1 - api/services.go | 2 +- api/shutdown.go | 3 +-- api/tool/rankdb-cli/main.go | 2 +- backup/job.go | 1 - backup/s3/s3.go | 2 +- backup/server/rankdb.go | 2 +- blobstore/badgerstore/badger.go | 2 +- blobstore/bstest/bstest.go | 2 ++ blobstore/lazysaver.go | 3 ++- cmd/rankdb/main.go | 4 ++-- elements.go | 17 ----------------- elements_test.go | 1 + list-id.go | 1 - list.go | 14 ++++++++------ list_test.go | 4 ++++ lists_test.go | 1 + msgppool.go | 1 - ranksdb_test.go | 2 +- segment.go | 2 +- segments.go | 23 ++++++++--------------- sortfloat/float_test.go | 2 +- tools.go | 9 ++------- 32 files changed, 82 insertions(+), 114 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 821aa72..4f2b329 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,18 +16,18 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - name: golangci-lint - # TODO: fix golangci-lint errors and warnings, and remove continue-on-error - continue-on-error: true - uses: golangci/golangci-lint-action@v2 + # Note: this is @v3.2.0. + uses: golangci/golangci-lint-action@537aa1903e5d359d0b27dbc19ddd22c5087f3fbc with: version: v1.43 + args: --timeout=5m - name: Tests run: | @@ -41,12 +41,12 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: go-version: 1.16.x - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: goreleaser deprecation run: curl -sfL https://git.io/goreleaser | VERSION=v1.2.5 sh -s -- check diff --git a/api/aws.go b/api/aws.go index 2cb63cb..3fd34b6 100644 --- a/api/aws.go +++ b/api/aws.go @@ -17,7 +17,6 @@ import ( var ( awsSession *session.Session - awsCreds *credentials.Credentials ) func initAws(ctx context.Context) { @@ -46,7 +45,6 @@ func initAws(ctx context.Context) { log.Error(ctx, "AWS not initialized", "error", err.Error()) return } - awsCreds = creds cfg := aws.NewConfig().WithRegion(config.AWS.Region).WithCredentials(creds) if config.AWS.S3Endpoint != "" { cfg = cfg.WithEndpoint(config.AWS.S3Endpoint) diff --git a/api/backup_test.go b/api/backup_test.go index 5d428c2..28030e6 100644 --- a/api/backup_test.go +++ b/api/backup_test.go @@ -26,7 +26,7 @@ func TestBackupController_Status(t *testing.T) { } createList(t, t.Name(), elems) createList(t, t.Name()+"-2", elems) - dir, err := ioutil.TempDir("", t.Name()) + dir, _ := ioutil.TempDir("", t.Name()) fileName := filepath.Join(dir, t.Name()+".bin") payload := client.MultiListBackup{ Destination: &client.BackupDestination{ @@ -107,7 +107,7 @@ func TestBackupController_Delete(t *testing.T) { } createList(t, t.Name(), elems) createList(t, t.Name()+"-2", elems) - dir, err := ioutil.TempDir("", t.Name()) + dir, _ := ioutil.TempDir("", t.Name()) fileName := filepath.Join(dir, t.Name()+".bin") payload := client.MultiListBackup{ Destination: &client.BackupDestination{ diff --git a/api/health.go b/api/health.go index 4fc87f8..6f30be3 100644 --- a/api/health.go +++ b/api/health.go @@ -63,24 +63,21 @@ func (c *HealthController) Health(ctx *app.HealthHealthContext) error { func (c *HealthController) memoryReader() { ctx := c.Context ticker := time.Tick(time.Minute) - for { - select { - case <-ticker: - var mem runtime.MemStats - runtime.ReadMemStats(&mem) - j, err := json.MarshalIndent(mem, "", "\t") - if err != nil { - log.Error(ctx, "unable to marshal memstats", "error", err.Error()) - } - var m map[string]interface{} - err = json.Unmarshal(j, &m) - if err != nil { - log.Error(ctx, "unable to unmarshal memstats", "error", err.Error()) - } - c.memoryMuStats.Lock() - c.memoryStats = m - c.memoryMuStats.Unlock() + for range ticker { + var mem runtime.MemStats + runtime.ReadMemStats(&mem) + j, err := json.MarshalIndent(mem, "", "\t") + if err != nil { + log.Error(ctx, "unable to marshal memstats", "error", err.Error()) } + var m map[string]interface{} + err = json.Unmarshal(j, &m) + if err != nil { + log.Error(ctx, "unable to unmarshal memstats", "error", err.Error()) + } + c.memoryMuStats.Lock() + c.memoryStats = m + c.memoryMuStats.Unlock() } } diff --git a/api/lists.go b/api/lists.go index 2aba814..438daf9 100644 --- a/api/lists.go +++ b/api/lists.go @@ -130,10 +130,8 @@ func (c *ListsController) GetPercentile(ctx *app.GetPercentileListsContext) erro if !ok { return ctx.NotFound(goa.ErrNotFound("not_found", "list_id", ctx.ListID)) } - percentile := float64(50.0) - if fromTop, err2 := strconv.ParseFloat(ctx.FromTop, 64); err2 == nil { - percentile = fromTop - } else { + percentile, err := strconv.ParseFloat(ctx.FromTop, 64) + if err != nil { return ctx.BadRequest(goa.InvalidParamTypeError("from_top", ctx.FromTop, "number")) } if percentile < 0 { diff --git a/api/main_test.go b/api/main_test.go index 97c69c2..ecf639c 100644 --- a/api/main_test.go +++ b/api/main_test.go @@ -39,9 +39,6 @@ const ( // ContentTypeMsgpack specifies a messagepack encoding. // Compact and fast. contentMsgpack = "application/msgpack" - - // ContentTypeJSON forces JSON as transport protocol. - contentJSON = "application/json" ) var ( @@ -52,6 +49,7 @@ var ( tClientManage *client.Client ) +//nolint:staticcheck func TestMain(m *testing.M) { lr := logrus.New() lr.Formatter = &logrus.TextFormatter{DisableColors: true} @@ -65,7 +63,7 @@ func TestMain(m *testing.M) { err = conf.Close() exitOnFailure(err) enableJWTCreation = true - go StartServices(logger, ctx, err) + go StartServices(logger, ctx) <-listening // Initialize clients @@ -107,7 +105,7 @@ func initClient(ctx context.Context, dstClient **client.Client, scope string) { "scopes": scope, // token scope - not a standard claim } token.Claims = claims - signedToken, err := token.SignedString(privKey) + signedToken, _ := token.SignedString(privKey) cl.JWTSigner = &goaclient.JWTSigner{ TokenSource: &goaclient.StaticTokenSource{ diff --git a/api/multilist.go b/api/multilist.go index 7c8091b..3c32cad 100644 --- a/api/multilist.go +++ b/api/multilist.go @@ -472,7 +472,7 @@ func onEveryList(ctx context.Context, lists rankdb.ListIDs, limit int, fn func(l var mu sync.Mutex var results = app.RankdbResultlist{ Success: make(map[string]*app.RankdbOperationSuccess, len(lists)), - Errors: make(map[string]string, 0), + Errors: make(map[string]string), } var tokens = make(chan struct{}, limit) for i := 0; i < limit; i++ { diff --git a/api/multilist_test.go b/api/multilist_test.go index 1ec530c..862adbb 100644 --- a/api/multilist_test.go +++ b/api/multilist_test.go @@ -286,7 +286,7 @@ func TestMultilistController_Backup(t *testing.T) { } createList(t, t.Name(), elems) createList(t, t.Name()+"-2", elems) - dir, err := ioutil.TempDir("", t.Name()) + dir, _ := ioutil.TempDir("", t.Name()) fileName := filepath.Join(dir, t.Name()+".bin") payload := client.MultiListBackup{ Destination: &client.BackupDestination{ diff --git a/api/newrelic.go b/api/newrelic.go index 143447b..3440bf0 100644 --- a/api/newrelic.go +++ b/api/newrelic.go @@ -95,16 +95,16 @@ func NewRelicTx() goa.Middleware { app := nrApp.app txn := app.StartTransaction(goa.ContextController(ctx)+"."+goa.ContextAction(ctx), rw, req) r := goa.ContextRequest(ctx) - txn.AddAttribute("source_ip", from(req)) + _ = txn.AddAttribute("source_ip", from(req)) if config.Debug && len(r.Header) > 0 { for k, v := range r.Header { - txn.AddAttribute("header_"+k, `"`+strings.Join(v, `","`)+`"`) + _ = txn.AddAttribute("header_"+k, `"`+strings.Join(v, `","`)+`"`) } } if len(r.Params) > 0 { for k, v := range r.Params { - txn.AddAttribute("param_"+k, `"`+strings.Join(v, `","`)+`"`) + _ = txn.AddAttribute("param_"+k, `"`+strings.Join(v, `","`)+`"`) } } if config.Debug && r.ContentLength > 0 && r.ContentLength < 1024 { @@ -113,10 +113,10 @@ func NewRelicTx() goa.Middleware { if err != nil { js = []byte("") } - txn.AddAttribute("payload_json", string(js)) + _ = txn.AddAttribute("payload_json", string(js)) } ierr := func(msg string, keyvals ...interface{}) { - txn.NoticeError(errors.New(msg)) + _ = txn.NoticeError(errors.New(msg)) for len(keyvals) > 0 { key := fmt.Sprint(keyvals[0]) keyvals = keyvals[1:] @@ -125,7 +125,7 @@ func NewRelicTx() goa.Middleware { val = fmt.Sprint(keyvals[0]) keyvals = keyvals[1:] } - txn.AddAttribute(key, val) + _ = txn.AddAttribute(key, val) } } var ninfo int @@ -136,7 +136,7 @@ func NewRelicTx() goa.Middleware { ninfo++ mu.Unlock() if n < 10 { - txn.AddAttribute(fmt.Sprintf("info_msg_%d", n), formatMsg(msg, keyvals, false)) + _ = txn.AddAttribute(fmt.Sprintf("info_msg_%d", n), formatMsg(msg, keyvals, false)) } } intLogger := log.Intercept(log.Logger(ctx), iinfo, ierr) @@ -146,18 +146,18 @@ func NewRelicTx() goa.Middleware { defer func() { resp := goa.ContextResponse(ctx) if code := resp.ErrorCode; code != "" { - txn.AddAttribute("error_code", code) - txn.NoticeError(err) + _ = txn.AddAttribute("error_code", code) + _ = txn.NoticeError(err) } else if resp.Status >= 500 || err != nil { err2 := err if err == nil { err2 = errors.New(resp.ErrorCode) } - txn.NoticeError(err2) + _ = txn.NoticeError(err2) } - txn.AddAttribute("response_status_code", resp.Status) - txn.AddAttribute("response_bytes", resp.Length) - txn.End() + _ = txn.AddAttribute("response_status_code", resp.Status) + _ = txn.AddAttribute("response_bytes", resp.Length) + _ = txn.End() }() return err } diff --git a/api/rankdb.go b/api/rankdb.go index 5e41807..08ec89b 100644 --- a/api/rankdb.go +++ b/api/rankdb.go @@ -232,7 +232,6 @@ func StartServer(ctx context.Context, confData io.Reader, lr *logrus.Logger) err updateBucket = rankdb.NewBucket(config.MaxUpdates) } if config.CacheEntries > 0 { - err = nil switch config.CacheType { case "", "ARC": cache, err = lru.NewARC(config.CacheEntries) diff --git a/api/services.go b/api/services.go index a253631..a927b1d 100644 --- a/api/services.go +++ b/api/services.go @@ -29,7 +29,7 @@ var ( listenAddr net.Addr ) -func StartServices(logger goa.LogAdapter, ctx context.Context, err error) { +func StartServices(logger goa.LogAdapter, ctx context.Context) { shutdown.PreShutdownFn(func() { close(shutdownStarted) }) diff --git a/api/shutdown.go b/api/shutdown.go index 3a9f973..a155d64 100644 --- a/api/shutdown.go +++ b/api/shutdown.go @@ -18,7 +18,7 @@ import ( var ( errShutdown = goa.NewErrorClass("server_restarting", http.StatusServiceUnavailable)("Server restarting") errCancelled = goa.NewErrorClass("request_cancelled", 499)("Request Cancelled") - shutdownStarted = make(chan struct{}, 0) + shutdownStarted = make(chan struct{}) ) // ShutdownMiddleware rejects request once shutdown starts. @@ -32,7 +32,6 @@ func ShutdownMiddleware(h goa.Handler) goa.Handler { err := h(ctx, rw, req) if err == context.Canceled || ctx.Err() == context.Canceled { log.Info(ctx, "Context was cancelled") - err = nil if shutdown.Started() { return errShutdown } diff --git a/api/tool/rankdb-cli/main.go b/api/tool/rankdb-cli/main.go index 3f62dfe..416b61a 100644 --- a/api/tool/rankdb-cli/main.go +++ b/api/tool/rankdb-cli/main.go @@ -37,7 +37,7 @@ func main() { // Execute! if err := app.Execute(); err != nil { - fmt.Fprintf(os.Stderr, err.Error()) + fmt.Fprint(os.Stderr, err.Error()) os.Exit(-1) } } diff --git a/backup/job.go b/backup/job.go index 8018529..6489e21 100644 --- a/backup/job.go +++ b/backup/job.go @@ -53,7 +53,6 @@ func (id ID) Cancel() { return } job.cancel() - return } func (id ID) String() string { diff --git a/backup/s3/s3.go b/backup/s3/s3.go index 2224d43..7448649 100644 --- a/backup/s3/s3.go +++ b/backup/s3/s3.go @@ -48,7 +48,7 @@ func (f *File) Save(ctx context.Context) (io.WriteCloser, error) { go func() { res, err := uploader.UploadWithContext(ctx, &input) if err != nil { - reader.CloseWithError(err) + _ = reader.CloseWithError(err) log.Error(ctx, "Unable to upload data", "error", err.Error()) } f.Result <- res diff --git a/backup/server/rankdb.go b/backup/server/rankdb.go index 61fb397..ba8a618 100644 --- a/backup/server/rankdb.go +++ b/backup/server/rankdb.go @@ -88,7 +88,7 @@ func (b *bodyWriter) Transfer(ctx context.Context) { } log.Info(ctx, "Destination server returned ok", "full_path", b.req.URL.String()) b.r.Close() - io.Copy(ioutil.Discard, resp.Body) + _, _ = io.Copy(ioutil.Discard, resp.Body) resp.Body.Close() } diff --git a/blobstore/badgerstore/badger.go b/blobstore/badgerstore/badger.go index 931a489..cacf215 100644 --- a/blobstore/badgerstore/badger.go +++ b/blobstore/badgerstore/badger.go @@ -88,7 +88,7 @@ func (b *BadgerStore) flush() error { txn := b.db.NewTransaction(true) for k, v := range b.queue { if err := txn.Set([]byte(k), v); err == badger.ErrTxnTooBig { - err = txn.Commit() + _ = txn.Commit() txn = b.db.NewTransaction(true) err = txn.Set([]byte(k), v) if err != nil { diff --git a/blobstore/bstest/bstest.go b/blobstore/bstest/bstest.go index 18badc4..3fe3c79 100644 --- a/blobstore/bstest/bstest.go +++ b/blobstore/bstest/bstest.go @@ -1,3 +1,4 @@ +//nolint // Package bstest supplies helpers to test blobstores. package bstest @@ -255,6 +256,7 @@ func (t Test) Colliding(ctx context.Context, tt *testing.T) { } gRNG := rand.New(rand.NewSource(1337)) set, key := randStringRng(20, gRNG.Int63()), randStringRng(20, gRNG.Int63()) + //nolint:errcheck test := func(i int) { rng := rand.New(rand.NewSource(int64(i))) var blobSize = int(rng.Int63()%MaxBlobSize) + 1 diff --git a/blobstore/lazysaver.go b/blobstore/lazysaver.go index 72a71ba..4163f09 100644 --- a/blobstore/lazysaver.go +++ b/blobstore/lazysaver.go @@ -179,7 +179,7 @@ func NewLazySaver(store Store, opts ...lazySaveOption) (*LazySaver, error) { cacheIdx: make(map[string]*list.Element), itemAdded: make(chan struct{}, 1), savech: make(chan *list.Element), - shutdownCh: make(chan struct{}, 0), + shutdownCh: make(chan struct{}), } for _, opt := range opts { err := opt(&l.lazySaveOptions) @@ -681,5 +681,6 @@ func putDataBufferChunk(p []byte) { if size >= 1<<20 { i = 10 } + //nolint - don't tell me what to make pointerlike dataChunkPools[i].Put(p[:0]) } diff --git a/cmd/rankdb/main.go b/cmd/rankdb/main.go index 554025b..967c436 100644 --- a/cmd/rankdb/main.go +++ b/cmd/rankdb/main.go @@ -54,7 +54,7 @@ func main() { var dumpOnce sync.Once shutdown.OnTimeout(func(stage shutdown.Stage, s string) { dumpOnce.Do(func() { - pprof.Lookup("goroutine").WriteTo(lr.Out, 1) + _ = pprof.Lookup("goroutine").WriteTo(lr.Out, 1) }) }) @@ -74,7 +74,7 @@ func main() { } }() } - api.StartServices(logger, ctx, err) + api.StartServices(logger, ctx) } // exitOnFailure prints a fatal error message and exits the process with status 1. diff --git a/elements.go b/elements.go index 6f4c8ab..6dbf4d2 100644 --- a/elements.go +++ b/elements.go @@ -314,7 +314,6 @@ func (l *Elements) MergeDeduplicate(ins Elements) { // Re-sort. sort.Slice(ins, ins.Sorter()) *l = ins - return } // MinMax returns the minimum and maximum values of the elements. @@ -451,22 +450,6 @@ func (e Elements) FirstElementsWithScore(scores []uint64) Elements { return res } -// minMaxUnsorted provides the smallest and biggest score -// on unsorted elements. -func (l Elements) minMaxUnsorted() (min, max uint64) { - min = math.MaxUint64 - max = 0 - for _, v := range l { - if v.Score > max { - max = v.Score - } - if v.Score < min { - min = v.Score - } - } - return min, max -} - // ElementIDs returns element ids as ranked elements, // where score is the element id and payload is the segment to which they belong. func (l Elements) ElementIDs(id SegmentID) IndexElements { diff --git a/elements_test.go b/elements_test.go index 1e9c756..e85d7d8 100644 --- a/elements_test.go +++ b/elements_test.go @@ -1,3 +1,4 @@ +//nolint:errcheck package rankdb_test // Copyright 2019 Vivino. All rights reserved diff --git a/list-id.go b/list-id.go index 9dc5327..0197dac 100644 --- a/list-id.go +++ b/list-id.go @@ -48,5 +48,4 @@ func (ids ListIDs) Sort() { sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] }) - return } diff --git a/list.go b/list.go index 08a3ee9..e12ffae 100644 --- a/list.go +++ b/list.go @@ -783,8 +783,8 @@ func (l *List) reindex(ctx context.Context, bs blobstore.Store, segs *segments) // VerifyUnlocked validates that all elements of the list can be locked. // This is only really usable for tests, where list context is controlled. func (l *List) VerifyUnlocked(ctx context.Context, timeout time.Duration) error { - var fail = make(chan struct{}, 0) - var ok = make(chan struct{}, 0) + var fail = make(chan struct{}) + var ok = make(chan struct{}) var wg = &sync.WaitGroup{} go func() { @@ -806,7 +806,7 @@ func (l *List) VerifyUnlocked(ctx context.Context, timeout time.Duration) error return errors.New("timeout acquiring locks") case <-ok: } - ok = make(chan struct{}, 0) + ok = make(chan struct{}) // We need read lock. l.RLock() @@ -837,7 +837,7 @@ func (l *List) VerifyUnlocked(ctx context.Context, timeout time.Duration) error select { case <-fail: - pprof.Lookup("goroutine").WriteTo(os.Stderr, 1) + _ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1) time.Sleep(time.Millisecond * 100) return errors.New("timeout acquiring locks") case <-ok: @@ -848,7 +848,7 @@ func (l *List) VerifyUnlocked(ctx context.Context, timeout time.Duration) error func testLock(log log.Adapter, fail chan struct{}, locker sync.Locker, wg *sync.WaitGroup) { wg.Add(1) - var ok = make(chan struct{}, 0) + var ok = make(chan struct{}) go func() { locker.Lock() close(ok) @@ -1022,7 +1022,9 @@ func (l *List) Stats(ctx context.Context, bs blobstore.Store, elements bool) (*L index := segs.index res.Elements = scores.Elements() + //nolint - why's it okay to use scores before checking if it's nil again? res.Segments = len(scores.Segments) + //nolint - why's it okay to use scores before checking if it's nil again? if scores != nil { res.CacheHits = l.scores.cacheHits + l.index.cacheHits res.CacheMisses = l.scores.cacheMisses + l.index.cacheMisses @@ -1057,7 +1059,7 @@ func (l *List) Stats(ctx context.Context, bs blobstore.Store, elements bool) (*L func (l *List) ReleaseSegments(ctx context.Context) { ctx = log.WithFn(ctx) var gotLoading, gotSegments, cancelled bool - var done = make(chan struct{}, 0) + var done = make(chan struct{}) var mu sync.Mutex l.RLock() listID := l.ID diff --git a/list_test.go b/list_test.go index 3caa961..2ee27ae 100644 --- a/list_test.go +++ b/list_test.go @@ -1,3 +1,4 @@ +//nolint package rankdb_test // Copyright 2019 Vivino. All rights reserved @@ -240,6 +241,9 @@ func TestNewListClone(t *testing.T) { rankdb.WithListOption.Clone(l0), rankdb.WithListOption.Cache(nil), ) + if err != nil { + t.Fatal(err) + } // Delete all of original err = l0.DeleteAll(ctx, store) diff --git a/lists_test.go b/lists_test.go index 63f0efb..5a9b2f1 100644 --- a/lists_test.go +++ b/lists_test.go @@ -1,3 +1,4 @@ +//nolint package rankdb_test // Copyright 2019 Vivino. All rights reserved diff --git a/msgppool.go b/msgppool.go index 1f12153..92f97aa 100644 --- a/msgppool.go +++ b/msgppool.go @@ -36,7 +36,6 @@ var writerMsgpPool = sync.Pool{ } const ( - versionExt = 0 // Reserved for future extensions. versionError = 255 // Error ) diff --git a/ranksdb_test.go b/ranksdb_test.go index fa87972..2070cf6 100644 --- a/ranksdb_test.go +++ b/ranksdb_test.go @@ -16,7 +16,7 @@ func TestMain(m *testing.M) { go func() { <-time.After(200 * time.Second) fmt.Println("200 second timeout... Goroutines:") - pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) + _ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) os.Exit(1) }() os.Exit(m.Run()) diff --git a/segment.go b/segment.go index a4b9613..6cbacb3 100644 --- a/segment.go +++ b/segment.go @@ -192,7 +192,7 @@ func (s *Segment) loadElements(ctx context.Context, store blobstore.WithSet, rea } return forReader, err } - s.loader.Loading = make(chan struct{}, 0) + s.loader.Loading = make(chan struct{}) s.loader.LoadingMu.Unlock() defer func() { // Signal others we are loaded. diff --git a/segments.go b/segments.go index 71ddfd1..a3e7bf7 100644 --- a/segments.go +++ b/segments.go @@ -444,7 +444,11 @@ func (s *Segments) DeleteElements(ctx context.Context, bs blobstore.WithSet, e E log.Info(ctx, "Did not find all elements", "left", len(toDelete)-ndeleted) } ls.elements = ls.elements[:wrtto] - s.replaceSegment(ctx, bs, ls) + err = s.replaceSegment(ctx, bs, ls) + if err != nil { + errC <- err + return + } if withIdx { deleted.Sort() idxC <- deleted @@ -901,17 +905,6 @@ func (s *Segments) Delete(ctx context.Context, store blobstore.WithSet) error { return store.Delete(ctx, string(s.ID)) } -// topRankOf returns the rank of the top element in segment. -func (s *Segments) topRankOf(segIdx int) (n int) { - for i := range s.Segments { - if i == segIdx { - return n - } - n += s.Segments[i].N - } - panic("segment index out of bounds") -} - // FindElements returns elements with the supplied indices. // Not found errors are logged, but not fatal. // Caller must hold s.scores read lock. @@ -925,7 +918,7 @@ func (s *Segments) FindElements(ctx context.Context, bs blobstore.WithSet, ids I // For each segment look up the collected elements. var wg sync.WaitGroup - var errch = make(chan error, 0) + var errch = make(chan error) var resch = make(chan []RankedElement, len(split)) wg.Add(len(split)) offsets, total := s.topOffsets() @@ -1133,7 +1126,7 @@ func (s *Segments) updateIndexElements(ctx context.Context, bs blobstore.WithSet split := s.splitIndexUpdates(ctx, e) // For each segment look up the collected elements. var wg sync.WaitGroup - var errch = make(chan error, 0) + var errch = make(chan error) now := uint32(time.Now().Unix()) wg.Add(len(split)) for segIdx, elems := range split { @@ -1191,7 +1184,7 @@ func (s *Segments) getFirstWithScore(ctx context.Context, bs blobstore.WithSet, // The find the elements in each segment. split := s.splitScores(ctx, scores) var wg sync.WaitGroup - var errch = make(chan error, 0) + var errch = make(chan error) var resch = make(chan Elements, len(split)) wg.Add(len(split)) var b = NewBucket(8) diff --git a/sortfloat/float_test.go b/sortfloat/float_test.go index b34f325..aaf0dba 100644 --- a/sortfloat/float_test.go +++ b/sortfloat/float_test.go @@ -25,7 +25,7 @@ func TestSortableFloat64(t *testing.T) { tosort = append(tosort, forward) } sort.SliceStable(tosort, func(i, j int) bool { return tosort[i] < tosort[j] }) - sort.Sort(sort.Float64Slice(values)) + sort.Float64s(values) for i, v := range values { got := ReverseFloat64(tosort[i]) diff --git a/tools.go b/tools.go index c09bcef..ae031c1 100644 --- a/tools.go +++ b/tools.go @@ -81,14 +81,9 @@ finish: select { case <-ctx.Done(): // Flush errors and return. - for { - select { - case _, ok := <-errch: - if !ok { - return ctx.Err() - } - } + for range errch { } + return ctx.Err() case err, ok := <-errch: if !ok { break finish