forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support direct scheme on rpc resolver
- Loading branch information
Showing
11 changed files
with
170 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package resolver | ||
|
||
import ( | ||
"strings" | ||
|
||
"google.golang.org/grpc/resolver" | ||
) | ||
|
||
type directBuilder struct{} | ||
|
||
func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( | ||
resolver.Resolver, error) { | ||
var addrs []resolver.Address | ||
endpoints := strings.Split(target.Endpoint, EndpointSep) | ||
|
||
for _, val := range subset(endpoints, subsetSize) { | ||
addrs = append(addrs, resolver.Address{ | ||
Addr: val, | ||
}) | ||
} | ||
cc.UpdateState(resolver.State{ | ||
Addresses: addrs, | ||
}) | ||
|
||
return &nopResolver{cc: cc}, nil | ||
} | ||
|
||
func (d *directBuilder) Scheme() string { | ||
return DirectScheme | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package resolver | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/tal-tech/go-zero/core/discov" | ||
"google.golang.org/grpc/resolver" | ||
) | ||
|
||
type discovBuilder struct{} | ||
|
||
func (d *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( | ||
resolver.Resolver, error) { | ||
hosts := strings.Split(target.Authority, EndpointSep) | ||
sub, err := discov.NewSubscriber(hosts, target.Endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
update := func() { | ||
var addrs []resolver.Address | ||
for _, val := range subset(sub.Values(), subsetSize) { | ||
addrs = append(addrs, resolver.Address{ | ||
Addr: val, | ||
}) | ||
} | ||
cc.UpdateState(resolver.State{ | ||
Addresses: addrs, | ||
}) | ||
} | ||
sub.AddListener(update) | ||
update() | ||
|
||
return &nopResolver{cc: cc}, nil | ||
} | ||
|
||
func (d *discovBuilder) Scheme() string { | ||
return DiscovScheme | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,30 @@ | ||
package resolver | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/tal-tech/go-zero/core/discov" | ||
"google.golang.org/grpc/resolver" | ||
) | ||
import "google.golang.org/grpc/resolver" | ||
|
||
const ( | ||
DirectScheme = "direct" | ||
DiscovScheme = "discov" | ||
EndpointSep = "," | ||
subsetSize = 32 | ||
) | ||
|
||
var builder discovBuilder | ||
|
||
type discovBuilder struct{} | ||
|
||
func (b *discovBuilder) Scheme() string { | ||
return DiscovScheme | ||
} | ||
|
||
func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) ( | ||
resolver.Resolver, error) { | ||
if target.Scheme != DiscovScheme { | ||
return nil, fmt.Errorf("bad scheme: %s", target.Scheme) | ||
} | ||
|
||
hosts := strings.Split(target.Authority, EndpointSep) | ||
sub, err := discov.NewSubscriber(hosts, target.Endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
update := func() { | ||
var addrs []resolver.Address | ||
for _, val := range subset(sub.Values(), subsetSize) { | ||
addrs = append(addrs, resolver.Address{ | ||
Addr: val, | ||
}) | ||
} | ||
cc.UpdateState(resolver.State{ | ||
Addresses: addrs, | ||
}) | ||
} | ||
sub.AddListener(update) | ||
update() | ||
var ( | ||
dirBuilder directBuilder | ||
disBuilder discovBuilder | ||
) | ||
|
||
return &discovResolver{ | ||
cc: cc, | ||
}, nil | ||
func RegisterResolver() { | ||
resolver.Register(&dirBuilder) | ||
resolver.Register(&disBuilder) | ||
} | ||
|
||
type discovResolver struct { | ||
type nopResolver struct { | ||
cc resolver.ClientConn | ||
} | ||
|
||
func (r *discovResolver) Close() { | ||
func (r *nopResolver) Close() { | ||
} | ||
|
||
func (r *discovResolver) ResolveNow(options resolver.ResolveNowOptions) { | ||
} | ||
|
||
func RegisterResolver() { | ||
resolver.Register(&builder) | ||
func (r *nopResolver) ResolveNow(options resolver.ResolveNowOptions) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/tal-tech/go-zero/rpcx/internal/resolver" | ||
) | ||
|
||
func BuildDirectTarget(endpoints []string) string { | ||
return fmt.Sprintf("%s:///%s", resolver.DirectScheme, strings.Join(endpoints, resolver.EndpointSep)) | ||
} | ||
|
||
func BuildDiscovTarget(endpoints []string, key string) string { | ||
return fmt.Sprintf("%s://%s/%s", resolver.DiscovScheme, | ||
strings.Join(endpoints, resolver.EndpointSep), key) | ||
} |
Oops, something went wrong.