Giter Club home page Giter Club logo

Comments (2)

rolandshoemaker avatar rolandshoemaker commented on May 26, 2024

Had a go at this, haven't had a chance to test since I'm going to sleep now but it seems we can stop deliveries to the consumer setup in amqpSubscribe in a way that keeps the rest of the channel open by using Channel.Cancel to stop deliveries to rpc.ServerQueue and flush the channel of remaining messages so we can Publish() the responses before we close the entire channel...

I'll work on this a bit more tomorrow but here is a rough sketch of what I've come up with so far

// Starts the AMQP-RPC server running in a separate thread.
// There is currently no Stop() method.
func (rpc *AmqpRPCServer) Start() (err error) {
    msgs, err := amqpSubscribe(rpc.channel, rpc.serverQueue)
    if err != nil {
        return
    }

    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGTERM)
    signal.Notify(sigChan, os.Interrupt)

    go func() {
        <-sigChan
        log.Printf("  [!] SIGTERM/SIGINT recieved, stopping new deliveries and processing remaining messages")
        // Lets clean things uuup, stop delivers to the consumer
        // i'm not sure if this blocks itself, or if we need to block
        // until len(msgs) == 0...
        rpc.channel.Cancel(rpc.serverQueue, false)
        log.Printf("  [!] Finished processing messages, exiting...")

        // close channel cleanly
        rpc.channel.Close()

        // success i guess?
        os.Exit(0)
    }()

    go func() {
        for msg := range msgs {
            // XXX-JWS: jws.Verify(body)
            cb, present := rpc.dispatchTable[msg.Type]
            log.Printf(" [s<] received %s(%s) [%s]", msg.Type, core.B64enc(msg.Body), msg.CorrelationId)
            if !present {
                continue
            }
            response := cb(msg.Body)
            log.Printf(" [s>] sending %s(%s) [%s]", msg.Type, core.B64enc(response), msg.CorrelationId)
            rpc.channel.Publish(
                AmqpExchange,
                msg.ReplyTo,
                AmqpMandatory,
                AmqpImmediate,
                amqp.Publishing{
                    CorrelationId: msg.CorrelationId,
                    Type:          msg.Type,
                    Body:          response, // XXX-JWS: jws.Sign(privKey, body)
                })
        }
    }()
    return
}

from boulder.

larsth avatar larsth commented on May 26, 2024

@jcjones You could mold https://github.com/fvbock/endless into what you need.

from boulder.

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.