Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
818a0e6356 | |||
56e02ec463 | |||
6ca851401d | |||
bd8216b397 | |||
2b13b3f128 | |||
9957380b6d | |||
e10f8c0fa0 | |||
45252fe4a6 | |||
faad082efe | |||
8ab35cbd9b | |||
ad58ab6943 | |||
0e97049e1d | |||
edb0bbf9cf | |||
|
1b01bd22a6 | ||
2fbaa26f0f | |||
35d3e4b332 | |||
|
e98a93d530 | ||
e3545532e8 | |||
09653c2fb2 | |||
70adfeab0d | |||
a45b672c98 | |||
4509323cae | |||
b3f4c670d5 |
7
.github/dependabot.yml
vendored
7
.github/dependabot.yml
vendored
@@ -11,9 +11,16 @@ updates:
|
|||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
|
commit-message:
|
||||||
|
prefix: "chore"
|
||||||
|
include: "scope"
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
# Maintain dependencies for Golang
|
||||||
- package-ecosystem: "gomod"
|
- package-ecosystem: "gomod"
|
||||||
directory: "/"
|
directory: "/"
|
||||||
schedule:
|
schedule:
|
||||||
interval: "daily"
|
interval: "daily"
|
||||||
|
commit-message:
|
||||||
|
prefix: "chore"
|
||||||
|
include: "scope"
|
||||||
|
|
||||||
|
75
.github/workflows/codeql-analysis.yml
vendored
Normal file
75
.github/workflows/codeql-analysis.yml
vendored
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
# For most projects, this workflow file will not need changing; you simply need
|
||||||
|
# to commit it to your repository.
|
||||||
|
#
|
||||||
|
# You may wish to alter this file to override the set of languages analyzed,
|
||||||
|
# or to provide custom queries or build logic.
|
||||||
|
#
|
||||||
|
# ******** NOTE ********
|
||||||
|
# We have attempted to detect the languages in your repository. Please check
|
||||||
|
# the `language` matrix defined below to confirm you have the correct set of
|
||||||
|
# supported CodeQL languages.
|
||||||
|
#
|
||||||
|
name: "CodeQL"
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["prbuild"]
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
# The branches below must be a subset of the branches above
|
||||||
|
branches: [ master ]
|
||||||
|
schedule:
|
||||||
|
- cron: '34 1 * * 0'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
analyze:
|
||||||
|
name: Analyze
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
actions: read
|
||||||
|
contents: read
|
||||||
|
security-events: write
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
language: [ 'go' ]
|
||||||
|
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||||
|
# Learn more:
|
||||||
|
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# Initializes the CodeQL tools for scanning.
|
||||||
|
- name: Initialize CodeQL
|
||||||
|
uses: github/codeql-action/init@v1
|
||||||
|
with:
|
||||||
|
languages: ${{ matrix.language }}
|
||||||
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
|
# By default, queries listed here will override any specified in a config file.
|
||||||
|
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||||
|
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||||
|
|
||||||
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
|
- name: Autobuild
|
||||||
|
uses: github/codeql-action/autobuild@v1
|
||||||
|
|
||||||
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
|
# 📚 https://git.io/JvXDl
|
||||||
|
|
||||||
|
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||||
|
# and modify them (or add more) to build your code if your project
|
||||||
|
# uses a compiled language
|
||||||
|
|
||||||
|
#- run: |
|
||||||
|
# make bootstrap
|
||||||
|
# make release
|
||||||
|
|
||||||
|
- name: Perform CodeQL Analysis
|
||||||
|
uses: github/codeql-action/analyze@v1
|
66
.github/workflows/dependabot-automerge.yml
vendored
Normal file
66
.github/workflows/dependabot-automerge.yml
vendored
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
name: "prautomerge"
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["prbuild"]
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Dependabot-Automerge:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
# Contains workaround to execute if dependabot updates the PR by checking for the base branch in the linked PR
|
||||||
|
# The the github.event.workflow_run.event value is 'push' and not 'pull_request'
|
||||||
|
# dont work with multiple workflows when last returns success
|
||||||
|
if: >-
|
||||||
|
github.event.workflow_run.conclusion == 'success'
|
||||||
|
&& github.actor == 'dependabot[bot]'
|
||||||
|
&& github.event.sender.login == 'dependabot[bot]'
|
||||||
|
&& github.event.sender.type == 'Bot'
|
||||||
|
&& (github.event.workflow_run.event == 'pull_request'
|
||||||
|
|| (github.event.workflow_run.event == 'push' && github.event.workflow_run.pull_requests[0].base.ref == github.event.repository.default_branch ))
|
||||||
|
steps:
|
||||||
|
- name: Approve Changes and Merge changes if label 'dependencies' is set
|
||||||
|
uses: actions/github-script@v4
|
||||||
|
with:
|
||||||
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
script: |
|
||||||
|
console.log(context.payload.workflow_run);
|
||||||
|
|
||||||
|
var labelNames = await github.paginate(
|
||||||
|
github.issues.listLabelsOnIssue,
|
||||||
|
{
|
||||||
|
repo: context.repo.repo,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
issue_number: context.payload.workflow_run.pull_requests[0].number,
|
||||||
|
},
|
||||||
|
(response) => response.data.map(
|
||||||
|
(label) => label.name
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(labelNames);
|
||||||
|
|
||||||
|
if (labelNames.includes('dependencies')) {
|
||||||
|
console.log('Found label');
|
||||||
|
|
||||||
|
await github.pulls.createReview({
|
||||||
|
repo: context.repo.repo,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
pull_number: context.payload.workflow_run.pull_requests[0].number,
|
||||||
|
event: 'APPROVE'
|
||||||
|
});
|
||||||
|
console.log('Approved PR');
|
||||||
|
|
||||||
|
await github.pulls.merge({
|
||||||
|
repo: context.repo.repo,
|
||||||
|
owner: context.repo.owner,
|
||||||
|
pull_number: context.payload.workflow_run.pull_requests[0].number,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Merged PR');
|
||||||
|
}
|
15
SECURITY.md
Normal file
15
SECURITY.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Security Policy
|
||||||
|
|
||||||
|
## Supported Versions
|
||||||
|
|
||||||
|
Use this section to tell people about which versions of your project are
|
||||||
|
currently being supported with security updates.
|
||||||
|
|
||||||
|
| Version | Supported |
|
||||||
|
| ------- | ------------------ |
|
||||||
|
| 3.7.x | :white_check_mark: |
|
||||||
|
| < 3.7.0 | :x: |
|
||||||
|
|
||||||
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
If you find any issue, please create github issue in this repo
|
@@ -55,7 +55,7 @@ type Auth interface {
|
|||||||
type Account struct {
|
type Account struct {
|
||||||
// Metadata any other associated metadata
|
// Metadata any other associated metadata
|
||||||
Metadata metadata.Metadata `json:"metadata"`
|
Metadata metadata.Metadata `json:"metadata"`
|
||||||
// ID of the account e.g. email or uuid
|
// ID of the account e.g. email or id
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
// Type of the account, e.g. service
|
// Type of the account, e.g. service
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/google/uuid"
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
type noopAuth struct {
|
type noopAuth struct {
|
||||||
@@ -61,11 +61,11 @@ func (n *noopAuth) Verify(acc *Account, res *Resource, opts ...VerifyOption) err
|
|||||||
|
|
||||||
// Inspect a token
|
// Inspect a token
|
||||||
func (n *noopAuth) Inspect(token string) (*Account, error) {
|
func (n *noopAuth) Inspect(token string) (*Account, error) {
|
||||||
uid, err := uuid.NewRandom()
|
id, err := id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Account{ID: uid.String(), Issuer: n.Options().Issuer}, nil
|
return &Account{ID: id, Issuer: n.Options().Issuer}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token generation using an account id and secret
|
// Token generation using an account id and secret
|
||||||
|
@@ -4,10 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
maddr "github.com/unistack-org/micro/v3/util/addr"
|
maddr "github.com/unistack-org/micro/v3/util/addr"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
mnet "github.com/unistack-org/micro/v3/util/net"
|
mnet "github.com/unistack-org/micro/v3/util/net"
|
||||||
"github.com/unistack-org/micro/v3/util/rand"
|
"github.com/unistack-org/micro/v3/util/rand"
|
||||||
)
|
)
|
||||||
@@ -224,7 +224,7 @@ func (m *memoryBroker) BatchSubscribe(ctx context.Context, topic string, handler
|
|||||||
}
|
}
|
||||||
m.RUnlock()
|
m.RUnlock()
|
||||||
|
|
||||||
id, err := uuid.NewRandom()
|
sid, err := id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -233,7 +233,7 @@ func (m *memoryBroker) BatchSubscribe(ctx context.Context, topic string, handler
|
|||||||
|
|
||||||
sub := &memorySubscriber{
|
sub := &memorySubscriber{
|
||||||
exit: make(chan bool, 1),
|
exit: make(chan bool, 1),
|
||||||
id: id.String(),
|
id: sid,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
batchhandler: handler,
|
batchhandler: handler,
|
||||||
opts: options,
|
opts: options,
|
||||||
@@ -269,7 +269,7 @@ func (m *memoryBroker) Subscribe(ctx context.Context, topic string, handler Hand
|
|||||||
}
|
}
|
||||||
m.RUnlock()
|
m.RUnlock()
|
||||||
|
|
||||||
id, err := uuid.NewRandom()
|
sid, err := id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -278,7 +278,7 @@ func (m *memoryBroker) Subscribe(ctx context.Context, topic string, handler Hand
|
|||||||
|
|
||||||
sub := &memorySubscriber{
|
sub := &memorySubscriber{
|
||||||
exit: make(chan bool, 1),
|
exit: make(chan bool, 1),
|
||||||
id: id.String(),
|
id: sid,
|
||||||
topic: topic,
|
topic: topic,
|
||||||
handler: handler,
|
handler: handler,
|
||||||
opts: options,
|
opts: options,
|
||||||
|
@@ -41,11 +41,11 @@ type MessageType int
|
|||||||
// connection. ReadBody may be called with a nil argument to force the
|
// connection. ReadBody may be called with a nil argument to force the
|
||||||
// body to be read and discarded.
|
// body to be read and discarded.
|
||||||
type Codec interface {
|
type Codec interface {
|
||||||
ReadHeader(io.Reader, *Message, MessageType) error
|
ReadHeader(r io.Reader, m *Message, mt MessageType) error
|
||||||
ReadBody(io.Reader, interface{}) error
|
ReadBody(r io.Reader, v interface{}) error
|
||||||
Write(io.Writer, *Message, interface{}) error
|
Write(w io.Writer, m *Message, v interface{}) error
|
||||||
Marshal(interface{}) ([]byte, error)
|
Marshal(v interface{}, opts ...Option) ([]byte, error)
|
||||||
Unmarshal([]byte, interface{}) error
|
Unmarshal(b []byte, v interface{}, opts ...Option) error
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
34
codec/context.go
Normal file
34
codec/context.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package codec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type codecKey struct{}
|
||||||
|
|
||||||
|
// FromContext returns codec from context
|
||||||
|
func FromContext(ctx context.Context) (Codec, bool) {
|
||||||
|
if ctx == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
c, ok := ctx.Value(codecKey{}).(Codec)
|
||||||
|
return c, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewContext put codec in context
|
||||||
|
func NewContext(ctx context.Context, c Codec) context.Context {
|
||||||
|
if ctx == nil {
|
||||||
|
ctx = context.Background()
|
||||||
|
}
|
||||||
|
return context.WithValue(ctx, codecKey{}, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetOption returns a function to setup a context with given value
|
||||||
|
func SetOption(k, v interface{}) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
if o.Context == nil {
|
||||||
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, k, v)
|
||||||
|
}
|
||||||
|
}
|
@@ -5,7 +5,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
type noopCodec struct{}
|
type noopCodec struct {
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
|
||||||
func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error {
|
func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error {
|
||||||
return nil
|
return nil
|
||||||
@@ -69,11 +71,11 @@ func (c *noopCodec) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewCodec returns new noop codec
|
// NewCodec returns new noop codec
|
||||||
func NewCodec() Codec {
|
func NewCodec(opts ...Option) Codec {
|
||||||
return &noopCodec{}
|
return &noopCodec{opts: NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *noopCodec) Marshal(v interface{}) ([]byte, error) {
|
func (c *noopCodec) Marshal(v interface{}, opts ...Option) ([]byte, error) {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -96,7 +98,7 @@ func (c *noopCodec) Marshal(v interface{}) ([]byte, error) {
|
|||||||
return json.Marshal(v)
|
return json.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *noopCodec) Unmarshal(d []byte, v interface{}) error {
|
func (c *noopCodec) Unmarshal(d []byte, v interface{}, opts ...Option) error {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
package codec
|
package codec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/meter"
|
"github.com/unistack-org/micro/v3/meter"
|
||||||
"github.com/unistack-org/micro/v3/tracer"
|
"github.com/unistack-org/micro/v3/tracer"
|
||||||
@@ -19,6 +21,10 @@ type Options struct {
|
|||||||
Tracer tracer.Tracer
|
Tracer tracer.Tracer
|
||||||
// MaxMsgSize specifies max messages size that reads by codec
|
// MaxMsgSize specifies max messages size that reads by codec
|
||||||
MaxMsgSize int
|
MaxMsgSize int
|
||||||
|
// TagName specifies tag name in struct to control codec
|
||||||
|
TagName string
|
||||||
|
// Context stores additional codec options
|
||||||
|
Context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxMsgSize sets the max message size
|
// MaxMsgSize sets the max message size
|
||||||
@@ -28,6 +34,13 @@ func MaxMsgSize(n int) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TagName sets the codec tag name in struct
|
||||||
|
func TagName(n string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.TagName = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Logger sets the logger
|
// Logger sets the logger
|
||||||
func Logger(l logger.Logger) Option {
|
func Logger(l logger.Logger) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
@@ -52,10 +65,12 @@ func Meter(m meter.Meter) Option {
|
|||||||
// NewOptions returns new options
|
// NewOptions returns new options
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
|
Context: context.Background(),
|
||||||
Logger: logger.DefaultLogger,
|
Logger: logger.DefaultLogger,
|
||||||
Meter: meter.DefaultMeter,
|
Meter: meter.DefaultMeter,
|
||||||
Tracer: tracer.DefaultTracer,
|
Tracer: tracer.DefaultTracer,
|
||||||
MaxMsgSize: DefaultMaxMsgSize,
|
MaxMsgSize: DefaultMaxMsgSize,
|
||||||
|
TagName: DefaultTagName,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
|
@@ -37,7 +37,7 @@ var (
|
|||||||
|
|
||||||
// Error type
|
// Error type
|
||||||
type Error struct {
|
type Error struct {
|
||||||
// Id holds error id or service, usually someting like my_service or uuid
|
// Id holds error id or service, usually someting like my_service or id
|
||||||
Id string
|
Id string
|
||||||
// Detail holds some useful details about error
|
// Detail holds some useful details about error
|
||||||
Detail string
|
Detail string
|
||||||
|
@@ -6,13 +6,13 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/silas/dag"
|
"github.com/silas/dag"
|
||||||
"github.com/unistack-org/micro/v3/client"
|
"github.com/unistack-org/micro/v3/client"
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/store"
|
"github.com/unistack-org/micro/v3/store"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
type microFlow struct {
|
type microFlow struct {
|
||||||
@@ -149,18 +149,18 @@ func (w *microWorkflow) getSteps(start string, reverse bool) ([][]Step, error) {
|
|||||||
return steps, nil
|
return steps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *microWorkflow) Abort(ctx context.Context, eid string) error {
|
func (w *microWorkflow) Abort(ctx context.Context, id string) error {
|
||||||
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", eid))
|
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", id))
|
||||||
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusAborted.String())})
|
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusAborted.String())})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *microWorkflow) Suspend(ctx context.Context, eid string) error {
|
func (w *microWorkflow) Suspend(ctx context.Context, id string) error {
|
||||||
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", eid))
|
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", id))
|
||||||
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusSuspend.String())})
|
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusSuspend.String())})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *microWorkflow) Resume(ctx context.Context, eid string) error {
|
func (w *microWorkflow) Resume(ctx context.Context, id string) error {
|
||||||
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", eid))
|
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", id))
|
||||||
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusRunning.String())})
|
return workflowStore.Write(ctx, "status", &codec.Frame{Data: []byte(StatusRunning.String())})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,11 +176,10 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
}
|
}
|
||||||
w.Unlock()
|
w.Unlock()
|
||||||
|
|
||||||
uid, err := uuid.NewRandom()
|
eid, err := id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
eid := uid.String()
|
|
||||||
|
|
||||||
stepStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("steps", eid))
|
stepStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("steps", eid))
|
||||||
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", eid))
|
workflowStore := store.NewNamespaceStore(w.opts.Store, filepath.Join("workflows", eid))
|
||||||
@@ -330,7 +329,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
close(cherr)
|
close(cherr)
|
||||||
case <-chstatus:
|
case <-chstatus:
|
||||||
close(chstatus)
|
close(chstatus)
|
||||||
return uid.String(), nil
|
return eid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
@@ -351,7 +350,7 @@ func (w *microWorkflow) Execute(ctx context.Context, req *Message, opts ...Execu
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
return uid.String(), err
|
return eid, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFlow(opts ...Option) Flow {
|
func NewFlow(opts ...Option) Flow {
|
||||||
|
@@ -116,11 +116,11 @@ type Workflow interface {
|
|||||||
// Steps returns steps slice where parallel steps returned on the same level
|
// Steps returns steps slice where parallel steps returned on the same level
|
||||||
Steps() ([][]Step, error)
|
Steps() ([][]Step, error)
|
||||||
// Suspend suspends execution
|
// Suspend suspends execution
|
||||||
Suspend(ctx context.Context, eid string) error
|
Suspend(ctx context.Context, id string) error
|
||||||
// Resume resumes execution
|
// Resume resumes execution
|
||||||
Resume(ctx context.Context, eid string) error
|
Resume(ctx context.Context, id string) error
|
||||||
// Abort abort execution
|
// Abort abort execution
|
||||||
Abort(ctx context.Context, eid string) error
|
Abort(ctx context.Context, id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flow the base interface to interact with workflows
|
// Flow the base interface to interact with workflows
|
||||||
|
4
go.mod
4
go.mod
@@ -4,10 +4,10 @@ go 1.16
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ef-ds/deque v1.0.4
|
github.com/ef-ds/deque v1.0.4
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible
|
github.com/golang-jwt/jwt/v4 v4.0.0
|
||||||
github.com/google/uuid v1.3.0
|
|
||||||
github.com/imdario/mergo v0.3.12
|
github.com/imdario/mergo v0.3.12
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34
|
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34
|
||||||
|
github.com/unistack-org/micro-proto v0.0.9
|
||||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed
|
golang.org/x/net v0.0.0-20210510120150-4163338589ed
|
||||||
)
|
)
|
||||||
|
16
go.sum
16
go.sum
@@ -1,15 +1,18 @@
|
|||||||
github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI=
|
github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI=
|
||||||
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
github.com/golang-jwt/jwt/v4 v4.0.0 h1:RAqyYixv1p7uEnocuy8P1nru5wprCh/MH2BIlW5z5/o=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
|
||||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
|
||||||
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34 h1:vBfVmA5mZhsQa2jr1FOL9nfA37N/jnbBmi5XUfviVTI=
|
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34 h1:vBfVmA5mZhsQa2jr1FOL9nfA37N/jnbBmi5XUfviVTI=
|
||||||
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||||
|
github.com/unistack-org/micro-proto v0.0.9 h1:KrWLS4FUX7UAWNAilQf70uad6ZPf/0EudeddCXllRVc=
|
||||||
|
github.com/unistack-org/micro-proto v0.0.9/go.mod h1:Cckwmzd89gvS7ThxzZp9kQR/EOdksFQcsTAtDDyKwrg=
|
||||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
|
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
|
||||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
@@ -17,6 +20,11 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
|
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||||
|
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
@@ -11,15 +11,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
lvl, err := GetLevel(os.Getenv("MICRO_LOG_LEVEL"))
|
|
||||||
if err != nil {
|
|
||||||
lvl = InfoLevel
|
|
||||||
}
|
|
||||||
|
|
||||||
DefaultLogger = NewLogger(WithLevel(lvl))
|
|
||||||
}
|
|
||||||
|
|
||||||
type defaultLogger struct {
|
type defaultLogger struct {
|
||||||
enc *json.Encoder
|
enc *json.Encoder
|
||||||
opts Options
|
opts Options
|
||||||
@@ -48,6 +39,28 @@ func (l *defaultLogger) String() string {
|
|||||||
return "micro"
|
return "micro"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *defaultLogger) Clone(opts ...Option) Logger {
|
||||||
|
newopts := NewOptions(opts...)
|
||||||
|
oldopts := l.opts
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&newopts)
|
||||||
|
o(&oldopts)
|
||||||
|
}
|
||||||
|
|
||||||
|
oldopts.Wrappers = newopts.Wrappers
|
||||||
|
l.Lock()
|
||||||
|
cl := &defaultLogger{opts: oldopts, logFunc: l.logFunc, logfFunc: l.logfFunc}
|
||||||
|
l.Unlock()
|
||||||
|
|
||||||
|
// wrap the Log func
|
||||||
|
for i := len(newopts.Wrappers); i > 0; i-- {
|
||||||
|
cl.logFunc = newopts.Wrappers[i-1].Log(cl.logFunc)
|
||||||
|
cl.logfFunc = newopts.Wrappers[i-1].Logf(cl.logfFunc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cl
|
||||||
|
}
|
||||||
|
|
||||||
func (l *defaultLogger) V(level Level) bool {
|
func (l *defaultLogger) V(level Level) bool {
|
||||||
l.RLock()
|
l.RLock()
|
||||||
ok := l.opts.Level.Enabled(level)
|
ok := l.opts.Level.Enabled(level)
|
||||||
@@ -55,6 +68,12 @@ func (l *defaultLogger) V(level Level) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *defaultLogger) Level(level Level) {
|
||||||
|
l.Lock()
|
||||||
|
l.opts.Level = level
|
||||||
|
l.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (l *defaultLogger) Fields(fields ...interface{}) Logger {
|
func (l *defaultLogger) Fields(fields ...interface{}) Logger {
|
||||||
nl := &defaultLogger{opts: l.opts, enc: l.enc}
|
nl := &defaultLogger{opts: l.opts, enc: l.enc}
|
||||||
if len(fields) == 0 {
|
if len(fields) == 0 {
|
||||||
|
@@ -1,24 +1,20 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Level means logger level
|
// Level means logger level
|
||||||
type Level int8
|
type Level int8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// TraceLevel level. Designates finer-grained informational events than the Debug.
|
// TraceLevel level usually used to find bugs, very verbose
|
||||||
TraceLevel Level = iota - 2
|
TraceLevel Level = iota - 2
|
||||||
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
// DebugLevel level used only when enabled debugging
|
||||||
DebugLevel
|
DebugLevel
|
||||||
// InfoLevel level. General operational entries about what's going on inside the application.
|
// InfoLevel level used for general info about what's going on inside the application
|
||||||
InfoLevel
|
InfoLevel
|
||||||
// WarnLevel level. Non-critical entries that deserve eyes.
|
// WarnLevel level used for non-critical entries
|
||||||
WarnLevel
|
WarnLevel
|
||||||
// ErrorLevel level. Used for errors that should definitely be noted.
|
// ErrorLevel level used for errors that should definitely be noted
|
||||||
ErrorLevel
|
ErrorLevel
|
||||||
// FatalLevel level. Logs and then calls `os.Exit(1)`. highest level of severity.
|
// FatalLevel level used for critical errors and then calls `os.Exit(1)`
|
||||||
FatalLevel
|
FatalLevel
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,7 +34,7 @@ func (l Level) String() string {
|
|||||||
case FatalLevel:
|
case FatalLevel:
|
||||||
return "fatal"
|
return "fatal"
|
||||||
}
|
}
|
||||||
return ""
|
return "info"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enabled returns true if the given level is at or above this level.
|
// Enabled returns true if the given level is at or above this level.
|
||||||
@@ -46,22 +42,22 @@ func (l Level) Enabled(lvl Level) bool {
|
|||||||
return lvl >= l
|
return lvl >= l
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLevel converts a level string into a logger Level value.
|
// ParseLevel converts a level string into a logger Level value.
|
||||||
// returns an error if the input string does not match known values.
|
// returns an InfoLevel if the input string does not match known values.
|
||||||
func GetLevel(levelStr string) (Level, error) {
|
func ParseLevel(lvl string) Level {
|
||||||
switch levelStr {
|
switch lvl {
|
||||||
case TraceLevel.String():
|
case TraceLevel.String():
|
||||||
return TraceLevel, nil
|
return TraceLevel
|
||||||
case DebugLevel.String():
|
case DebugLevel.String():
|
||||||
return DebugLevel, nil
|
return DebugLevel
|
||||||
case InfoLevel.String():
|
case InfoLevel.String():
|
||||||
return InfoLevel, nil
|
return InfoLevel
|
||||||
case WarnLevel.String():
|
case WarnLevel.String():
|
||||||
return WarnLevel, nil
|
return WarnLevel
|
||||||
case ErrorLevel.String():
|
case ErrorLevel.String():
|
||||||
return ErrorLevel, nil
|
return ErrorLevel
|
||||||
case FatalLevel.String():
|
case FatalLevel.String():
|
||||||
return FatalLevel, nil
|
return FatalLevel
|
||||||
}
|
}
|
||||||
return InfoLevel, fmt.Errorf("unknown Level String: '%s', use InfoLevel", levelStr)
|
return InfoLevel
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,14 @@
|
|||||||
// Package logger provides a log interface
|
// Package logger provides a log interface
|
||||||
package logger
|
package logger
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultLogger variable
|
// DefaultLogger variable
|
||||||
DefaultLogger Logger = NewLogger()
|
DefaultLogger Logger = NewLogger(WithLevel(ParseLevel(os.Getenv("MICRO_LOG_LEVEL"))))
|
||||||
// DefaultLevel used by logger
|
// DefaultLevel used by logger
|
||||||
DefaultLevel Level = InfoLevel
|
DefaultLevel Level = InfoLevel
|
||||||
// DefaultCallerSkipCount used by logger
|
// DefaultCallerSkipCount used by logger
|
||||||
@@ -16,8 +19,12 @@ var (
|
|||||||
type Logger interface {
|
type Logger interface {
|
||||||
// Init initialises options
|
// Init initialises options
|
||||||
Init(opts ...Option) error
|
Init(opts ...Option) error
|
||||||
|
// Clone create logger copy with new options
|
||||||
|
Clone(opts ...Option) Logger
|
||||||
// V compare provided verbosity level with current log level
|
// V compare provided verbosity level with current log level
|
||||||
V(level Level) bool
|
V(level Level) bool
|
||||||
|
// Level sets the log level for logger
|
||||||
|
Level(level Level)
|
||||||
// The Logger options
|
// The Logger options
|
||||||
Options() Options
|
Options() Options
|
||||||
// Fields set fields to always be logged with keyval pairs
|
// Fields set fields to always be logged with keyval pairs
|
||||||
|
@@ -7,6 +7,27 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestClone(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
buf := bytes.NewBuffer(nil)
|
||||||
|
l := NewLogger(WithLevel(TraceLevel), WithOutput(buf))
|
||||||
|
if err := l.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
nl := l.Clone(WithLevel(ErrorLevel))
|
||||||
|
if err := nl.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
nl.Info(ctx, "info message")
|
||||||
|
if len(buf.Bytes()) != 0 {
|
||||||
|
t.Fatal("message must not be logged")
|
||||||
|
}
|
||||||
|
l.Info(ctx, "info message")
|
||||||
|
if len(buf.Bytes()) == 0 {
|
||||||
|
t.Fatal("message must be logged")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRedirectStdLogger(t *testing.T) {
|
func TestRedirectStdLogger(t *testing.T) {
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
l := NewLogger(WithLevel(TraceLevel), WithOutput(buf))
|
l := NewLogger(WithLevel(TraceLevel), WithOutput(buf))
|
||||||
|
@@ -20,9 +20,7 @@ type Wrapper interface {
|
|||||||
Logf(LogfFunc) LogfFunc
|
Logf(LogfFunc) LogfFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var _ Logger = &OmitLogger{}
|
||||||
_ Logger = &OmitLogger{}
|
|
||||||
)
|
|
||||||
|
|
||||||
type OmitLogger struct {
|
type OmitLogger struct {
|
||||||
l Logger
|
l Logger
|
||||||
@@ -40,6 +38,14 @@ func (w *OmitLogger) V(level Level) bool {
|
|||||||
return w.l.V(level)
|
return w.l.V(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *OmitLogger) Level(level Level) {
|
||||||
|
w.l.Level(level)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *OmitLogger) Clone(opts ...Option) Logger {
|
||||||
|
return w.l.Clone(opts...)
|
||||||
|
}
|
||||||
|
|
||||||
func (w *OmitLogger) Options() Options {
|
func (w *OmitLogger) Options() Options {
|
||||||
return w.l.Options()
|
return w.l.Options()
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,8 @@
|
|||||||
package meter
|
package meter
|
||||||
|
|
||||||
//go:generate protoc -I./handler -I../ -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/unistack-org/micro-proto@v0.0.1 --micro_out=components=micro|http|server,standalone=false,debug=true,paths=source_relative:./handler handler/handler.proto
|
//go:generate sh -c "protoc -I./handler -I../ -I$(go list -f '{{ .Dir }}' -m github.com/unistack-org/micro-proto) --go-micro_out='components=micro|http|server',standalone=false,debug=true,paths=source_relative:./handler handler/handler.proto"
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/unistack-org/micro-proto/api"
|
||||||
|
_ "github.com/unistack-org/micro-proto/openapiv2"
|
||||||
|
)
|
||||||
|
@@ -11,17 +11,17 @@ service Meter {
|
|||||||
rpc Metrics(micro.codec.Frame) returns (micro.codec.Frame) {
|
rpc Metrics(micro.codec.Frame) returns (micro.codec.Frame) {
|
||||||
option (micro.openapiv2.openapiv2_operation) = {
|
option (micro.openapiv2.openapiv2_operation) = {
|
||||||
operation_id: "Metrics";
|
operation_id: "Metrics";
|
||||||
responses: {
|
responses: {
|
||||||
key: "default";
|
response_code: {
|
||||||
|
name: "default";
|
||||||
value: {
|
value: {
|
||||||
description: "Error response";
|
json_reference: {
|
||||||
schema: {
|
description: "Error response";
|
||||||
json_schema: {
|
_ref: "micro.codec.Frame";
|
||||||
ref: "micro.codec.Frame";
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
option (micro.api.http) = { get: "/metrics"; };
|
option (micro.api.http) = { get: "/metrics"; };
|
||||||
};
|
};
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
// Code generated by protoc-gen-micro
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
|
// protoc-gen-go-micro version: v3.4.2
|
||||||
// source: handler.proto
|
// source: handler.proto
|
||||||
|
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -8,15 +10,21 @@ import (
|
|||||||
codec "github.com/unistack-org/micro/v3/codec"
|
codec "github.com/unistack-org/micro/v3/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewMeterEndpoints() []*api.Endpoint {
|
var (
|
||||||
return []*api.Endpoint{
|
MeterName = "Meter"
|
||||||
&api.Endpoint{
|
|
||||||
|
MeterEndpoints = []api.Endpoint{
|
||||||
|
{
|
||||||
Name: "Meter.Metrics",
|
Name: "Meter.Metrics",
|
||||||
Path: []string{"/metrics"},
|
Path: []string{"/metrics"},
|
||||||
Method: []string{"GET"},
|
Method: []string{"GET"},
|
||||||
Handler: "rpc",
|
Handler: "rpc",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewMeterEndpoints() []api.Endpoint {
|
||||||
|
return MeterEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
type MeterServer interface {
|
type MeterServer interface {
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
// Code generated by protoc-gen-micro
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
|
// protoc-gen-go-micro version: v3.4.2
|
||||||
// source: handler.proto
|
// source: handler.proto
|
||||||
|
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -26,8 +28,8 @@ func RegisterMeterServer(s server.Server, sh MeterServer, opts ...server.Handler
|
|||||||
}
|
}
|
||||||
h := &meterServer{sh}
|
h := &meterServer{sh}
|
||||||
var nopts []server.HandlerOption
|
var nopts []server.HandlerOption
|
||||||
for _, endpoint := range NewMeterEndpoints() {
|
for _, endpoint := range MeterEndpoints {
|
||||||
nopts = append(nopts, api.WithEndpoint(endpoint))
|
nopts = append(nopts, api.WithEndpoint(&endpoint))
|
||||||
}
|
}
|
||||||
return s.Handle(s.NewHandler(&Meter{h}, append(nopts, opts...)...))
|
return s.Handle(s.NewHandler(&Meter{h}, append(nopts, opts...)...))
|
||||||
}
|
}
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
package network
|
package network
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/meter"
|
"github.com/unistack-org/micro/v3/meter"
|
||||||
"github.com/unistack-org/micro/v3/network/tunnel"
|
"github.com/unistack-org/micro/v3/network/tunnel"
|
||||||
"github.com/unistack-org/micro/v3/proxy"
|
"github.com/unistack-org/micro/v3/proxy"
|
||||||
"github.com/unistack-org/micro/v3/router"
|
"github.com/unistack-org/micro/v3/router"
|
||||||
"github.com/unistack-org/micro/v3/tracer"
|
"github.com/unistack-org/micro/v3/tracer"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option func
|
// Option func
|
||||||
@@ -119,7 +119,7 @@ func Tracer(t tracer.Tracer) Option {
|
|||||||
// NewOptions returns network default options
|
// NewOptions returns network default options
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Id: uuid.New().String(),
|
Id: id.Must(),
|
||||||
Name: "go.micro",
|
Name: "go.micro",
|
||||||
Address: ":0",
|
Address: ":0",
|
||||||
Logger: logger.DefaultLogger,
|
Logger: logger.DefaultLogger,
|
||||||
|
@@ -3,11 +3,11 @@ package tunnel
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/meter"
|
"github.com/unistack-org/micro/v3/meter"
|
||||||
"github.com/unistack-org/micro/v3/network/transport"
|
"github.com/unistack-org/micro/v3/network/transport"
|
||||||
"github.com/unistack-org/micro/v3/tracer"
|
"github.com/unistack-org/micro/v3/tracer"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -164,7 +164,7 @@ func DialWait(b bool) DialOption {
|
|||||||
// NewOptions returns router default options with filled values
|
// NewOptions returns router default options with filled values
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
ID: uuid.New().String(),
|
ID: id.Must(),
|
||||||
Address: DefaultAddress,
|
Address: DefaultAddress,
|
||||||
Token: DefaultToken,
|
Token: DefaultToken,
|
||||||
Logger: logger.DefaultLogger,
|
Logger: logger.DefaultLogger,
|
||||||
|
@@ -6,8 +6,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -378,13 +378,16 @@ func (m *memory) ListServices(ctx context.Context, opts ...ListOption) ([]*Servi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *memory) Watch(ctx context.Context, opts ...WatchOption) (Watcher, error) {
|
func (m *memory) Watch(ctx context.Context, opts ...WatchOption) (Watcher, error) {
|
||||||
|
id, err := id.New()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
wo := NewWatchOptions(opts...)
|
wo := NewWatchOptions(opts...)
|
||||||
|
|
||||||
// construct the watcher
|
// construct the watcher
|
||||||
w := &watcher{
|
w := &watcher{
|
||||||
exit: make(chan bool),
|
exit: make(chan bool),
|
||||||
res: make(chan *Result),
|
res: make(chan *Result),
|
||||||
id: uuid.New().String(),
|
id: id,
|
||||||
wo: wo,
|
wo: wo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -53,7 +53,7 @@ func (d *dns) Lookup(opts ...QueryOption) ([]Route, error) {
|
|||||||
for i, ip := range ips {
|
for i, ip := range ips {
|
||||||
result[i] = Route{
|
result[i] = Route{
|
||||||
Service: options.Service,
|
Service: options.Service,
|
||||||
Address: fmt.Sprintf("%s:%d", ip, uint16(p)),
|
Address: fmt.Sprintf("%s:%d", ip, p),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
|
@@ -3,9 +3,9 @@ package router
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
"github.com/unistack-org/micro/v3/register"
|
"github.com/unistack-org/micro/v3/register"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Options are router options
|
// Options are router options
|
||||||
@@ -80,7 +80,7 @@ func Name(n string) Option {
|
|||||||
// NewOptions returns router default options
|
// NewOptions returns router default options
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Id: uuid.New().String(),
|
Id: id.Must(),
|
||||||
Network: DefaultNetwork,
|
Network: DefaultNetwork,
|
||||||
Register: register.DefaultRegister,
|
Register: register.DefaultRegister,
|
||||||
Logger: logger.DefaultLogger,
|
Logger: logger.DefaultLogger,
|
||||||
|
@@ -1,3 +1,8 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
//go:generate protoc -I./health -I../ -I/home/vtolstov/.cache/go-path/pkg/mod/github.com/unistack-org/micro-proto@v0.0.1 --micro_out=components=micro|http|server,standalone=false,debug=true,paths=source_relative:./health health/health.proto
|
//go:generate sh -c "protoc -I./health -I../ -I$(go list -f '{{ .Dir }}' -m github.com/unistack-org/micro-proto) --go-micro_out='components=micro|http|server',standalone=false,debug=true,paths=source_relative:./health health/health.proto"
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/unistack-org/micro-proto/api"
|
||||||
|
_ "github.com/unistack-org/micro-proto/openapiv2"
|
||||||
|
)
|
||||||
|
@@ -11,51 +11,51 @@ service Health {
|
|||||||
rpc Live(micro.codec.Frame) returns (micro.codec.Frame) {
|
rpc Live(micro.codec.Frame) returns (micro.codec.Frame) {
|
||||||
option (micro.openapiv2.openapiv2_operation) = {
|
option (micro.openapiv2.openapiv2_operation) = {
|
||||||
operation_id: "Live";
|
operation_id: "Live";
|
||||||
responses: {
|
responses: {
|
||||||
key: "default";
|
response_code: {
|
||||||
|
name: "default";
|
||||||
value: {
|
value: {
|
||||||
description: "Error response";
|
json_reference: {
|
||||||
schema: {
|
description: "Error response";
|
||||||
json_schema: {
|
_ref: "micro.codec.Frame";
|
||||||
ref: "micro.codec.Frame";
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
option (micro.api.http) = { get: "/live"; };
|
option (micro.api.http) = { get: "/live"; };
|
||||||
};
|
};
|
||||||
rpc Ready(micro.codec.Frame) returns (micro.codec.Frame) {
|
rpc Ready(micro.codec.Frame) returns (micro.codec.Frame) {
|
||||||
option (micro.openapiv2.openapiv2_operation) = {
|
option (micro.openapiv2.openapiv2_operation) = {
|
||||||
operation_id: "Ready";
|
operation_id: "Ready";
|
||||||
responses: {
|
responses: {
|
||||||
key: "default";
|
response_code: {
|
||||||
|
name: "default";
|
||||||
value: {
|
value: {
|
||||||
description: "Error response";
|
json_reference: {
|
||||||
schema: {
|
description: "Error response";
|
||||||
json_schema: {
|
_ref: "micro.codec.Frame";
|
||||||
ref: "micro.codec.Frame";
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
option (micro.api.http) = { get: "/ready"; };
|
option (micro.api.http) = { get: "/ready"; };
|
||||||
};
|
};
|
||||||
rpc Version(micro.codec.Frame) returns (micro.codec.Frame) {
|
rpc Version(micro.codec.Frame) returns (micro.codec.Frame) {
|
||||||
option (micro.openapiv2.openapiv2_operation) = {
|
option (micro.openapiv2.openapiv2_operation) = {
|
||||||
operation_id: "Version";
|
operation_id: "Version";
|
||||||
responses: {
|
responses: {
|
||||||
key: "default";
|
response_code: {
|
||||||
|
name: "default";
|
||||||
value: {
|
value: {
|
||||||
description: "Error response";
|
json_reference: {
|
||||||
schema: {
|
description: "Error response";
|
||||||
json_schema: {
|
_ref: "micro.codec.Frame";
|
||||||
ref: "micro.codec.Frame";
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
option (micro.api.http) = { get: "/version"; };
|
option (micro.api.http) = { get: "/version"; };
|
||||||
};
|
};
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
// Code generated by protoc-gen-micro
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
|
// protoc-gen-go-micro version: v3.4.2
|
||||||
// source: health.proto
|
// source: health.proto
|
||||||
|
|
||||||
package health
|
package health
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -8,27 +10,33 @@ import (
|
|||||||
codec "github.com/unistack-org/micro/v3/codec"
|
codec "github.com/unistack-org/micro/v3/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHealthEndpoints() []*api.Endpoint {
|
var (
|
||||||
return []*api.Endpoint{
|
HealthName = "Health"
|
||||||
&api.Endpoint{
|
|
||||||
|
HealthEndpoints = []api.Endpoint{
|
||||||
|
{
|
||||||
Name: "Health.Live",
|
Name: "Health.Live",
|
||||||
Path: []string{"/live"},
|
Path: []string{"/live"},
|
||||||
Method: []string{"GET"},
|
Method: []string{"GET"},
|
||||||
Handler: "rpc",
|
Handler: "rpc",
|
||||||
},
|
},
|
||||||
&api.Endpoint{
|
{
|
||||||
Name: "Health.Ready",
|
Name: "Health.Ready",
|
||||||
Path: []string{"/ready"},
|
Path: []string{"/ready"},
|
||||||
Method: []string{"GET"},
|
Method: []string{"GET"},
|
||||||
Handler: "rpc",
|
Handler: "rpc",
|
||||||
},
|
},
|
||||||
&api.Endpoint{
|
{
|
||||||
Name: "Health.Version",
|
Name: "Health.Version",
|
||||||
Path: []string{"/version"},
|
Path: []string{"/version"},
|
||||||
Method: []string{"GET"},
|
Method: []string{"GET"},
|
||||||
Handler: "rpc",
|
Handler: "rpc",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewHealthEndpoints() []api.Endpoint {
|
||||||
|
return HealthEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
type HealthServer interface {
|
type HealthServer interface {
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
// Code generated by protoc-gen-micro
|
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
|
||||||
|
// protoc-gen-go-micro version: v3.4.2
|
||||||
// source: health.proto
|
// source: health.proto
|
||||||
|
|
||||||
package health
|
package health
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -36,8 +38,8 @@ func RegisterHealthServer(s server.Server, sh HealthServer, opts ...server.Handl
|
|||||||
}
|
}
|
||||||
h := &healthServer{sh}
|
h := &healthServer{sh}
|
||||||
var nopts []server.HandlerOption
|
var nopts []server.HandlerOption
|
||||||
for _, endpoint := range NewHealthEndpoints() {
|
for _, endpoint := range HealthEndpoints {
|
||||||
nopts = append(nopts, api.WithEndpoint(endpoint))
|
nopts = append(nopts, api.WithEndpoint(&endpoint))
|
||||||
}
|
}
|
||||||
return s.Handle(s.NewHandler(&Health{h}, append(nopts, opts...)...))
|
return s.Handle(s.NewHandler(&Health{h}, append(nopts, opts...)...))
|
||||||
}
|
}
|
||||||
|
@@ -5,10 +5,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/codec"
|
"github.com/unistack-org/micro/v3/codec"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/register"
|
"github.com/unistack-org/micro/v3/register"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultServer default server
|
// DefaultServer default server
|
||||||
@@ -22,7 +22,7 @@ var (
|
|||||||
// DefaultVersion will be used if no version passed
|
// DefaultVersion will be used if no version passed
|
||||||
DefaultVersion = "latest"
|
DefaultVersion = "latest"
|
||||||
// DefaultID will be used if no id passed
|
// DefaultID will be used if no id passed
|
||||||
DefaultID = uuid.New().String()
|
DefaultID = id.Must()
|
||||||
// DefaultRegisterCheck holds func that run before register server
|
// DefaultRegisterCheck holds func that run before register server
|
||||||
DefaultRegisterCheck = func(context.Context) error { return nil }
|
DefaultRegisterCheck = func(context.Context) error { return nil }
|
||||||
// DefaultRegisterInterval holds interval for register
|
// DefaultRegisterInterval holds interval for register
|
||||||
|
@@ -4,9 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/auth"
|
"github.com/unistack-org/micro/v3/auth"
|
||||||
"github.com/unistack-org/micro/v3/logger"
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Verify the auth credentials and refresh the auth token periodically
|
// Verify the auth credentials and refresh the auth token periodically
|
||||||
@@ -22,7 +22,11 @@ func Verify(a auth.Auth) error {
|
|||||||
auth.WithScopes("service"),
|
auth.WithScopes("service"),
|
||||||
}
|
}
|
||||||
|
|
||||||
acc, err := a.Generate(uuid.New().String(), opts...)
|
id, err := id.New()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
acc, err := a.Generate(id, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
22
util/id/LICENSE
Normal file
22
util/id/LICENSE
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2018-2021 Matous Dzivjak <matousdzivjak@gmail.com>
|
||||||
|
Copyright (c) 2021 Unistack LLC <v.tolstov@unistack.org>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
112
util/id/id.go
Normal file
112
util/id/id.go
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package id
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/unistack-org/micro/v3/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultAlphabet is the alphabet used for ID characters by default
|
||||||
|
var DefaultAlphabet = []rune("6789BCDFGHJKLMNPQRTWbcdfghjkmnpqrtwz")
|
||||||
|
|
||||||
|
// DefaultSize is the size used for ID by default
|
||||||
|
// To get uuid like collision specify 21
|
||||||
|
var DefaultSize = 16
|
||||||
|
|
||||||
|
// getMask generates bit mask used to obtain bits from the random bytes that are used to get index of random character
|
||||||
|
// from the alphabet. Example: if the alphabet has 6 = (110)_2 characters it is sufficient to use mask 7 = (111)_2
|
||||||
|
func getMask(alphabetSize int) int {
|
||||||
|
for i := 1; i <= 8; i++ {
|
||||||
|
mask := (2 << uint(i)) - 1
|
||||||
|
if mask >= alphabetSize-1 {
|
||||||
|
return mask
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns new id or error
|
||||||
|
func New(opts ...Option) (string, error) {
|
||||||
|
options := NewOptions(opts...)
|
||||||
|
|
||||||
|
if len(options.Alphabet) == 0 || len(options.Alphabet) > 255 {
|
||||||
|
return "", errors.New("alphabet must not be empty and contain no more than 255 chars")
|
||||||
|
}
|
||||||
|
if options.Size <= 0 {
|
||||||
|
return "", errors.New("size must be positive integer")
|
||||||
|
}
|
||||||
|
|
||||||
|
chars := options.Alphabet
|
||||||
|
|
||||||
|
mask := getMask(len(chars))
|
||||||
|
// estimate how many random bytes we will need for the ID, we might actually need more but this is tradeoff
|
||||||
|
// between average case and worst case
|
||||||
|
ceilArg := 1.6 * float64(mask*options.Size) / float64(len(options.Alphabet))
|
||||||
|
step := int(math.Ceil(ceilArg))
|
||||||
|
|
||||||
|
id := make([]rune, options.Size)
|
||||||
|
bytes := make([]byte, step)
|
||||||
|
for j := 0; ; {
|
||||||
|
_, err := rand.Read(bytes)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
for i := 0; i < step; i++ {
|
||||||
|
currByte := bytes[i] & byte(mask)
|
||||||
|
if currByte < byte(len(chars)) {
|
||||||
|
id[j] = chars[currByte]
|
||||||
|
j++
|
||||||
|
if j == options.Size {
|
||||||
|
return string(id[:options.Size]), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must is the same as New but fatals on error
|
||||||
|
func Must(opts ...Option) string {
|
||||||
|
id, err := New(opts...)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(context.TODO(), err)
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options contains id deneration options
|
||||||
|
type Options struct {
|
||||||
|
Alphabet []rune
|
||||||
|
Size int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Option func signature
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
// Alphabet specifies alphabet to use
|
||||||
|
func Alphabet(alphabet string) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Alphabet = []rune(alphabet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size specifies id size
|
||||||
|
func Size(size int) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Size = size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOptions returns new Options struct filled by opts
|
||||||
|
func NewOptions(opts ...Option) Options {
|
||||||
|
options := Options{
|
||||||
|
Alphabet: DefaultAlphabet,
|
||||||
|
Size: DefaultSize,
|
||||||
|
}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
|
}
|
@@ -5,8 +5,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/network/transport"
|
"github.com/unistack-org/micro/v3/network/transport"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pool struct {
|
type pool struct {
|
||||||
@@ -87,9 +87,13 @@ func (p *pool) Get(ctx context.Context, addr string, opts ...transport.DialOptio
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
id, err := id.New()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &poolConn{
|
return &poolConn{
|
||||||
Client: c,
|
Client: c,
|
||||||
id: uuid.New().String(),
|
id: id,
|
||||||
created: time.Now(),
|
created: time.Now(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@@ -1,17 +1,17 @@
|
|||||||
package rand
|
package rand
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
crand "crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Rand is a wrapper around crypto/rand that adds some convenience functions known from math/rand.
|
// Rand is a wrapper around crypto/rand that adds some convenience functions known from math/rand
|
||||||
type Rand struct {
|
type Rand struct {
|
||||||
buf [8]byte
|
buf [8]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rand) Int31() int32 {
|
func (r *Rand) Int31() int32 {
|
||||||
_, _ = rand.Read(r.buf[:4])
|
_, _ = crand.Read(r.buf[:4])
|
||||||
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
|
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,11 +54,11 @@ func (r *Rand) Intn(n int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rand) Int63() int64 {
|
func (r *Rand) Int63() int64 {
|
||||||
_, _ = rand.Read(r.buf[:])
|
_, _ = crand.Read(r.buf[:])
|
||||||
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
|
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
|
||||||
}
|
}
|
||||||
|
|
||||||
// copied from the standard library math/rand implementation of Int63n
|
// Int31n copied from the standard library math/rand implementation of Int31n
|
||||||
func (r *Rand) Int31n(n int32) int32 {
|
func (r *Rand) Int31n(n int32) int32 {
|
||||||
if n&(n-1) == 0 { // n is power of two, can mask
|
if n&(n-1) == 0 { // n is power of two, can mask
|
||||||
return r.Int31() & (n - 1)
|
return r.Int31() & (n - 1)
|
||||||
@@ -71,6 +71,7 @@ func (r *Rand) Int31n(n int32) int32 {
|
|||||||
return v % n
|
return v % n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int63n copied from the standard library math/rand implementation of Int63n
|
||||||
func (r *Rand) Int63n(n int64) int64 {
|
func (r *Rand) Int63n(n int64) int64 {
|
||||||
if n&(n-1) == 0 { // n is power of two, can mask
|
if n&(n-1) == 0 { // n is power of two, can mask
|
||||||
return r.Int63() & (n - 1)
|
return r.Int63() & (n - 1)
|
||||||
@@ -82,3 +83,26 @@ func (r *Rand) Int63n(n int64) int64 {
|
|||||||
}
|
}
|
||||||
return v % n
|
return v % n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shuffle copied from the standard library math/rand implementation of Shuffle
|
||||||
|
func (r *Rand) Shuffle(n int, swap func(i, j int)) {
|
||||||
|
if n < 0 {
|
||||||
|
panic("invalid argument to Shuffle")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fisher-Yates shuffle: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
|
||||||
|
// Shuffle really ought not be called with n that doesn't fit in 32 bits.
|
||||||
|
// Not only will it take a very long time, but with 2³¹! possible permutations,
|
||||||
|
// there's no way that any PRNG can have a big enough internal state to
|
||||||
|
// generate even a minuscule percentage of the possible permutations.
|
||||||
|
// Nevertheless, the right API signature accepts an int n, so handle it as best we can.
|
||||||
|
i := n - 1
|
||||||
|
for ; i > 1<<31-1-1; i-- {
|
||||||
|
j := int(r.Int63n(int64(i + 1)))
|
||||||
|
swap(i, j)
|
||||||
|
}
|
||||||
|
for ; i > 0; i-- {
|
||||||
|
j := int(r.Int31n(int32(i + 1)))
|
||||||
|
swap(i, j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -5,7 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Buffer is ring buffer
|
// Buffer is ring buffer
|
||||||
@@ -112,7 +112,7 @@ func (b *Buffer) Stream() (<-chan *Entry, chan bool) {
|
|||||||
defer b.Unlock()
|
defer b.Unlock()
|
||||||
|
|
||||||
entries := make(chan *Entry, 128)
|
entries := make(chan *Entry, 128)
|
||||||
id := uuid.New().String()
|
id := id.Must()
|
||||||
stop := make(chan bool)
|
stop := make(chan bool)
|
||||||
|
|
||||||
b.streams[id] = &Stream{
|
b.streams[id] = &Stream{
|
||||||
|
@@ -6,9 +6,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/unistack-org/micro/v3/auth"
|
"github.com/unistack-org/micro/v3/auth"
|
||||||
"github.com/unistack-org/micro/v3/store"
|
"github.com/unistack-org/micro/v3/store"
|
||||||
|
"github.com/unistack-org/micro/v3/util/id"
|
||||||
"github.com/unistack-org/micro/v3/util/token"
|
"github.com/unistack-org/micro/v3/util/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,7 +44,11 @@ func (b *Basic) Generate(acc *auth.Account, opts ...token.GenerateOption) (*toke
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write to the store
|
// write to the store
|
||||||
key := uuid.New().String()
|
key, err := id.New()
|
||||||
|
if err !=nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
err = b.store.Write(context.Background(), fmt.Sprintf("%v%v", StorePrefix, key), bytes, store.WriteTTL(options.Expiry))
|
err = b.store.Write(context.Background(), fmt.Sprintf("%v%v", StorePrefix, key), bytes, store.WriteTTL(options.Expiry))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -4,7 +4,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
"github.com/unistack-org/micro/v3/auth"
|
"github.com/unistack-org/micro/v3/auth"
|
||||||
"github.com/unistack-org/micro/v3/metadata"
|
"github.com/unistack-org/micro/v3/metadata"
|
||||||
"github.com/unistack-org/micro/v3/util/token"
|
"github.com/unistack-org/micro/v3/util/token"
|
||||||
|
Reference in New Issue
Block a user