suborbital / grav

Embedded decentralized message bus
Apache License 2.0
103 stars 8 forks source link

Example Mesh Network #58

Open dills122 opened 2 years ago

dills122 commented 2 years ago

I started playing around with this project a little bit and was wondering if there was any examples on how to setup a multi Grav instanced network?

I noticed the docs in this area was still under active work, so just wanted to reach out to see if there happened to be any working examples or anything I could play around with to get my head around some of this.

Thanks!

dills122 commented 2 years ago

I gave an attempt at a basic Mesh network after taking a look at the http transport tester, which I have pasted below.

package main

import (
    "fmt"
    "log"
    "os"
    "strconv"
    "time"

    "github.com/suborbital/grav/discovery/local"
    "github.com/suborbital/grav/grav"
    ghttp "github.com/suborbital/grav/transport/http"
    "github.com/suborbital/vektor/vk"
    "github.com/suborbital/vektor/vlog"
)

func main() {
    anotherTest()
}

type TestNode struct {
    GravPort string
    HttpPort int
}

func anotherTest() {

    var testNodes [2]TestNode

    testNodes[0] = TestNode{GravPort: "5555", HttpPort: 4011}
    testNodes[1] = TestNode{GravPort: "5555", HttpPort: 4012}

    logger := vlog.Default(vlog.Level(vlog.LogLevelDebug))
    gravhttp := ghttp.New()
    locald := local.New()

    var testNode TestNode

    if len(os.Args) >= 1 {
        if s, err := strconv.ParseInt(os.Args[1], 10, 32); err == nil {
            testNode = testNodes[int(s)]
        }
    }

    g := grav.New(
        grav.UseLogger(logger),
        grav.UseEndpoint(testNode.GravPort, ""),
        grav.UseTransport(gravhttp),
        grav.UseDiscovery(locald),
    )

    pod := g.Connect()
    pod.On(func(msg grav.Message) error {
        fmt.Println("received something:", string(msg.Data()))
        return nil
    })

    vk := vk.New(
        vk.UseAppName("http tester"),
        vk.UseHTTPPort(testNode.HttpPort),
    )
    vk.POST("/meta/message", gravhttp.HandlerFunc())

    go func() {
        <-time.After(time.Second * time.Duration(10))
        pod.Send(grav.NewMsg(grav.MsgTypeDefault, []byte("hello, world from: "+g.NodeUUID)))
    }()

    if err := vk.Start(); err != nil {
        log.Fatal(err)
    }
}

Then I run it with

go run ./main.go 0 # Node one

# separate console
go run ./main.go 1 # Node two

However, I keep getting an handshake error.

{
        "log_message": "(E) failed to connection.DoOutgoingHandshake: failed to doRequest: [transport-http] failed to Do request: Post \"http://{IP-REDACTED}:5555/meta/message\": dial tcp {IP-REDACTED}:5555: connectex: No connection could be made because the target machine actively refused it.",
        "timestamp": "2022-03-11T20:49:31.3951421-05:00",
        "level": 1
    }

Do I need to configure something on my machine to allow the local discovery to work correctly or did I not set something up correctly in the Node configurations?

Any help would be appreciated.

cohix commented 2 years ago

@dills122 apologies for not seeing this sooner! I think the websocket transport is the easiest to get started with, it's more reasonable than the HTTP plugin... in fact we plan to deprecate the HTTP plugin soon.

Ensuring that your two nodes are on different ports is important, and that UseEndpoint is correctly advertising the URL/port of the node.