interface
This commit is contained in:
parent
052bd7c190
commit
3f1660ed44
|
@ -22,6 +22,21 @@ const (
|
||||||
AppendOpenFlags = os.O_APPEND | os.O_CREATE | os.O_WRONLY
|
AppendOpenFlags = os.O_APPEND | os.O_CREATE | os.O_WRONLY
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Key defines the set of types that can be used as keys in the
|
||||||
|
// UploadScheduler. It can be any integer type or a string.
|
||||||
|
type Key interface {
|
||||||
|
constraints.Integer | ~string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scheduler manages the scheduling of multi-part uploads. It maintains
|
||||||
|
// a map of active uploads and handles the appending of chunks, as well as
|
||||||
|
// the automatic finalization of uploads based on a timeout.
|
||||||
|
type Scheduler[K Key] interface {
|
||||||
|
Prepare(k K, timeout time.Duration, cb func(K, error)) error
|
||||||
|
Append(k K, chunk multipart.File, dst io.Writer) error
|
||||||
|
Finish(k K) error
|
||||||
|
}
|
||||||
|
|
||||||
// upload holds the state for a single upload, including its timeout
|
// upload holds the state for a single upload, including its timeout
|
||||||
// duration and an associated timer.
|
// duration and an associated timer.
|
||||||
type upload struct {
|
type upload struct {
|
||||||
|
@ -29,23 +44,15 @@ type upload struct {
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadKey defines the set of types that can be used as keys in the
|
// scheduler implements the Scheduler interface.
|
||||||
// UploadScheduler. It can be any integer type or a string.
|
type scheduler[K Key] struct {
|
||||||
type UploadKey interface {
|
|
||||||
constraints.Integer | ~string
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadScheduler manages the scheduling of multi-part uploads. It maintains
|
|
||||||
// a map of active uploads and handles the appending of chunks, as well as
|
|
||||||
// the automatic finalization of uploads based on a timeout.
|
|
||||||
type UploadScheduler[K UploadKey] struct {
|
|
||||||
m *haxmap.Map[K, upload]
|
m *haxmap.Map[K, upload]
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewScheduler creates a new UploadScheduler instance. It returns an
|
// NewScheduler creates a new UploadScheduler instance. It returns an
|
||||||
// UploadScheduler configured to manage uploads keyed by the specified type.
|
// UploadScheduler configured to manage uploads keyed by the specified type.
|
||||||
func NewScheduler[K UploadKey]() UploadScheduler[K] {
|
func NewScheduler[K Key]() Scheduler[K] {
|
||||||
return UploadScheduler[K]{
|
return scheduler[K]{
|
||||||
m: haxmap.New[K, upload](),
|
m: haxmap.New[K, upload](),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,7 +68,7 @@ func NewScheduler[K UploadKey]() UploadScheduler[K] {
|
||||||
// finished, the callback function is invoked.
|
// finished, the callback function is invoked.
|
||||||
//
|
//
|
||||||
// Returns an error if the key already exists in the scheduler.
|
// Returns an error if the key already exists in the scheduler.
|
||||||
func (us UploadScheduler[K]) Prepare(k K, timeout time.Duration, cb func(K, error)) error {
|
func (us scheduler[K]) Prepare(k K, timeout time.Duration, cb func(K, error)) error {
|
||||||
_, ok := us.m.Get(k)
|
_, ok := us.m.Get(k)
|
||||||
if ok {
|
if ok {
|
||||||
return errors.New("upload key already exists")
|
return errors.New("upload key already exists")
|
||||||
|
@ -93,7 +100,7 @@ func (us UploadScheduler[K]) Prepare(k K, timeout time.Duration, cb func(K, erro
|
||||||
//
|
//
|
||||||
// It is recommended to use AppendOpenFlags for actual files that are passed
|
// It is recommended to use AppendOpenFlags for actual files that are passed
|
||||||
// to this function.
|
// to this function.
|
||||||
func (us UploadScheduler[K]) Append(k K, chunk multipart.File, dst io.Writer) error {
|
func (us scheduler[K]) Append(k K, chunk multipart.File, dst io.Writer) error {
|
||||||
u, ok := us.m.Get(k)
|
u, ok := us.m.Get(k)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("upload key does not exist")
|
return errors.New("upload key does not exist")
|
||||||
|
@ -113,7 +120,7 @@ func (us UploadScheduler[K]) Append(k K, chunk multipart.File, dst io.Writer) er
|
||||||
// Finish finalizes the upload associated with the given key. It stops the
|
// Finish finalizes the upload associated with the given key. It stops the
|
||||||
// associated timer and removes the upload from the scheduler's internal map.
|
// associated timer and removes the upload from the scheduler's internal map.
|
||||||
// If the key does not exist, an error is returned.
|
// If the key does not exist, an error is returned.
|
||||||
func (us UploadScheduler[K]) Finish(k K) error {
|
func (us scheduler[K]) Finish(k K) error {
|
||||||
u, ok := us.m.Get(k)
|
u, ok := us.m.Get(k)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("upload key does not exist")
|
return errors.New("upload key does not exist")
|
||||||
|
|
Loading…
Reference in a new issue