2019-06-07 01:29:24 +03:00
|
|
|
package router
|
|
|
|
|
2019-06-10 01:09:38 +03:00
|
|
|
// LookupPolicy defines query policy
|
|
|
|
type LookupPolicy int
|
2019-06-07 01:29:24 +03:00
|
|
|
|
|
|
|
const (
|
2019-06-10 01:09:38 +03:00
|
|
|
// DiscardNoRoute discards query when no route is found
|
|
|
|
DiscardNoRoute LookupPolicy = iota
|
|
|
|
// ClosestMatch returns closest match to supplied query
|
2019-06-07 01:29:24 +03:00
|
|
|
ClosestMatch
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryOptions allow to define routing table query options
|
|
|
|
type QueryOptions struct {
|
|
|
|
// Route allows to set route options
|
2019-06-10 15:34:23 +03:00
|
|
|
Route *RouteOptions
|
2019-06-07 01:29:24 +03:00
|
|
|
// Service is micro service name
|
|
|
|
Service string
|
|
|
|
// Policy defines query lookup policy
|
2019-06-10 01:09:38 +03:00
|
|
|
Policy LookupPolicy
|
2019-06-10 01:19:56 +03:00
|
|
|
// Count defines max number of results to return
|
|
|
|
Count int
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// RouteOpts allows to set the route query options
|
|
|
|
func RouteOpts(r *RouteOptions) QueryOption {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *QueryOptions) {
|
|
|
|
o.Route = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service allows to set the service name in routing query
|
|
|
|
func Service(s string) QueryOption {
|
|
|
|
return func(o *QueryOptions) {
|
|
|
|
o.Service = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// QueryPolicy allows to define query lookup policy
|
|
|
|
func QueryPolicy(p LookupPolicy) QueryOption {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *QueryOptions) {
|
|
|
|
o.Policy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 01:19:56 +03:00
|
|
|
// ResultCount allows to set max results to return
|
|
|
|
func ResultCount(c int) QueryOption {
|
|
|
|
return func(o *QueryOptions) {
|
|
|
|
o.Count = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 01:29:24 +03:00
|
|
|
// Query defines routing table query
|
|
|
|
type Query interface {
|
|
|
|
// Options returns query options
|
|
|
|
Options() QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type query struct {
|
|
|
|
opts QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQuery creates new query and returns it
|
|
|
|
func NewQuery(opts ...QueryOption) Query {
|
|
|
|
qopts := QueryOptions{}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&qopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &query{
|
|
|
|
opts: qopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns query options
|
|
|
|
func (q *query) Options() QueryOptions {
|
|
|
|
return q.opts
|
|
|
|
}
|