github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/queue/unique_queue.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package queue
     7  
     8  import (
     9  	"fmt"
    10  )
    11  
    12  // UniqueQueue defines a queue which guarantees only one instance of same
    13  // data is in the queue. Instances with same identity will be
    14  // discarded if there is already one in the line.
    15  //
    16  // This queue is particularly useful for preventing duplicated task
    17  // of same purpose - please note that this does not guarantee that a particular
    18  // task cannot be processed twice or more at the same time. Uniqueness is
    19  // only guaranteed whilst the task is waiting in the queue.
    20  //
    21  // Users of this queue should be careful to push only the identifier of the
    22  // data
    23  type UniqueQueue interface {
    24  	Queue
    25  	PushFunc(Data, func() error) error
    26  	Has(Data) (bool, error)
    27  }
    28  
    29  // ErrAlreadyInQueue is returned when trying to push data to the queue that is already in the queue
    30  var ErrAlreadyInQueue = fmt.Errorf("already in queue")