Git Product home page Git Product logo

Comments (11)

x1ddos avatar x1ddos commented on July 18, 2024

One solution is to simply limit goroutine retries to some max.

from gcslock.

x1ddos avatar x1ddos commented on July 18, 2024

Another solution is to stop the locking/unlocking goroutine before returning an error.

from gcslock.

x1ddos avatar x1ddos commented on July 18, 2024

Actually, limiting num. of retries of a locking goroutine won't work. I believe the best approach is to always terminate the locking/unlocking goroutine before returning.

from gcslock.

mco-gh avatar mco-gh commented on July 18, 2024

So, some code which does something like this in a goroutine?

for {
  err := gcslock.Lock(l, time.Second)
  if err == nil {
    break
  }
}

The timed Lock fails for any reason (e.g. timeout), and some other part of the same program gets and releases the lock. Won't this loop eventually obtain the lock properly? Not seeing the inconsistency.

from gcslock.

x1ddos avatar x1ddos commented on July 18, 2024

That example is not a long running process. This one is:

// Loop indefinitely processing one task at a time.
for {
  // Receive a new task from a channel ch.
  task := <- ch
  // Try to obtain a global lock or skip the task, reporting the error.
  if err := gcslock.Lock(l, time.Second); err != nil {
    reportf("lock failed: %v", err)
    // Lock failed so we skip the task and wait for a new one.
    // Unfortunately, the above lock goroutine will keep running,
    // trying to acquire a lock we don't need anymore.
    continue
  }
  execute(task)
}

Most importantly, whenever the above gcslock.Lock(l, time.Second) fails, the underlying gcslock goroutine will needlessly keep trying to acquire the lock and may very well succeed at some point. Meaning, any future "useful" gcslock.Lock(l, time.Second) will never succeed anymore, until the lock is removed by an external process.

This is very similar to what would happen in the case with push-to-publish git lock.

What I think needs to happen instead is for the locking goroutine to stop even if gcslock.Lock returned an error, instead of keep running.

from gcslock.

mco-gh avatar mco-gh commented on July 18, 2024

I get it. Thanks for clarifying. I need to stop the goroutine when I get a timeout, something like this:

  case <-time.After(d):
    // stop running goroutine here 
    return errors.New("lock request timed out")

Looks like there's no way to externally stop a goroutine but I'm sure there's some trick I can use. Will do some googling.

from gcslock.

x1ddos avatar x1ddos commented on July 18, 2024

Exactly, the // stop running goroutine here part is what's missing.
The locking loop needs to be controlled and stopped - it currently never exits.

from gcslock.

x1ddos avatar x1ddos commented on July 18, 2024

There's no built-in way to stop a goroutine via some kind of a "goroutine handle" but it can be controlled using a context.WithTimeout or a channel. I would probably use a context with a deadline equal to the d argument passed to gcslock.Lock.

from gcslock.

mco-gh avatar mco-gh commented on July 18, 2024

On it, first thing tomorrow am. Thanks!

from gcslock.

mco-gh avatar mco-gh commented on July 18, 2024

This looks like exactly my scenario.

from gcslock.

x1ddos avatar x1ddos commented on July 18, 2024

sync.WaitGroup feels like an overkill just for one goroutine:

A WaitGroup waits for a collection of goroutines to finish ...

from gcslock.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.