Skip to content

Commit

Permalink
release v1.1.0 (#9)
Browse files Browse the repository at this point in the history
* update badges (#4)

* update badges

* fix typo

* Update README.md (#5)

* Update README.zh_CN.md (#6)

* add issue template (#7)

* ResolvableSelector supports parse addr and extractor options (#8)

* all: ResolvableSelector supports parse addr and extractor options

* all: revert service_name_extractor change

---------

Co-authored-by: MengYinlei <[email protected]>
  • Loading branch information
liuzengh and YoungFr authored May 16, 2024
1 parent 3541f03 commit 5a73e60
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 32 deletions.
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/00-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bugs
about: config, server, client, or anything else
title: "affected/package: "
---

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### What version of trpc-go-selector-dsn are you using?


### Does this issue reproduce with the latest release?



### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env

</pre></details>

### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on go.dev/play is best.
-->


### What did you expect to see?



### What did you see instead?
33 changes: 33 additions & 0 deletions .github/ISSUE_TEMPLATE/01-feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: Features
about: config, server, client, or anything else
title: "affected/package: "
---

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### Is your feature request related to a problem?

<!--
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
-->

### What is the solution you'd like?

<!--
A clear and concise description of what you want to happen.
-->

### What alternatives have you considered?

<!--
A clear and concise description of any alternative solutions or features you've considered.
-->

### Could you provide additional context?

<!--
Add any other context or screenshots about the feature request here.
-->
11 changes: 11 additions & 0 deletions .github/ISSUE_TEMPLATE/03-proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
name: Proposals
about: New external API or other notable changes
title: "proposal: affected/package: "
labels: Proposal
---

<!--
Our proposal process is documented here:
https://github.com/trpc-group/trpc/tree/main/proposal
-->
32 changes: 32 additions & 0 deletions .github/workflows/cla.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: "CLA Assistant"
on:
issue_comment:
types: [created]
pull_request_target:
types: [opened, synchronize, reopened]

# explicitly configure permissions, in case your GITHUB_TOKEN workflow permissions are set to read-only in repository settings
permissions:
actions: write
contents: write
pull-requests: write
statuses: write

jobs:
CLAAssistant:
runs-on: ubuntu-latest
steps:
- name: "CLA Assistant"
if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA') || github.event_name == 'pull_request_target'
uses: contributor-assistant/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PERSONAL_ACCESS_TOKEN: ${{ secrets.CLA_DATABASE_ACCESS_TOKEN }}
with:
remote-organization-name: trpc-group
remote-repository-name: cla-database
path-to-signatures: 'signatures/${{ github.event.repository.name }}-${{ github.repository_id }}/cla.json'
path-to-document: 'https://github.com/trpc-group/cla-database/blob/main/Tencent-Contributor-License-Agreement.md'
# branch should not be protected
branch: 'main'
allowlist: bot*
21 changes: 0 additions & 21 deletions .github/workflows/pr.yml

This file was deleted.

60 changes: 60 additions & 0 deletions .github/workflows/prc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Pull Request Check
on:
pull_request:
push:
workflow_dispatch:
permissions:
contents: read
pull-requests: read # Use with `only-new-issues` option.
jobs:
build:
name: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: 1.19
- name: Build
run: go build -v ./...
- name: Test
run: go test -v -coverprofile=coverage.out ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
files: coverage.out
flags: unittests
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: 1.19
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
only-new-issues: true
skip-cache: true # To prevent occasional failure of this action.
typos:
name: typos
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: typos
uses: crate-ci/typos@master
go-apidiff:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v4
with:
go-version: 1.19
- uses: joelanford/go-apidiff@main
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
English | [中文](README.zh_CN.md)

# Data Source Name Selector

English | [简体中文](./README-zh.md)
[![Go Reference](https://pkg.go.dev/badge/github.com/trpc-ecosystem/go-selector-dsn.svg)](https://pkg.go.dev/github.com/trpc-ecosystem/go-selector-dsn)
[![Go Report Card](https://goreportcard.com/badge/trpc.group/trpc-go/trpc-selector-dsn)](https://goreportcard.com/report/trpc.group/trpc-go/trpc-selector-dsn)
[![LICENSE](https://img.shields.io/badge/license-Apache--2.0-green.svg)](https://github.com/trpc-ecosystem/go-selector-dsn/blob/main/LICENSE)
[![Releases](https://img.shields.io/github/release/trpc-ecosystem/go-selector-dsn.svg?style=flat-square)](https://github.com/trpc-ecosystem/go-selector-dsn/releases)
[![Tests](https://github.com/trpc-ecosystem/go-selector-dsn/actions/workflows/prc.yml/badge.svg)](https://github.com/trpc-ecosystem/go-selector-dsn/actions/workflows/prc.yml)
[![Coverage](https://codecov.io/gh/trpc-ecosystem/go-selector-dsn/branch/main/graph/badge.svg)](https://app.codecov.io/gh/trpc-ecosystem/go-selector-dsn/tree/main)

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

Expand All @@ -9,7 +16,7 @@ DSN(Data Source Name) Selector implements a selector for tRPC-Go, which uses the
client: # backend-config for client
service: # backend's config
- name: trpc.dsn.xxx.xxx
target: dsn://user:passwd@tcp(ip:port)/db # select retruns the address after "://"
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
Expand Down
9 changes: 8 additions & 1 deletion README-zh.md → README.zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
[English](README.md) | 中文

# Data Source Name 寻址方式

[English](./README.md) | 简体中文
[![Go Reference](https://pkg.go.dev/badge/github.com/trpc-ecosystem/go-selector-dsn.svg)](https://pkg.go.dev/github.com/trpc-ecosystem/go-selector-dsn)
[![Go Report Card](https://goreportcard.com/badge/trpc.group/trpc-go/trpc-selector-dsn)](https://goreportcard.com/report/trpc.group/trpc-go/trpc-selector-dsn)
[![LICENSE](https://img.shields.io/badge/license-Apache--2.0-green.svg)](https://github.com/trpc-ecosystem/go-selector-dsn/blob/main/LICENSE)
[![Releases](https://img.shields.io/github/release/trpc-ecosystem/go-selector-dsn.svg?style=flat-square)](https://github.com/trpc-ecosystem/go-selector-dsn/releases)
[![Tests](https://github.com/trpc-ecosystem/go-selector-dsn/actions/workflows/prc.yml/badge.svg)](https://github.com/trpc-ecosystem/go-selector-dsn/actions/workflows/prc.yml)
[![Coverage](https://codecov.io/gh/trpc-ecosystem/go-selector-dsn/branch/main/graph/badge.svg)](https://app.codecov.io/gh/trpc-ecosystem/go-selector-dsn/tree/main)

实现了 tRPC-Go 的 selector 接口,将 client 的 target 当做 data source name 来使用,并在 Node 的 Address 中返回

Expand Down
88 changes: 85 additions & 3 deletions dsn_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dsn
import (
"errors"
"fmt"
"net"
"sync"
"time"

Expand All @@ -32,12 +33,27 @@ func init() {
}

// DefaultSelector dsn default selector
var DefaultSelector = &DsnSelector{dsns: make(map[string]*registry.Node)}
var DefaultSelector = NewDsnSelector(false)

// NewDsnSelector creates a new dsn selector.
func NewDsnSelector(keepAddrUnresolved bool) *DsnSelector {
var parseAddr func(network, address string) net.Addr
if keepAddrUnresolved {
parseAddr = func(network, address string) net.Addr {
return unresolvedAddr{network: network, address: address}
}
}
return &DsnSelector{
parseAddr: parseAddr,
dsns: make(map[string]*registry.Node),
}
}

// DsnSelector returns original service name node, with memory cache
type DsnSelector struct {
dsns map[string]*registry.Node
lock sync.RWMutex
parseAddr func(network, address string) net.Addr
dsns map[string]*registry.Node
lock sync.RWMutex
}

// Select selects address from dsn://user:passwd@tcp(ip:port)/db
Expand All @@ -60,6 +76,7 @@ func (s *DsnSelector) Select(serviceName string, opt ...selector.Option) (*regis
node = &registry.Node{
ServiceName: serviceName,
Address: serviceName,
ParseAddr: s.parseAddr,
}
s.dsns[serviceName] = node
return node, nil
Expand All @@ -70,12 +87,36 @@ func (s *DsnSelector) Report(node *registry.Node, cost time.Duration, err error)
return nil
}

// Options are ResolvableSelector options.
type Options struct {
Extractor ServiceNameExtractor
EnableParseAddr bool
}

// Option sets ResolvableSelector options.
type Option func(*Options)

// WithExtractor ...
func WithExtractor(extractor ServiceNameExtractor) Option {
return func(o *Options) {
o.Extractor = extractor
}
}

// WithEnableParseAddr ...
func WithEnableParseAddr(enable bool) Option {
return func(o *Options) {
o.EnableParseAddr = enable
}
}

// ResolvableSelector dsn-selector with address resolver
type ResolvableSelector struct {
dsns map[string]*registry.Node
lock sync.RWMutex
resolverSelectorName string
extractor ServiceNameExtractor
EnableParseAddr bool
}

// ServiceNameExtractor extracts the part of the service name in the dsn, and return the starting position and length
Expand All @@ -98,6 +139,25 @@ func NewResolvableSelector(selectorName string, extractor ServiceNameExtractor)
}
}

// NewResolvableSelectorWithOpts ...
func NewResolvableSelectorWithOpts(selectorName string, opt ...Option) selector.Selector {
opts := &Options{
Extractor: &URIHostExtractor{},
EnableParseAddr: true,
}

for _, o := range opt {
o(opts)
}

return &ResolvableSelector{
dsns: make(map[string]*registry.Node),
resolverSelectorName: selectorName,
extractor: opts.Extractor,
EnableParseAddr: opts.EnableParseAddr,
}
}

// Select selects address from mongodb+polaris://user:passwd@poloars_name/db
func (s *ResolvableSelector) Select(serviceName string, opt ...selector.Option) (*registry.Node, error) {
// resolve serviceName from dsn
Expand Down Expand Up @@ -165,6 +225,13 @@ func (s *ResolvableSelector) dsnRW(address, serviceName string, resolvedNode *re
"resolved": resolvedNode,
},
}

if s.EnableParseAddr {
node.ParseAddr = func(network, address string) net.Addr {
return unresolvedAddr{resolvedNode.Network, resolvedNode.Address}
}
}

s.dsns[address] = node
return node
}
Expand Down Expand Up @@ -193,3 +260,18 @@ func (s *ResolvableSelector) Report(node *registry.Node, cost time.Duration, err

return resolver.Report(resolvedNode, cost, err)
}

type unresolvedAddr struct {
network string
address string
}

// Network returns the unresolved original network.
func (a unresolvedAddr) Network() string {
return a.network
}

// String returns the unresolved original address.
func (a unresolvedAddr) String() string {
return a.address
}
Loading

0 comments on commit 5a73e60

Please sign in to comment.