Giter Site home page Giter Site logo

how do u use this thing about redsync HOT 4 CLOSED

go-redsync avatar go-redsync commented on June 18, 2024 2
how do u use this thing

from redsync.

Comments (4)

MaerF0x0 avatar MaerF0x0 commented on June 18, 2024

probably doesnt even compile.

func del_key(key string) (bool, error) {
    redisUrl := "redis://localhost:6379"
    p := &redis.Pool{
        Dial: func() (redis.Conn, error) {
            c, err := redis.DialURL(redisUrl)
            return c, err
        },
    }
    rs := redsync.New([]Pool{p})

    m := rs.NewMutex("GLOBAL")

    var err error

    if err := m.Lock(); err != nil {
        fmt.Println("Failed", err)
    } else {
        fmt.Println("Acquired")
        defer m.Unlock()
    }

    c := p.Get()
    defer c.Close()
    return c.Do("DEL", "something")
}

from redsync.

hjr265 avatar hjr265 commented on June 18, 2024

@MaerF0x0 I suppose you mean the snippet above doesn't compile. I tried it out locally with Redsync and Redigo. Had to make two tiny changes to get it through the compiler:

func del_key(key string) (interface{}, error) {
    redisUrl := "redis://localhost:6379"
    p := &redis.Pool{
        Dial: func() (redis.Conn, error) {
            c, err := redis.DialURL(redisUrl)
            return c, err
        },
    }
    rs := redsync.New([]redsync.Pool{p})

    m := rs.NewMutex("GLOBAL")

    if err := m.Lock(); err != nil {
        fmt.Println("Failed", err)
    } else {
        fmt.Println("Acquired")
        defer m.Unlock()
    }

    c := p.Get()
    defer c.Close()
    return c.Do("DEL", "something")
}

from redsync.

SebastiaanKlippert avatar SebastiaanKlippert commented on June 18, 2024

Thanks for the sample. I am correct in assuming I can create one global Redsync var using redsync.New([]Pool{p}) or do I need a new one for each mutex?

I have done some tests using the global var using the race detector and it seems to run OK, but I am not sure because the pool is shared in NewMutex.

Also, if we have multiple Go instances (load balanced environment), will a single mutex be sufficient? I looks like you are using a local mutex inside the redsync mutex so on one instance it will work, but what if two or more Go instances will lock the same value? https://redis.io/topics/distlock talks about multiple Redis instances, but I am unsure how that translates to multiple Go instances.

Sorry if this is a novice question, but I think the docs could have some more info about the use cases and I am trying to "Make sure I understand how it works before using it in production environments." 😃
Thanks.

[edit]
After doing some more testing with multiple go instances it seems to work OK.

from redsync.

hjr265 avatar hjr265 commented on June 18, 2024

Hi @SebastiaanKlippert, thanks for trying it out. Glad to know it is working for you OK!

from redsync.

Related Issues (20)

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.