Documentation
¶
Index ¶
- Constants
- Variables
- func AutoKeepAlive(ctx context.Context, namespace *string, naming string, client *Client, ...) error
- func ParseEndpointPath(path string) (string, string, error)
- type Client
- type DiscoveryAndRegister
- type Endpoint
- func (e *Endpoint) Expired() bool
- func (e *Endpoint) Key() string
- func (e *Endpoint) Marshal() ([]byte, error)
- func (e *Endpoint) PutMetadata(md EndpointMetadata) (err error)
- func (e *Endpoint) SetTTL(ttl uint32)
- func (e *Endpoint) TTL() uint32
- func (e *Endpoint) Value() *Endpoint
- func (e *Endpoint) WithNaming(naming string) string
- type EndpointMetadata
- type KVMetadata
- type ListenCallbackFunc
- type Service
Examples ¶
Constants ¶
View Source
const (
MaxEndpointSize uint64 = 8192
)
Variables ¶
View Source
var ( ErrServiceNotExist = errors.New("sdr: service not existed") ErrDiscoveryHasExist = errors.New("sdr: discovery has existed") ErrShouldDiscoveryFirst = errors.New("sdr: should discovery first") ErrServiceUnreachable = errors.New("sdr: service unreachable") )
View Source
var ( DefaultDialOpts = []grpc.DialOption{ grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultCallOptions(grpc.WaitForReady(true)), } )
View Source
var (
ErrInvalidEndpointPathFormat = errors.New("sdr: ParseEndpointPath invalid endpoint path format")
)
Functions ¶
func AutoKeepAlive ¶
Types ¶
type Client ¶
type Client struct {
Namespace *string
DiscoveryAndRegister
// contains filtered or unexported fields
}
func NewWithClient ¶
func (*Client) Service ¶
Example ¶
package main
import (
"context"
discovery "github.com/RealFax/red-discovery"
)
const (
naming = "pkg.discovery.test"
)
var (
client *discovery.Client
)
func init() {
var err error
if client, err = discovery.New(context.Background(), []string{
"127.0.0.1:5230",
"127.0.0.1:4230",
"127.0.0.1:3230",
}); err != nil {
panic("init sdr client error, cause: " + err.Error())
}
}
func main() {
srv, found := client.Service(naming)
if !found {
// handle service not found error (discovery not called first)
}
conn, err := srv.NextAliveConn()
if err != nil {
// handle error
}
// invoke grpc api
// after the call is completed, conn.Release() should be called to release the connection
conn.Target()
}
Output:
func (*Client) SetNamespace ¶
type DiscoveryAndRegister ¶
type DiscoveryAndRegister interface {
// ReleaseDiscovery cancel the discovery of a Naming.
ReleaseDiscovery(naming string)
// Discovery a Naming, will open a goroutine to achieve continuous discovery of Naming.
Discovery(namespace *string, naming string) error
// Unregister one or more services using an Endpoint ID.
Unregister(namespace *string, naming string, ids ...string) error
// Register one or more services with Naming.
Register(namespace *string, naming string, endpoints ...*Endpoint) error
// UseListener
//
// Monitors whether a Naming is available.
// When ready is true, it means that there are available services for this Naming.
// When it is false, it means that there are no services available under the Naming.
UseListener(naming string, callback ListenCallbackFunc) (string, error)
// DestroyListener cancel listening to Naming based on the ListenerID returned by UseListener.
DestroyListener(naming, listenerID string)
}
func NewDiscoveryAndRegister ¶
type Endpoint ¶
type Endpoint struct {
ID string `json:"id"`
PeerAddress string `json:"peer-addr"`
Metadata jsoniter.RawMessage `json:"metadata,omitempty"`
// contains filtered or unexported fields
}
func NewEndpoint ¶
func ParseEndpoint ¶
func (*Endpoint) PutMetadata ¶
func (e *Endpoint) PutMetadata(md EndpointMetadata) (err error)
func (*Endpoint) WithNaming ¶
type EndpointMetadata ¶
type EndpointMetadata interface {
Entry() (jsoniter.RawMessage, error)
}
type KVMetadata ¶
type KVMetadata[K comparable, V any] struct { // contains filtered or unexported fields }
func NewKVMetadata ¶
func NewKVMetadata[K comparable, V any]() *KVMetadata[K, V]
func NewKVMetadataFromMap ¶
func NewKVMetadataFromMap[K comparable, V any](m map[K]V) *KVMetadata[K, V]
func (*KVMetadata[K, V]) Clone ¶
func (v *KVMetadata[K, V]) Clone(m map[K]V)
func (*KVMetadata[K, V]) Del ¶
func (v *KVMetadata[K, V]) Del(key K)
func (*KVMetadata[K, V]) Entry ¶
func (v *KVMetadata[K, V]) Entry() (jsoniter.RawMessage, error)
func (*KVMetadata[K, V]) Get ¶
func (v *KVMetadata[K, V]) Get(key K) (V, bool)
func (*KVMetadata[K, V]) Map ¶
func (v *KVMetadata[K, V]) Map() map[K]V
func (*KVMetadata[K, V]) Set ¶
func (v *KVMetadata[K, V]) Set(key K, value V)
type ListenCallbackFunc ¶
type ListenCallbackFunc func(ready bool, conn *grpc.ClientConn, wg *sync.WaitGroup)
type Service ¶
type Service interface {
// Naming returns service naming.
Naming() string
// SetNaming set service naming.
SetNaming(naming string)
WithEndpointNaming(endpointID string) string
// Alive returns whether the current service is available.
Alive() bool
// AddEndpoints by endpoints slice.
AddEndpoints(endpoints ...*Endpoint)
// DelEndpoints by id slice.
DelEndpoints(ids ...string)
RangeEndpoints(f func(endpoint *Endpoint) bool)
// AliveConn returns the grpc connection of all service endpoints on internal.
//
// DON'T CLOSE GRPC CONN
AliveConn() map[string]*grpc.ClientConn
// NextAliveConn returns the internal grpc connection through the load balancing algorithm.
//
// DON"T CLOSE GRPC CONN
NextAliveConn() (*grpc.ClientConn, error)
// CloseAliveConn Close internal all grpc conn.
CloseAliveConn()
// DialContext realizes the grpc connect function of internal load balancing through round-robin algorithm.
DialContext(ctx context.Context, opts ...grpc.DialOption) (*grpc.ClientConn, error)
// DialAll connect to all endpoints.
DialAll(ctx context.Context, opts ...grpc.DialOption) (map[string]*grpc.ClientConn, error)
}
func NewService ¶
Source Files
¶
Click to show internal directories.
Click to hide internal directories.