Skip to content

Commit

Permalink
Object argument added to Lock/Unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-scherba committed Aug 7, 2023
1 parent b067ee3 commit 5f05225
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions teomutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func (m *Mutex) SetLogWriter(w io.Writer) {
}

// Lock mutex
func (m Mutex) Lock() error {
func (m Mutex) Lock(object ...string) error {
repeatAfter := 1 * time.Millisecond
start := time.Now()
for {
if err := m.uploadObject(); err == nil {
if err := m.uploadObject(object...); err == nil {
return nil
} else {
fmt.Fprintf(m.w, "%s\n", err)
Expand All @@ -147,21 +147,27 @@ func (m Mutex) Lock() error {
}
}

// Unock mutex
func (m Mutex) Unlock() error {
return m.deleteObject()
// Unlock mutex
func (m Mutex) Unlock(object ...string) error {
return m.deleteObject(object...)
}

// uploadObject uploads mutex object.
func (m Mutex) uploadObject() error {
func (m Mutex) uploadObject(obj ...string) error {

// Define object
object := m.object
if len(obj) > 0 {
object = obj[0]
}

// Print start message
fmt.Fprintf(m.w, "Uploading object %s started...\n", m.object)
fmt.Fprintf(m.w, "Uploading object %s started...\n", object)

// Create bytes reader to upload
r := bytes.NewReader([]byte("locked"))

o := m.client.Bucket(m.bucket).Object(m.object)
o := m.client.Bucket(m.bucket).Object(object)

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to upload is aborted if the
Expand Down Expand Up @@ -189,20 +195,26 @@ func (m Mutex) uploadObject() error {
}

// Print success result message
fmt.Fprintf(m.w, "Blob %s uploaded.\n", m.object)
fmt.Fprintf(m.w, "Blob %s uploaded.\n", object)
return nil
}

// deleteObject deletess mutex object.
func (m Mutex) deleteObject() error {
func (m Mutex) deleteObject(obj ...string) error {

// Define object
object := m.object
if len(obj) > 0 {
object = obj[0]
}

// Print start message
fmt.Fprintf(m.w, "Deleting object %s started...\n", m.object)
fmt.Fprintf(m.w, "Deleting object %s started...\n", object)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

o := m.client.Bucket(m.bucket).Object(m.object)
o := m.client.Bucket(m.bucket).Object(object)

// Optional: set a generation-match precondition to avoid potential race
// conditions and data corruptions. The request to delete the file is aborted
Expand All @@ -214,10 +226,10 @@ func (m Mutex) deleteObject() error {
o = o.If(storage.Conditions{GenerationMatch: attrs.Generation})

if err := o.Delete(ctx); err != nil {
return fmt.Errorf("object(%q).Delete: %w", m.object, err)
return fmt.Errorf("object(%q).Delete: %w", object, err)
}

// Print success result message
fmt.Fprintf(m.w, "Blob %s deleted.\n", m.object)
fmt.Fprintf(m.w, "Blob %s deleted.\n", object)
return nil
}

0 comments on commit 5f05225

Please sign in to comment.