Skip to content

Commit

Permalink
Feature/gsvc interface (gogf#1871)
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn authored May 23, 2022
1 parent 6a01275 commit ab50626
Show file tree
Hide file tree
Showing 39 changed files with 669 additions and 500 deletions.
27 changes: 18 additions & 9 deletions contrib/registry/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package etcd

import (
"reflect"
"time"

etcd3 "go.etcd.io/etcd/client/v3"
Expand Down Expand Up @@ -79,26 +80,34 @@ func NewWithClient(client *etcd3.Client, option ...Option) *Registry {
}

// extractResponseToServices extracts etcd watch response context to service list.
func extractResponseToServices(res *etcd3.GetResponse) ([]*gsvc.Service, error) {
func extractResponseToServices(res *etcd3.GetResponse) ([]gsvc.Service, error) {
if res == nil || res.Kvs == nil {
return nil, nil
}
var (
services []*gsvc.Service
services []gsvc.Service
serviceKey string
serviceMap = make(map[string]*gsvc.Service)
serviceMap = make(map[string]*gsvc.LocalService)
)
for _, kv := range res.Kvs {
service, err := gsvc.NewServiceWithKV(kv.Key, kv.Value)
service, err := gsvc.NewServiceWithKV(string(kv.Key), string(kv.Value))
if err != nil {
return services, err
}
if service != nil {
serviceKey = service.KeyWithoutEndpoints()
if s, ok := serviceMap[serviceKey]; ok {
s.Endpoints = append(s.Endpoints, service.Endpoints...)
localService, ok := service.(*gsvc.LocalService)
if !ok {
return nil, gerror.Newf(
`service from "gsvc.NewServiceWithKV" is not "*gsvc.LocalService", but "%s"`,
reflect.TypeOf(service),
)
}
if localService != nil {
serviceKey = localService.GetPrefix()
var localServiceInMap *gsvc.LocalService
if localServiceInMap, ok = serviceMap[serviceKey]; ok {
localServiceInMap.Endpoints = append(localServiceInMap.Endpoints, localService.Endpoints...)
} else {
serviceMap[serviceKey] = service
serviceMap[serviceKey] = localService
services = append(services, service)
}
}
Expand Down
20 changes: 9 additions & 11 deletions contrib/registry/etcd/etcd_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import (
)

// Search is the etcd discovery search function.
func (r *Registry) Search(ctx context.Context, in gsvc.SearchInput) ([]*gsvc.Service, error) {
res, err := r.kv.Get(ctx, in.Key(), etcd3.WithPrefix())
func (r *Registry) Search(ctx context.Context, in gsvc.SearchInput) ([]gsvc.Service, error) {
if in.Prefix == "" && in.Name != "" {
in.Prefix = gsvc.NewServiceWithName(in.Name).GetPrefix()
}

res, err := r.kv.Get(ctx, in.Prefix, etcd3.WithPrefix())
if err != nil {
return nil, err
}
Expand All @@ -25,18 +29,12 @@ func (r *Registry) Search(ctx context.Context, in gsvc.SearchInput) ([]*gsvc.Ser
return nil, err
}
// Service filter.
filteredServices := make([]*gsvc.Service, 0)
filteredServices := make([]gsvc.Service, 0)
for _, v := range services {
if in.Deployment != "" && in.Deployment != v.Deployment {
continue
}
if in.Namespace != "" && in.Namespace != v.Namespace {
continue
}
if in.Name != "" && in.Name != v.Name {
if in.Name != "" && in.Name != v.GetName() {
continue
}
if in.Version != "" && in.Version != v.Version {
if in.Version != "" && in.Version != v.GetVersion() {
continue
}
service := v
Expand Down
19 changes: 9 additions & 10 deletions contrib/registry/etcd/etcd_registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import (
)

// Register implements the gsvc.Register interface.
func (r *Registry) Register(ctx context.Context, service *gsvc.Service) error {
func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Service, error) {
r.lease = etcd3.NewLease(r.client)
grant, err := r.lease.Grant(ctx, int64(r.keepaliveTTL.Seconds()))
if err != nil {
return gerror.Wrapf(err, `etcd grant failed with keepalive ttl "%s"`, r.keepaliveTTL)
return nil, gerror.Wrapf(err, `etcd grant failed with keepalive ttl "%s"`, r.keepaliveTTL)
}
var (
key = service.Key()
value = service.Value()
key = service.GetKey()
value = service.GetValue()
)
_, err = r.client.Put(ctx, key, value, etcd3.WithLease(grant.ID))
if err != nil {
return gerror.Wrapf(
return nil, gerror.Wrapf(
err,
`etcd put failed with key "%s", value "%s", lease "%d"`,
key, value, grant.ID,
Expand All @@ -41,16 +41,15 @@ func (r *Registry) Register(ctx context.Context, service *gsvc.Service) error {
)
keepAliceCh, err := r.client.KeepAlive(context.Background(), grant.ID)
if err != nil {
return err
return nil, err
}
go r.doKeepAlive(grant.ID, keepAliceCh)
service.Separator = gsvc.DefaultSeparator
return nil
return service, nil
}

// Deregister implements the gsvc.Deregister interface.
func (r *Registry) Deregister(ctx context.Context, service *gsvc.Service) error {
_, err := r.client.Delete(ctx, service.Key())
func (r *Registry) Deregister(ctx context.Context, service gsvc.Service) error {
_, err := r.client.Delete(ctx, service.GetKey())
if r.lease != nil {
_ = r.lease.Close()
}
Expand Down
4 changes: 2 additions & 2 deletions contrib/registry/etcd/etcd_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newWatcher(key string, client *etcd3.Client) (*watcher, error) {
}

// Proceed is used to watch the key.
func (w *watcher) Proceed() ([]*gsvc.Service, error) {
func (w *watcher) Proceed() ([]gsvc.Service, error) {
select {
case <-w.ctx.Done():
return nil, w.ctx.Err()
Expand All @@ -58,7 +58,7 @@ func (w *watcher) Close() error {
return w.watcher.Close()
}

func (w *watcher) getServicesByPrefix() ([]*gsvc.Service, error) {
func (w *watcher) getServicesByPrefix() ([]gsvc.Service, error) {
res, err := w.kv.Get(w.ctx, w.key, etcd3.WithPrefix())
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion contrib/registry/etcd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/gogf/gf/v2 v2.0.0
go.etcd.io/etcd/client/v3 v3.5.1
go.etcd.io/etcd/client/v3 v3.5.4
)

replace github.com/gogf/gf/v2 => ../../../
14 changes: 7 additions & 7 deletions contrib/registry/etcd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -189,12 +189,12 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/etcd/api/v3 v3.5.1 h1:v28cktvBq+7vGyJXF8G+rWJmj+1XUmMtqcLnH8hDocM=
go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/client/pkg/v3 v3.5.1 h1:XIQcHCFSG53bJETYeRJtIxdLv2EWRGxcfzR8lSnTH4E=
go.etcd.io/etcd/client/pkg/v3 v3.5.1/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.1 h1:oImGuV5LGKjCqXdjkMHCyWa5OO1gYKCnC/1sgdfj1Uk=
go.etcd.io/etcd/client/v3 v3.5.1/go.mod h1:OnjH4M8OnAotwaB2l9bVgZzRFKru7/ZMoS46OtKyd3Q=
go.etcd.io/etcd/api/v3 v3.5.4 h1:OHVyt3TopwtUQ2GKdd5wu3PmmipR4FTwCqoEjSyRdIc=
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4 h1:lrneYvz923dvC14R54XcA7FXoZ3mlGZAgmwhfm7HqOg=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.4 h1:p83BUL3tAYS0OT/r0qglgc3M1JjhM0diV8DSWAhVXv4=
go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY=
go.opentelemetry.io/otel v1.7.0 h1:Z2lA3Tdch0iDcrhJXDIlC94XE+bxok1F9B+4Lz/lGsM=
go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk=
go.opentelemetry.io/otel/sdk v1.7.0 h1:4OmStpcKVOfvDOgCt7UriAPtKolwIhxpnSNI/yK+1B0=
Expand Down
2 changes: 1 addition & 1 deletion contrib/registry/polaris/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/gogf/gf/v2 v2.0.0
github.com/polarismesh/polaris-go v1.1.0
github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f
)

replace github.com/gogf/gf/v2 => ../../../
Loading

0 comments on commit ab50626

Please sign in to comment.