add version selector wrapper
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
commit
8408145db8
16
README.md
Normal file
16
README.md
Normal file
@ -0,0 +1,16 @@
|
||||
# Version Wrapper
|
||||
|
||||
The version wrapper is a stateful client wrapper which gives you a ability to select only latest version services. That suitable for easy upgrade running services without downtime.
|
||||
|
||||
## Usage
|
||||
|
||||
Pass in the wrapper when you create your service
|
||||
|
||||
```
|
||||
wrapper := version.NewClientWrapper()
|
||||
|
||||
service := micro.NewService(
|
||||
micro.Name("foo"),
|
||||
micro.WrapClient(wrapper),
|
||||
)
|
||||
```
|
51
version.go
Normal file
51
version.go
Normal file
@ -0,0 +1,51 @@
|
||||
package version
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"github.com/micro/go-micro/client"
|
||||
"github.com/micro/go-micro/client/selector"
|
||||
"github.com/micro/go-micro/registry"
|
||||
)
|
||||
|
||||
// NewClientWrapper is a wrapper which selects only latest versions of services
|
||||
func NewClientWrapper() client.Wrapper {
|
||||
return func(c client.Client) client.Client {
|
||||
return &latestVersionWrapper{
|
||||
Client: c,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type latestVersionWrapper struct {
|
||||
client.Client
|
||||
}
|
||||
|
||||
func (w *latestVersionWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
nOpts := append(opts, client.WithSelectOption(selector.WithFilter(filterLatestVersion())))
|
||||
return w.Client.Call(ctx, req, rsp, nOpts...)
|
||||
}
|
||||
|
||||
func filterLatestVersion() selector.Filter {
|
||||
return func(svcsOld []*registry.Service) []*registry.Service {
|
||||
var svcsNew []*registry.Service
|
||||
versions := make([]string, len(svcsOld))
|
||||
|
||||
for i, svc := range svcsOld {
|
||||
versions[i] = svc.Version
|
||||
}
|
||||
|
||||
sort.Strings(versions)
|
||||
|
||||
gtVersion := versions[len(versions)-1]
|
||||
|
||||
for _, svc := range svcsOld {
|
||||
if svc.Version == gtVersion {
|
||||
svcsNew = append(svcsNew, svc)
|
||||
}
|
||||
}
|
||||
|
||||
return svcsNew
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user