summaryrefslogtreecommitdiff
path: root/internal/discovery
diff options
context:
space:
mode:
Diffstat (limited to 'internal/discovery')
-rw-r--r--internal/discovery/discovery.go7
-rw-r--r--internal/discovery/manager.go8
2 files changed, 14 insertions, 1 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go
index b6fef6f..c7eae95 100644
--- a/internal/discovery/discovery.go
+++ b/internal/discovery/discovery.go
@@ -44,6 +44,7 @@ type Organization struct {
KeywordList discotypes.MapOrString `json:"keyword_list,omitempty"`
}
+// Score returns the levenshstein score for a search query `search` on the organizations
func (o *Organization) Score(search string) int {
return levenshtein.DiscoveryScore(search, o.DisplayName, o.KeywordList)
}
@@ -75,7 +76,7 @@ type Server struct {
SupportContact []string `json:"support_contact,omitempty"`
}
-// Matches returns if the search query `str` matches with this server
+// Score returns the score of the search query `str` on this server
func (s *Server) Score(search string) int {
return levenshtein.DiscoveryScore(search, s.DisplayName, s.KeywordList)
}
@@ -404,12 +405,16 @@ func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error)
return &discovery.ServerList, true, nil
}
+// UpdateServers updates the discovery servers to the new version
+// It does this by checking versions
func (discovery *Discovery) UpdateServers(other Discovery) {
if other.ServerList.Version >= discovery.ServerList.Version {
discovery.ServerList = other.ServerList
}
}
+// Copy creates a deep-copy for the discovery struct
+// It does this by marshalling and unmarshalling it as JSON
func (discovery *Discovery) Copy() (Discovery, error) {
var dest Discovery
b, err := json.Marshal(discovery)
diff --git a/internal/discovery/manager.go b/internal/discovery/manager.go
index e71f13a..6a78486 100644
--- a/internal/discovery/manager.go
+++ b/internal/discovery/manager.go
@@ -7,6 +7,8 @@ import (
"codeberg.org/eduVPN/eduvpn-common/internal/log"
)
+// Manager is the discovery struct that is cached
+// with some bookkeeping to avoid race conditions
type Manager struct {
disco *Discovery
@@ -15,6 +17,7 @@ type Manager struct {
wait sync.WaitGroup
}
+// NewManager creates a new Discovery manager
func NewManager(disco *Discovery) *Manager {
return &Manager{disco: disco}
}
@@ -35,6 +38,8 @@ func (m *Manager) unlock(write bool) {
m.mu.RUnlock()
}
+// Discovery gets the cached discovery
+// `write` is true if discovery will be written to
func (m *Manager) Discovery(write bool) (*Discovery, func()) {
if write {
m.wait.Wait()
@@ -45,6 +50,7 @@ func (m *Manager) Discovery(write bool) (*Discovery, func()) {
}
}
+// Cancel aborts the running discovery startup process
func (m *Manager) Cancel() {
if m.cancel != nil {
m.cancel()
@@ -52,6 +58,8 @@ func (m *Manager) Cancel() {
m.wait.Wait()
}
+// Startup handles the discovery process in the background
+// It's called Startup because it's called when the lib is initialised
func (m *Manager) Startup(ctx context.Context, cb func()) {
ctx, cancel := context.WithCancel(ctx)
m.cancel = cancel