Code Monkey home page Code Monkey logo

Comments (6)

etopian avatar etopian commented on September 15, 2024

It's at bit hacky, and perhaps a better fix could be found but works for my use case for now.

from philote.

etopian avatar etopian commented on September 15, 2024

Without doing this philote very quickly runs out of connections in the connection pool on multiple page loads with each page opening a new websocket.

from philote.

pote avatar pote commented on September 15, 2024

Wow, great catch! I completely missed it.

I'm not on my computer right now, but I'll test this first thing in the morning. Will let you know.

Thank you for your contribution!

from philote.

etopian avatar etopian commented on September 15, 2024

Also need some way to decrement key usage. It seems the usage keeps creeping up on each page load but there is no mechanism for it to go down.

from philote.

etopian avatar etopian commented on September 15, 2024

The following function closes the connection properly and is the recommended way of doing it with redigo, but still stuck on decrementing connections on exit.

https://github.com/garyburd/redigo/issues/65

func (s *Socket) ListenToRedis() {
    rConn := redis.PubSubConn{Conn: RedisPool.Get()}
    //defer rConn.Close()
    defer rConn.Close()

    rConn.Subscribe(s.redisChannels()...)
    s.logMsg("new socket.")
    var (
        message *Message
        err     error
    )

    for {
        switch event := rConn.Receive().(type) {

        case redis.Message:
            //fmt.Printf("%s: %s\n", event.Channel, event.Data)
            err = json.Unmarshal(event.Data, &message)

            //s.logMsg("Event redis: %s", message.Event)
            //s.logMsg("Data redis: %s", message.Data)

            if err != nil {
                s.logMsg("[SECURITY] Redis message isn't JSON: %s", event.Data)
                continue
            }

            switch message.Event {
            case "message":
                //s.logMsg("message.IssuerID %s", message.IssuerID)
                //s.logMsg("s.ID %s", s.ID)
                if message.IssuerID == s.ID {
                    s.logMsg("LOCAL MESSAGE IGNORING")
                    // Message was sent by this connection, ignore.
                    continue
                }

                s.logMsg("Received message from redis on '%s'", message.Channel)
                websocket.JSON.Send(s.ws, &message)

            case "close":

                s.logMsg("Close called, should unsubscribe")
                if message.IssuerID == s.ID {
                    //s.logMsg("unsubscribe %s", s.redisChannels())
                    //rConn.Unsubscribe(message.Channel)
                    //rConn.PUnsubscribe(s.redisChannels()...)
                    err := rConn.Unsubscribe()
                    if err != nil {
                        s.logMsg("error unsubscribe %s: ", err.Error())
                    }

                    s.logMsg("channel len %d", len(s.redisChannels()))

                }

            default:
                s.logMsg("Default msg %s", message.Data)
                break
            }
            break
        case error:
            s.logMsg("Error.")
            defer rConn.Close()
            rConn = redis.PubSubConn{Conn: RedisPool.Get()}
            rConn.Subscribe(s.redisChannels()...)

        case redis.Subscription:
            fmt.Printf("%s: %s %d\n", event.Channel, event.Kind, event.Count)
            if event.Count == 0 {
                log.Println("Done, ending connection.")
                return
            }
            break

        }
    }
}

from philote.

pote avatar pote commented on September 15, 2024

The code was drastically changed, this issue no longer applies. Thanks for reporting!

from philote.

Related Issues (2)

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.