Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] integrate datasource of OpenSergo by OpenSergo Go SDK #489

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/datasource/opensergo/README.md
@@ -0,0 +1,13 @@
# Opensergo DataSource for Sentinel Go

## Prepare Environment for Test Version

- Prepare the `OpenSergo GO SDK`.
Because of the `OpenSergo GO SDK` has no published version, so should download the sourcecode of [`OpenSergo GO SDK`](https://github.com/jnan806/opensergo-go-sdk/tree/initial-version), and move it into you `GOPATH`.
- Rename the right version in directory name of `OpenSergo GO SDK` sourcecode.
Make sure the version in sourcecode directory name is the same with go.mod.
eg. `$PATH/pkg/mod/github.com/opensergo/opensergo-go@v0.0.0-20220331070310-e5b01fee4d1c`

## Samples

- [datasource_opensergo_example.go](./demo/datasource_opensergo_example.go)
119 changes: 119 additions & 0 deletions pkg/datasource/opensergo/demo/datasource_opensergo_example.go
@@ -0,0 +1,119 @@
// Copyright 1999-2020 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
jnan806 marked this conversation as resolved.
Show resolved Hide resolved
"fmt"
"log"
"math/rand"
"sync/atomic"
"time"

sentinel "github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/core/config"
_ "github.com/alibaba/sentinel-golang/ext/datasource"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/pkg/datasource/opensergo"
_ "github.com/alibaba/sentinel-golang/pkg/datasource/opensergo"
"github.com/alibaba/sentinel-golang/util"
)

const (
host string = "127.0.0.1"
port uint32 = 10246

namespace string = "default"
app string = "foo-app"
)

type Counter struct {
pass *int64
block *int64
total *int64
}

func main() {
openSergoDataSource, _ := opensergo.NewOpenSergoDataSource(host, port, namespace, app)
openSergoDataSource.Initialize()

// simulate concurrency request
simulateConcurrency()

select {}
}

func simulateConcurrency() {
counter := Counter{pass: new(int64), block: new(int64), total: new(int64)}

// simulate request
go startFlowModule(&counter)
// print counter
go timerTask(&counter)
}

func startFlowModule(counter *Counter) {
// We should initialize Sentinel first.
conf := config.NewDefaultConfig()
// for testing, logging output to console
conf.Sentinel.Log.Logger = logging.NewConsoleLogger()
if err := sentinel.InitWithConfig(conf); err != nil {
log.Fatal(err)
}

for i := 0; i < 10; i++ {
go func() {
for {
e, b := sentinel.Entry("GET:/foo/1", sentinel.WithTrafficType(base.Inbound))
if b != nil {
// Blocked. We could get the block reason from the BlockError.
atomic.AddInt64(counter.block, 1)
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
} else {
// Passed, wrap the logic here.
atomic.AddInt64(counter.pass, 1)
time.Sleep(time.Duration(rand.Uint64()%10) * time.Millisecond)
// Be sure the entry is exited finally.
e.Exit()
}
atomic.AddInt64(counter.total, 1)
}
}()
}
}

//statistic print
func timerTask(counter *Counter) {
fmt.Println("begin to statistic!!!")
var (
oldTotal, oldPass, oldBlock int64
)
for {
time.Sleep(1 * time.Second)
globalTotal := atomic.LoadInt64(counter.total)
oneSecondTotal := globalTotal - oldTotal
oldTotal = globalTotal

globalPass := atomic.LoadInt64(counter.pass)
oneSecondPass := globalPass - oldPass
oldPass = globalPass

globalBlock := atomic.LoadInt64(counter.block)
oneSecondBlock := globalBlock - oldBlock
oldBlock = globalBlock

fmt.Println(util.CurrentTimeMillis()/1000, "total:", oneSecondTotal, " pass:", oneSecondPass, " block:", oneSecondBlock)
}
}
16 changes: 16 additions & 0 deletions pkg/datasource/opensergo/doc.go
@@ -0,0 +1,16 @@
// Copyright 1999-2020 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package opensergo This package provides OpenSergo dynamic data-source implementation for Sentinel.
package opensergo
10 changes: 10 additions & 0 deletions pkg/datasource/opensergo/go.mod
@@ -0,0 +1,10 @@
module github.com/alibaba/sentinel-golang/pkg/datasource/opensergo

go 1.13

require (
github.com/alibaba/sentinel-golang v1.0.4
github.com/opensergo/opensergo-go v0.0.0-20230105054255-fe6fd77bd6ce
github.com/pkg/errors v0.9.1
google.golang.org/protobuf v1.27.1
)