2021-03-03 11:28:39 +01:00
|
|
|
package sessionresolver
|
|
|
|
|
2022-06-08 22:01:51 +02:00
|
|
|
//
|
|
|
|
// Persistent on-disk state
|
|
|
|
//
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// storekey is the key used by the key value store to store
|
|
|
|
// the state required by this package.
|
|
|
|
const storekey = "sessionresolver.state"
|
|
|
|
|
|
|
|
// resolverinfo contains info about a resolver.
|
|
|
|
type resolverinfo struct {
|
|
|
|
// URL is the URL of a resolver.
|
|
|
|
URL string
|
|
|
|
|
|
|
|
// Score is the score of a resolver.
|
|
|
|
Score float64
|
|
|
|
}
|
|
|
|
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
// ErrNilKVStore indicates that the KVStore is nil.
|
|
|
|
var ErrNilKVStore = errors.New("sessionresolver: kvstore is nil")
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
// readstate reads the resolver state from disk
|
|
|
|
func (r *Resolver) readstate() ([]*resolverinfo, error) {
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
if r.KVStore == nil {
|
|
|
|
return nil, ErrNilKVStore
|
|
|
|
}
|
|
|
|
data, err := r.KVStore.Get(storekey)
|
2021-03-03 11:28:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ri []*resolverinfo
|
2022-06-08 22:01:51 +02:00
|
|
|
if err := r.codec().Decode(data, &ri); err != nil {
|
2021-03-03 11:28:39 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ri, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// errNoEntries indicates that no entry remained after we pruned
|
|
|
|
// all the available entries in readstateandprune.
|
|
|
|
var errNoEntries = errors.New("sessionresolver: no available entries")
|
|
|
|
|
|
|
|
// readstateandprune reads the state from disk and removes all the
|
|
|
|
// entries that we don't actually support.
|
|
|
|
func (r *Resolver) readstateandprune() ([]*resolverinfo, error) {
|
|
|
|
ri, err := r.readstate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out []*resolverinfo
|
|
|
|
for _, e := range ri {
|
|
|
|
if _, found := allbyurl[e.URL]; !found {
|
|
|
|
continue // we don't support this specific entry
|
|
|
|
}
|
|
|
|
out = append(out, e)
|
|
|
|
}
|
|
|
|
if len(out) <= 0 {
|
|
|
|
return nil, errNoEntries
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortstate sorts the state by descending score
|
|
|
|
func sortstate(ri []*resolverinfo) {
|
|
|
|
sort.SliceStable(ri, func(i, j int) bool {
|
|
|
|
return ri[i].Score >= ri[j].Score
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// readstatedefault reads the state from disk and merges the state
|
|
|
|
// so that all supported entries are represented.
|
|
|
|
func (r *Resolver) readstatedefault() []*resolverinfo {
|
|
|
|
ri, _ := r.readstateandprune()
|
|
|
|
here := make(map[string]bool)
|
|
|
|
for _, e := range ri {
|
|
|
|
here[e.URL] = true // record what we already have
|
|
|
|
}
|
|
|
|
for _, e := range allmakers {
|
|
|
|
if _, found := here[e.url]; found {
|
|
|
|
continue // already here so no need to add
|
|
|
|
}
|
|
|
|
ri = append(ri, &resolverinfo{
|
|
|
|
URL: e.url,
|
|
|
|
Score: e.score,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sortstate(ri)
|
|
|
|
return ri
|
|
|
|
}
|
|
|
|
|
2022-06-08 22:01:51 +02:00
|
|
|
// writestate writes the state to the kvstore.
|
2021-03-03 11:28:39 +01:00
|
|
|
func (r *Resolver) writestate(ri []*resolverinfo) error {
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
if r.KVStore == nil {
|
|
|
|
return ErrNilKVStore
|
|
|
|
}
|
2022-06-08 22:01:51 +02:00
|
|
|
data, err := r.codec().Encode(ri)
|
2021-03-03 11:28:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
return r.KVStore.Set(storekey, data)
|
2021-03-03 11:28:39 +01:00
|
|
|
}
|