Git Product home page Git Product logo

Comments (2)

garyburd avatar garyburd commented on June 2, 2024

See #49.

To end a pubsub receive loop, unsubscribe the connection from all channels. In the receive loop, exit when the number of subscriptions == 0:

package main

import (
    "github.com/garyburd/redigo/redis"
    "log"
    "sync"
)

func main() {
    var wg sync.WaitGroup

    conn, err := redis.DialTimeout("tcp", "localhost:6379", 0, 0, 0)  // read timeout is zero
    if err != nil {
        log.Fatal(err)
    }

    pubsub := redis.PubSubConn{conn}
    pubsub.Subscribe("x")

    wg.Add(1)
    go func() {
        defer wg.Done()

        for {
            switch m := pubsub.Receive().(type) {
            case redis.Message:
                log.Println("Message:", m)
            case redis.Subscription:
                if m.Count == 0 {
                    log.Println("Done")
                    return
                }
            case error:
                log.Println("Error:", m)
                return
            }
        }
    }()

    // unsubscribe all to end loop
    pubsub.Unsubscribe()

    // wait for goroutine to exit
    wg.Wait()
}

Another option is to send a heartbeat message at an interval that's shorter than the read timeout. The receive loop can decide to continue or not when handling the heartbeat message.

Polling a network connection with timeouts is not idiomatic in Go.

from redigo.

etiennebarrie avatar etiennebarrie commented on June 2, 2024

Ok I see, thanks. Sorry for not having found the other issue.

from redigo.

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.