protocol-diver / go-gossip

Go implementation of the Gossip protocol
BSD 3-Clause "New" or "Revised" License
10 stars 0 forks source link

Is the `Transport.WriteToUDP` method is being implemented? #8

Closed fuadnafiz98 closed 1 year ago

fuadnafiz98 commented 1 year ago

Hello, :wave:

I am exploring the codebase to have an idea of how the gossip protocol works. I stumbled upon this line of code https://github.com/protocol-diver/go-gossip/blob/131e97a1a2d6b3a03078228aec3e7ffdd87d7a72/transport.go#L9,

I am not fully sure if the WriteToUDP method has been implemented or not.

Am I missing something, I will really appreciate if you can help me to understand it.

Thanks a lot :pray:

dbadoy commented 1 year ago

Hi! The WriteToUDP method is not implemented by this library. WriteToUDP, ReadFromUDP are some of the methods of net.UDPConn in the Golang base library.

You can create a net.UDPConn extenally and then convert it to a Transport.

import (
    "net"

    gossip "github.com/protocol-diver/go-gossip"
)

func main() {
    addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:1234")
    if err != nil {
        panic(err)
    }

    conn, err := net.DialUDP("udp", nil, addr)
    if err != nil {
        panic(err)
    }

    _ = gossip.Transport(conn)
}


Instead of using the net.UDPConn directly, we use to an interface called Transport to implement it externally as needed. Below is an example of implementing a Transport that can collect data inbound and outbound counts.

type myTransport struct {
    conn         *net.UDPConn
    ingressCount uint64
    egressCount  uint64
}

func (t *myTransport) ReadFromUDP(b []byte) (int, *net.UDPAddr, error) {
    n, addr, err := t.conn.ReadFromUDP(b)
    t.ingressCount++
    return n, addr, err
}

func (t *myTransport) WriteToUDP(b []byte, addr *net.UDPAddr) (int, error) {
    n, err := t.conn.WriteToUDP(b, addr)
    t.egressCount++
    return n, err
}

func main() {
    addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:1234")
    if err != nil {
        panic(err)
    }

    conn, err := net.DialUDP("udp", nil, addr)
    if err != nil {
        panic(err)
    }

    transport := &myTransport{conn, 0, 0}

    _ = gossip.Transport(transport)
}


I hope the answer was helpful to you.

fuadnafiz98 commented 1 year ago

Hi, Thank you so much for explaining!

Now I can understand how the Transport struct can be constructed.

I really appreciate your help. I will try to make a PR of some documentation so it can help any one in the future ;)

I am closing the issue.

Have a nice day! :)