feat: add protocol and reconciliation decisions
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
|
||||
)
|
||||
|
||||
var ErrNoCompatibleProtocol = errors.New("no compatible protocol version")
|
||||
|
||||
// SelectProtocol chooses the highest mutually supported minor version.
|
||||
func SelectProtocol(server, client *rvboxv1.ProtocolRange) (*rvboxv1.ProtocolVersion, error) {
|
||||
if server == nil || client == nil || server.Major == 0 || client.Major == 0 || server.Major != client.Major || server.MinMinor > server.MaxMinor || client.MinMinor > client.MaxMinor {
|
||||
return nil, ErrNoCompatibleProtocol
|
||||
}
|
||||
minimum := server.MinMinor
|
||||
if client.MinMinor > minimum {
|
||||
minimum = client.MinMinor
|
||||
}
|
||||
maximum := server.MaxMinor
|
||||
if client.MaxMinor < maximum {
|
||||
maximum = client.MaxMinor
|
||||
}
|
||||
if minimum > maximum {
|
||||
return nil, ErrNoCompatibleProtocol
|
||||
}
|
||||
return &rvboxv1.ProtocolVersion{Major: server.Major, Minor: maximum}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user