dsn

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2024 License: Apache-2.0, MIT Imports: 8 Imported by: 6

README

English | 中文

Data Source Name Selector

Go Reference Go Report Card LICENSE Releases Tests Coverage

DSN(Data Source Name) Selector implements a selector for tRPC-Go, which uses the client's target as a data source name , and returns it in the Node's Address

client

client:                                            # backend-config for client
  service:                                         # backend's config
    - name: trpc.dsn.xxx.xxx         
      target: dsn://user:passwd@tcp(ip:port)/db      # select returns the address after "://"
    - name: trpc.dsn.xxx.xxx1         
      # dsn+polaris means that the host in target will be resolved by polaris, and the actual address will be replaced 
      # after the host, and the part after "://" will be returned
      # polaris is specified when registering the selector, and can also be other selectors
      target: dsn+polaris://user:passwd@tcp(host)/db
// register selector
func init() {
    // use target as data source name or uri directly
    selector.Register("dsn", dsn.DefaultSelector)

    // selector which supports address resolution, polaris is the name of the address resolution selector
    // dsn.URIHostExtractor{} is the extractor to extract the key of polaris service from target
    selector.Register("dsn+polaris", dsn.NewResolvableSelector("polaris", &dsn.URIHostExtractor{}))
}

Documentation

Overview

Package dsn data layer storage address method, mainly used for mysql mongodb and other database address

Index

Constants

This section is empty.

Variables

View Source
var DefaultSelector = NewDsnSelector(false)

DefaultSelector dsn default selector

View Source
var SeletorName string = "dsn"

SeletorName dsn selector name

Functions

func NewResolvableSelector

func NewResolvableSelector(selectorName string, extractor ServiceNameExtractor) selector.Selector

NewResolvableSelector selector contains address resolution, implemented by other selectors. selectorName is the selector name for address resolution extractor is the func to extract the service name from the dsn, the extracted service name is the parameter of the selector used for address resolution eg: target: mongodb+polaris://user:passwd@poloars_name extractor extract polaris_name from target polaris selector will resolve polaris_name to address

func NewResolvableSelectorWithOpts added in v1.1.0

func NewResolvableSelectorWithOpts(selectorName string, opt ...Option) selector.Selector

NewResolvableSelectorWithOpts ...

Types

type DsnSelector

type DsnSelector struct {
	// contains filtered or unexported fields
}

DsnSelector returns original service name node, with memory cache

func NewDsnSelector added in v1.1.0

func NewDsnSelector(keepAddrUnresolved bool) *DsnSelector

NewDsnSelector creates a new dsn selector.

func (*DsnSelector) Report

func (s *DsnSelector) Report(node *registry.Node, cost time.Duration, err error) error

Report dsn selector no need to report

func (*DsnSelector) Select

func (s *DsnSelector) Select(serviceName string, opt ...selector.Option) (*registry.Node, error)

Select selects address from dsn://user:passwd@tcp(ip:port)/db

type Option added in v1.1.0

type Option func(*Options)

Option sets ResolvableSelector options.

func WithEnableParseAddr added in v1.1.0

func WithEnableParseAddr(enable bool) Option

WithEnableParseAddr ...

func WithExtractor added in v1.1.0

func WithExtractor(extractor ServiceNameExtractor) Option

WithExtractor ...

type Options added in v1.1.0

type Options struct {
	Extractor       ServiceNameExtractor
	EnableParseAddr bool
}

Options are ResolvableSelector options.

type ResolvableSelector

type ResolvableSelector struct {
	EnableParseAddr bool
	// contains filtered or unexported fields
}

ResolvableSelector dsn-selector with address resolver

func (*ResolvableSelector) Report

func (s *ResolvableSelector) Report(node *registry.Node, cost time.Duration, err error) error

Report dsn selector does not need to report, but the embedded selector needs to report. this func is only executed after Select() is successful, so the node returned by Select() needs to be checked before report

func (*ResolvableSelector) Select

func (s *ResolvableSelector) Select(serviceName string, opt ...selector.Option) (*registry.Node, error)

Select selects address from mongodb+polaris://user:passwd@poloars_name/db

type ServiceNameExtractor

type ServiceNameExtractor interface {
	Extract(string) (int, int, error)
}

ServiceNameExtractor extracts the part of the service name in the dsn, and return the starting position and length

type URIHostExtractor

type URIHostExtractor struct {
}

URIHostExtractor extracts host from URI, used for ip resolve(like get ip from polaris), work with ResolvableSelector

func (*URIHostExtractor) Extract

func (e *URIHostExtractor) Extract(uri string) (int, int, error)

Extract extracts host from uri. Note: The uri has been preprocessed, no longer contains the strings preceding :// and ://.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL