Skip to content

Commit

Permalink
V0.13.x (#237)
Browse files Browse the repository at this point in the history
* fix(factory): on reload, close the connection right away.

graceful shutdown logic should be implemented in the close function, not in the factory.

* fix(cron): test flakes

* feat: add eventv2

* wip: prototyping generics api

* wip: prototyping generics api

* wip: prototyping generics api

* feat: ctxmeta support generic

* fix: asyncContext.Value interface to any

* wip: generics

* wip: add once to event

* refactor: remove text package

* feat: interface{} to any

* refactor: remove deprecated cron

* test: workflow go-version 1.18beta2

* test: setup-go stable false

* test: fix workflow go-version

* doc: add listener comment

* feat: simplify run group

* fix: remove deprecatedcron test

* fix: missing StatusChanged

* fix: some test

* feat: simplify run group

* fix: some import style

* fix: remove config module dispatcher

* fix(otgorm): hooks (#236)

* fix(otgorm): hooks

* fix: remove debug prints

(cherry picked from commit 1afb3d7)

* refactor(control): add pool

* fix(pool): data race

* fix(pool): data race

* fix(pool): go vet "call to (*T).Fatalf from a non-test goroutine"

* refactor: change leader election semantics. (#238)

* refactor: change leader election semantics.

Now leader election campaign will not return when the node beomes leader, instead it will block until the leader election session exists, updating status proactively. This change will make leader election status more accurate.

* fix(leader): mock driver for test

* fix: return if renew fails

* test: increase test duration

* docs: add jetbrains thanks

* fix(cron): example flakes

* fix(leader): TestElection block test

* fix(leader): missing dispatch when leader status on

* fix(leader): test data race

* fix(leader): test key conflict

* test(ots3): test container of minio

* feat: add a simple way to ensure cron jobs to run at least once for each schedule.

* doc: add comment

* fix(leaderredis): data race

Co-authored-by: Trock <g_trock@163.com>

* fix: server.go merge conflict

* feat(test): upgrade github actions

* fix: otredis TestModule_ProvideCommand

* fix: workflow lint add setup-go

* fix: workflow lint add setup-go

* fix: pool.Run need start before call Go method when use async mode.

* feat: add pool.Counter to observe the status of pool.

Co-authored-by: Trock <g_trock@163.com>
Co-authored-by: Trock <35254251+GGXXLL@users.noreply.github.com>
  • Loading branch information
3 people committed May 19, 2022
1 parent 5ac3d1c commit a05f3a3
Show file tree
Hide file tree
Showing 183 changed files with 2,093 additions and 1,815 deletions.
63 changes: 35 additions & 28 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.16.x,1.17.x]
go-version: [ 1.18.x ]
runs-on: ubuntu-latest
services:
redis:
Expand Down Expand Up @@ -70,44 +70,51 @@ jobs:
options: --name minio-server

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Set up Go
uses: actions/setup-go@v3
with:
stable: false
go-version: ${{ matrix.go-version }}

- uses: actions/cache@v2
id: cache-go-mod
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('go.mod') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go-version }}
- uses: actions/cache@v3
id: cache-go-mod
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('go.mod') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go-version }}
- name: Environment Variables from Dotenv
uses: c-py/action-dotenv-to-setenv@v3
- name: Environment Variables from Dotenv
uses: c-py/action-dotenv-to-setenv@v3

- name: Build
if: steps.cache-go-mod.outputs.cache-hit != 'true'
run: go build -v ./...
- name: Build
if: steps.cache-go-mod.outputs.cache-hit != 'true'
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...

- name: Upload coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage.txt
- name: Upload coverage
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage.txt
lint:
strategy:
matrix:
go-version: [ 1.18.x ]
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- name: Lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.41
version: v1.45.2
args: --disable errcheck --timeout 5m0s
only-new-issues: true
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,4 @@ the new otetcd configuration is not backward compatible.

* **leader:** add leader election package. ([#56](https://github.com/DoNewsCode/core/issues/56)) (@[谷溪](guxi99@gmail.com))

>>>>>>> origin/master
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,7 @@ Be sure to checkout the documentation section to learn more.
- [Go Kit](https://github.com/DoNewsCode/core-kit) (if multiple transports)
- [Kratos v2](https://github.com/go-kratos/kratos)

## Thanks for JetBrains OS licenses

<a href="https://www.jetbrains.com/?from=Donews/core" target="_blank"><img src="https://user-images.githubusercontent.com/1787798/69898077-4f4e3d00-138f-11ea-81f9-96fb7c49da89.png" width="150" align="middle"/></a>

4 changes: 4 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,7 @@ type HTTPProvider interface {
- [Gin](https://github.com/DoNewsCode/core-gin) (如果仅限于 HTTP)
- [Go Kit](https://github.com/DoNewsCode/core-kit) (如果有多个运输工具)
- [kratos](https://github.com/go-kratos/kratos) (当前 v2 已经可以使用)

## 感谢 JetBrains 开源证书支持

<a href="https://www.jetbrains.com/?from=Donews/core" target="_blank"><img src="https://user-images.githubusercontent.com/1787798/69898077-4f4e3d00-138f-11ea-81f9-96fb7c49da89.png" width="150" align="middle"/></a>
62 changes: 24 additions & 38 deletions c.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/DoNewsCode/core/contract"
"github.com/DoNewsCode/core/di"
"github.com/DoNewsCode/core/logging"

"github.com/go-kit/log"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
Expand All @@ -34,29 +35,25 @@ type C struct {
contract.ConfigAccessor
logging.LevelLogger
*container.Container
contract.Dispatcher
di *dig.Container
baseLogger log.Logger
}

// ConfParser models a parser for configuration. For example, yaml.Parser.
type ConfParser interface {
Unmarshal([]byte) (map[string]interface{}, error)
Marshal(map[string]interface{}) ([]byte, error)
Unmarshal([]byte) (map[string]any, error)
Marshal(map[string]any) ([]byte, error)
}

// ConfProvider models a configuration provider. For example, file.Provider.
type ConfProvider interface {
ReadBytes() ([]byte, error)
Read() (map[string]interface{}, error)
Read() (map[string]any, error)
}

// ConfigProvider provides contract.ConfigAccessor to the core.
type ConfigProvider func(configStack []config.ProviderSet, configWatcher contract.ConfigWatcher) contract.ConfigUnmarshaler

// EventDispatcherProvider provides contract.Dispatcher to the core.
type EventDispatcherProvider func(conf contract.ConfigUnmarshaler) contract.Dispatcher

// DiProvider provides the *dig.Container to the core.
type DiProvider func(conf contract.ConfigUnmarshaler) *dig.Container

Expand All @@ -74,12 +71,11 @@ type coreValues struct {
configStack []config.ProviderSet
configWatcher contract.ConfigWatcher
// ConfProvider functions
configProvider ConfigProvider
eventDispatcherProvider EventDispatcherProvider
diProvider DiProvider
appNameProvider AppNameProvider
envProvider EnvProvider
loggerProvider LoggerProvider
configProvider ConfigProvider
diProvider DiProvider
appNameProvider AppNameProvider
envProvider EnvProvider
loggerProvider LoggerProvider
}

// CoreOption is the option to modify core attribute.
Expand All @@ -93,8 +89,8 @@ func WithYamlFile(path string) (CoreOption, CoreOption) {
}

// WithInline is a CoreOption that creates a inline config in the configuration stack.
func WithInline(key string, entry interface{}) CoreOption {
return WithConfigStack(confmap.Provider(map[string]interface{}{
func WithInline(key string, entry any) CoreOption {
return WithConfigStack(confmap.Provider(map[string]any{
key: entry,
}, "."), nil)
}
Expand Down Expand Up @@ -148,24 +144,16 @@ func SetDiProvider(provider DiProvider) CoreOption {
}
}

// SetEventDispatcherProvider is a CoreOption to replaces the default EventDispatcherProvider.
func SetEventDispatcherProvider(provider EventDispatcherProvider) CoreOption {
return func(values *coreValues) {
values.eventDispatcherProvider = provider
}
}

// New creates a new bare-bones C.
func New(opts ...CoreOption) *C {
values := coreValues{
configStack: []config.ProviderSet{},
configWatcher: nil,
configProvider: ProvideConfig,
appNameProvider: ProvideAppName,
envProvider: ProvideEnv,
loggerProvider: ProvideLogger,
diProvider: ProvideDi,
eventDispatcherProvider: ProvideEventDispatcher,
configStack: []config.ProviderSet{},
configWatcher: nil,
configProvider: ProvideConfig,
appNameProvider: ProvideAppName,
envProvider: ProvideEnv,
loggerProvider: ProvideLogger,
diProvider: ProvideDi,
}
for _, f := range opts {
f(&values)
Expand All @@ -175,15 +163,13 @@ func New(opts ...CoreOption) *C {
appName := values.appNameProvider(conf)
logger := values.loggerProvider(conf, appName, env)
diContainer := values.diProvider(conf)
dispatcher := values.eventDispatcherProvider(conf)

c := C{
AppName: appName,
Env: env,
ConfigAccessor: config.WithAccessor(conf),
LevelLogger: logging.WithLevel(logger),
Container: &container.Container{},
Dispatcher: dispatcher,
di: diContainer,
baseLogger: logger,
}
Expand All @@ -207,7 +193,7 @@ func Default(opts ...CoreOption) *C {
// container. The semantics of injection follows the same rule of dig.Invoke.
// Note that the module added in this way will not retain any original field
// values, i.e. the module will only contain fields populated by DI container.
func (c *C) AddModule(module interface{}) {
func (c *C) AddModule(module any) {
t := reflect.TypeOf(module)
if t.Kind() == reflect.Ptr && dig.IsIn(t.Elem()) {
err := di.IntoPopulator(c.di).Populate(module)
Expand Down Expand Up @@ -252,7 +238,7 @@ func (c *C) Provide(deps di.Deps) {
}
}

func (c *C) provide(constructor interface{}) {
func (c *C) provide(constructor any) {
var (
options []dig.ProvideOption
shouldMakeFunc bool
Expand Down Expand Up @@ -342,7 +328,7 @@ func (c *C) ProvideEssentials() {
DIPopulator contract.DIPopulator
Logger log.Logger
LevelLogger logging.LevelLogger
Dispatcher contract.Dispatcher
Lifecycles lifecycleOut
DefaultConfigs []config.ExportedConfig `group:"config,flatten"`
}

Expand All @@ -355,8 +341,8 @@ func (c *C) ProvideEssentials() {
ConfigAccessor: c.ConfigAccessor,
Logger: c.baseLogger,
LevelLogger: c.LevelLogger,
Dispatcher: c.Dispatcher,
DIPopulator: di.IntoPopulator(c.di),
Lifecycles: provideLifecycle(),
DefaultConfigs: provideDefaultConfig(),
}
if cc, ok := c.ConfigAccessor.(contract.ConfigRouter); ok {
Expand Down Expand Up @@ -391,7 +377,7 @@ func (c *C) Shutdown() {

// AddModuleFunc add the module after Invoking its constructor. Clean up
// functions and errors are handled automatically.
func (c *C) AddModuleFunc(constructor interface{}) {
func (c *C) AddModuleFunc(constructor any) {
c.provide(constructor)
ftype := reflect.TypeOf(constructor)
targetTypes := make([]reflect.Type, 0)
Expand Down Expand Up @@ -439,7 +425,7 @@ func (c *C) ApplyRootCommand(command *cobra.Command) {
//
// It internally calls uber's dig library. Consult dig's documentation for
// details. (https://pkg.go.dev/go.uber.org/dig)
func (c *C) Invoke(function interface{}) {
func (c *C) Invoke(function any) {
err := c.di.Invoke(function)
if err != nil {
panic(err)
Expand Down
Loading

0 comments on commit a05f3a3

Please sign in to comment.