Skip to content

Instantly share code, notes, and snippets.

@amdprophet
Forked from joshrotenberg/embedded.go
Created June 29, 2021 20:00
Show Gist options
  • Save amdprophet/24f7d7eaab0dedf6a1ce131a34e7fdb0 to your computer and use it in GitHub Desktop.
Save amdprophet/24f7d7eaab0dedf6a1ce131a34e7fdb0 to your computer and use it in GitHub Desktop.

Revisions

  1. Rotenberg, Joshua revised this gist Mar 16, 2016. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,11 @@ package main
    // This is a basic example of running an nsqd instance embedded. It creates
    // and runs an nsqd with all of the default options, and then produces
    // and consumes a single message. You are probably better off running a
    // standalone instance, but embedding it can simpllify deployment.
    // standalone instance, but embedding it can simplify deployment and is
    // useful in testing.

    // See https://github.com/bitly/nsq/blob/master/nsqd/options.go and
    // https://github.com/bitly/nsq/blob/master/apps/nsqd/nsqd.go for
    // See https://github.com/nsqio/nsq/blob/master/nsqd/options.go and
    // https://github.com/nsqio/nsq/blob/master/apps/nsqd/nsqd.go for
    // more details on how to configure an embedded nsqd instance.
    import (
    "bytes"
    @@ -31,6 +32,8 @@ func main() {
    opts := nsqd.NewOptions()
    nsqd := nsqd.New(opts)
    nsqd.Main()

    // wait until we are told to continue and exit
    <-done
    nsqd.Exit()
    }()
  2. Rotenberg, Joshua revised this gist Mar 16, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,8 @@ import (
    "log"
    "time"

    "github.com/bitly/go-nsq"
    "github.com/bitly/nsq/nsqd"
    "github.com/nsqio/go-nsq"
    "github.com/nsqio/nsq/nsqd"
    )

    func main() {
    @@ -28,8 +28,8 @@ func main() {
    // binary mainly wraps up the handling of command
    // line args and does something similar

    opts := nsqd.NewNSQDOptions()
    nsqd := nsqd.NewNSQD(opts)
    opts := nsqd.NewOptions()
    nsqd := nsqd.New(opts)
    nsqd.Main()
    <-done
    nsqd.Exit()
  3. @joshrotenberg joshrotenberg revised this gist Nov 10, 2014. 1 changed file with 13 additions and 15 deletions.
    28 changes: 13 additions & 15 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -11,31 +11,29 @@ package main
    import (
    "bytes"
    "log"

    "time"

    "github.com/bitly/go-nsq"
    "github.com/bitly/nsq/nsqd"
    )

    func runDefaultNSQD(done chan bool) {

    // running an nsqd with all of the default options (as if you
    // ran it from the command line with no flags) is literally
    // these three lines of code. the nsqd binary mainly wraps up
    // the handling of command line args and does something similar
    opts := nsqd.NewNSQDOptions()
    nsqd := nsqd.NewNSQD(opts)
    nsqd.Main()
    <-done
    nsqd.Exit()
    }

    func main() {
    done := make(chan bool)

    // Run the embedded nsqd in a go routine
    go runDefaultNSQD(done)
    go func() {
    // running an nsqd with all of the default options
    // (as if you ran it from the command line with no flags)
    // is literally these three lines of code. the nsqd
    // binary mainly wraps up the handling of command
    // line args and does something similar

    opts := nsqd.NewNSQDOptions()
    nsqd := nsqd.NewNSQD(opts)
    nsqd.Main()
    <-done
    nsqd.Exit()
    }()

    cfg := nsq.NewConfig()

  4. @joshrotenberg joshrotenberg revised this gist Nov 10, 2014. 1 changed file with 21 additions and 11 deletions.
    32 changes: 21 additions & 11 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -2,37 +2,41 @@ package main

    // This is a basic example of running an nsqd instance embedded. It creates
    // and runs an nsqd with all of the default options, and then produces
    // and consumes a single message.
    // and consumes a single message. You are probably better off running a
    // standalone instance, but embedding it can simpllify deployment.

    // See https://github.com/bitly/nsq/blob/master/nsqd/options.go and
    // https://github.com/bitly/nsq/blob/master/apps/nsqd/nsqd.go for
    // more details on how to configure an embedded nsqd instance.
    import (
    "bytes"
    "log"
    "os"
    "os/signal"
    "syscall"

    "time"

    "github.com/bitly/go-nsq"
    "github.com/bitly/nsq/nsqd"
    )

    func runDefaultNSQD() {
    func runDefaultNSQD(done chan bool) {

    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
    // running an nsqd with all of the default options (as if you
    // ran it from the command line with no flags) is literally
    // these three lines of code. the nsqd binary mainly wraps up
    // the handling of command line args and does something similar
    opts := nsqd.NewNSQDOptions()
    nsqd := nsqd.NewNSQD(opts)
    nsqd.Main()
    <-signalChan
    <-done
    nsqd.Exit()
    }

    func main() {
    done := make(chan bool)

    // Run the embedded nsqd in a go routine
    go runDefaultNSQD()
    go runDefaultNSQD(done)

    cfg := nsq.NewConfig()

    // the message we'll send to ourselves
    @@ -43,8 +47,9 @@ func main() {
    if err != nil {
    log.Fatal(err)
    }

    // Publish a single message to the 'embedded' topic
    err = p.Publish("embedded", msg) //[]byte("one message"))
    err = p.Publish("embedded", msg)
    if err != nil {
    log.Fatal(err)
    }
    @@ -54,21 +59,26 @@ func main() {
    if err != nil {
    log.Fatal(err)
    }

    // and a single handler that just checks that the message we
    // received matches the message we sent
    c.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
    if bytes.Compare(m.Body, msg) != 0 { //"one message" {
    if bytes.Compare(m.Body, msg) != 0 {
    log.Fatal("message didn't match:", string(m.Body))
    } else {
    log.Println("message matched:", string(m.Body))
    }
    return nil
    }))

    // Connect the consumer to the embedded nsqd instance
    c.ConnectToNSQD("localhost:4150")

    // Sleep a little to give everything time to start up and let
    // our producer and consumer run
    time.Sleep(250 * time.Millisecond)

    // tell the nsqd instance to exit
    done <- true

    }
  5. @joshrotenberg joshrotenberg revised this gist Nov 10, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -58,9 +58,9 @@ func main() {
    // received matches the message we sent
    c.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
    if bytes.Compare(m.Body, msg) != 0 { //"one message" {
    log.Fatal(m)
    log.Fatal("message didn't match:", string(m.Body))
    } else {
    log.Println("sent and received:", string(m.Body))
    log.Println("message matched:", string(m.Body))
    }
    return nil
    }))
  6. @joshrotenberg joshrotenberg revised this gist Nov 10, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,6 @@ import (

    func runDefaultNSQD() {

    //rand.Seed(time.Now().UTC().UnixNano())

    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
    opts := nsqd.NewNSQDOptions()
  7. @joshrotenberg joshrotenberg revised this gist Nov 10, 2014. 1 changed file with 70 additions and 1 deletion.
    71 changes: 70 additions & 1 deletion embedded.go
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,76 @@
    package main

    // This is a basic example of running an nsqd instance embedded. It creates
    // and runs an nsqd with all of the default options, and then produces
    // and consumes a single message.

    func main() {
    // See https://github.com/bitly/nsq/blob/master/nsqd/options.go and
    // https://github.com/bitly/nsq/blob/master/apps/nsqd/nsqd.go for
    // more details on how to configure an embedded nsqd instance.
    import (
    "bytes"
    "log"
    "os"
    "os/signal"
    "syscall"
    "time"

    "github.com/bitly/go-nsq"
    "github.com/bitly/nsq/nsqd"
    )

    func runDefaultNSQD() {

    //rand.Seed(time.Now().UTC().UnixNano())

    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
    opts := nsqd.NewNSQDOptions()
    nsqd := nsqd.NewNSQD(opts)
    nsqd.Main()
    <-signalChan
    nsqd.Exit()
    }

    func main() {
    // Run the embedded nsqd in a go routine
    go runDefaultNSQD()
    cfg := nsq.NewConfig()

    // the message we'll send to ourselves
    msg := []byte("the message")

    // Set up a Producer, pointing at the default host:port
    p, err := nsq.NewProducer("localhost:4150", cfg)
    if err != nil {
    log.Fatal(err)
    }
    // Publish a single message to the 'embedded' topic
    err = p.Publish("embedded", msg) //[]byte("one message"))
    if err != nil {
    log.Fatal(err)
    }

    // Now set up a consumer
    c, err := nsq.NewConsumer("embedded", "local", cfg)
    if err != nil {
    log.Fatal(err)
    }
    // and a single handler that just checks that the message we
    // received matches the message we sent
    c.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error {
    if bytes.Compare(m.Body, msg) != 0 { //"one message" {
    log.Fatal(m)
    } else {
    log.Println("sent and received:", string(m.Body))
    }
    return nil
    }))
    // Connect the consumer to the embedded nsqd instance
    c.ConnectToNSQD("localhost:4150")

    // Sleep a little to give everything time to start up and let
    // our producer and consumer run
    time.Sleep(250 * time.Millisecond)

    }
  8. @joshrotenberg joshrotenberg created this gist Nov 10, 2014.
    7 changes: 7 additions & 0 deletions embedded.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    package main


    func main() {

    }