Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: use TCP instead of UDP #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ Packet type (1 byte) <br>


## Transport/Security layer
Transport layer supports peer-to-peer UDP communication.
Transport layer supports peer-to-peer TCP communication. UDP is sufficient If the communication is simply sending a PullRequest to a random peer. However, this library's PullResponse payload size is unpredictable and will likely exceed the safe from packet fragmentation UDP size of 508 bytes(The minimum size of IPv4 datagram every device has to be able to receive(i.g. MTU) is 576 byte. And IP header(with fully option field) is 60 byte, UDP header is 8 byte. So I guess safety maximum UDP packet size from packet fragmentation is 508(576 - 60 - 8) byte). Implementing with UDP requires chopping packets at the application level and sending them over UDP to avoid packet fragmentation. There's no basis for doing that. <br>
Security layer resolve the secure between peer to peer trnasmission. It should be possible to add multiple encryption algorithms. I'm just considering a method of encrypting and decrypting using a passphrase(It is also okay to encrypt in the application and then propagate it. In this case, you should set NO-SECURE in config). <br>

Encrypt alogrithm (1 byte) <br>
> 0: NO-SECURE <br>
> 1: AES-256-CBC <br>

Max packet size is n.
22 changes: 18 additions & 4 deletions gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,27 @@ func (g *Gossiper) selectRandomPeers(n int) []string {
if len(peers) <= n {
return peers
}
random := rand.New(rand.NewSource(time.Now().UnixNano()))

// TODO(dbadoy): Avoid duplicate selection.
selected := make([]string, 0, n)
var (
random = rand.New(rand.NewSource(time.Now().UnixNano()))
indices = make([]int, 0, n)
selected = make([]string, 0, n)
)

// Avoid duplicate selection.
for r := random.Intn(n); len(indices) != n; r = random.Intn(n) {
for _, v := range indices {
if v == r {
continue
}
}
indices = append(indices, r)
}

for i := 0; i < n; i++ {
selected = append(selected, peers[random.Intn(n)])
selected = append(selected, peers[indices[i]])
}

return selected
}

Expand Down
3 changes: 3 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (g *Gossiper) pullRequestHandle(payload []byte, sender *net.UDPAddr) []Pack
if len(kl) != len(vl) {
panic("pullRequestHandle: invalid protocol detected, different key value sizes in the packet")
}
if len(kl) == 0 {
return nil
}

var packets []Packet

Expand Down