Skip to content

Commit

Permalink
Changes: rename 'tenant' to 'domain' and add 'project' key. (#160)
Browse files Browse the repository at this point in the history
* Changes: rename 'tenant' to 'domain' and add 'project' key.

(cherry picked from commit 111cb80)

* Optimize store.go
  • Loading branch information
little-cui committed Nov 9, 2017
1 parent b7710a3 commit 3f75851
Show file tree
Hide file tree
Showing 41 changed files with 857 additions and 776 deletions.
2 changes: 1 addition & 1 deletion integration/microservices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ var _ = Describe("MicroService Api Test", func() {
Expect(foundMicroService).To(Equal(true))
})

It("test get all service with wrong tenant name", func() {
It("test get all service with wrong domain name", func() {
req, _ := http.NewRequest(GET, SCURL+GETALLSERVICE, nil)
req.Header.Set("X-Domain-Name", "default1")
resp, _ := scclient.Do(req)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//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 store
package async

import (
"errors"
Expand Down Expand Up @@ -73,7 +73,7 @@ func (s *scheduler) Close() {
s.queue.Close()
}

type AsyncTasker struct {
type AsyncTaskService struct {
schedules map[string]*scheduler
removeTasks map[string]struct{}
goroutine *util.GoRoutine
Expand All @@ -82,7 +82,7 @@ type AsyncTasker struct {
isClose bool
}

func (lat *AsyncTasker) getOrNewScheduler(task AsyncTask) (s *scheduler, isNew bool) {
func (lat *AsyncTaskService) getOrNewScheduler(task AsyncTask) (s *scheduler, isNew bool) {
var (
ok bool
key = task.Key()
Expand Down Expand Up @@ -116,7 +116,7 @@ func (lat *AsyncTasker) getOrNewScheduler(task AsyncTask) (s *scheduler, isNew b
return
}

func (lat *AsyncTasker) AddTask(ctx context.Context, task AsyncTask) error {
func (lat *AsyncTaskService) Add(ctx context.Context, task AsyncTask) error {
if task == nil || ctx == nil {
return errors.New("invalid parameters")
}
Expand All @@ -129,11 +129,11 @@ func (lat *AsyncTasker) AddTask(ctx context.Context, task AsyncTask) error {
return s.AddTask(ctx, task)
}

func (lat *AsyncTasker) DeferRemoveTask(key string) error {
func (lat *AsyncTaskService) DeferRemove(key string) error {
lat.lock.Lock()
if lat.isClose {
lat.lock.Unlock()
return errors.New("AsyncTasker is stopped")
return errors.New("AsyncTaskService is stopped")
}
_, exist := lat.schedules[key]
if !exist {
Expand All @@ -146,7 +146,7 @@ func (lat *AsyncTasker) DeferRemoveTask(key string) error {
return nil
}

func (lat *AsyncTasker) removeScheduler(key string) {
func (lat *AsyncTaskService) removeScheduler(key string) {
if s, ok := lat.schedules[key]; ok {
s.Close()
delete(lat.schedules, key)
Expand All @@ -155,7 +155,7 @@ func (lat *AsyncTasker) removeScheduler(key string) {
util.Logger().Debugf("remove scheduler, key is %s", key)
}

func (lat *AsyncTasker) LatestHandled(key string) (AsyncTask, error) {
func (lat *AsyncTaskService) LatestHandled(key string) (AsyncTask, error) {
lat.lock.RLock()
s, ok := lat.schedules[key]
lat.lock.RUnlock()
Expand All @@ -165,12 +165,12 @@ func (lat *AsyncTasker) LatestHandled(key string) (AsyncTask, error) {
return s.latestTask, nil
}

func (lat *AsyncTasker) daemon(stopCh <-chan struct{}) {
func (lat *AsyncTaskService) daemon(stopCh <-chan struct{}) {
util.SafeCloseChan(lat.ready)
for {
select {
case <-stopCh:
util.Logger().Debugf("daemon thread exited for AsyncTasker is stopped")
util.Logger().Debugf("daemon thread exited for AsyncTaskService is stopped")
return
case <-time.After(DEFAULT_REMOVE_TASKS_INTERVAL):
if lat.isClose {
Expand All @@ -189,7 +189,7 @@ func (lat *AsyncTasker) daemon(stopCh <-chan struct{}) {
}
}

func (lat *AsyncTasker) Run() {
func (lat *AsyncTaskService) Run() {
lat.lock.Lock()
if !lat.isClose {
lat.lock.Unlock()
Expand All @@ -200,7 +200,7 @@ func (lat *AsyncTasker) Run() {
lat.goroutine.Do(lat.daemon)
}

func (lat *AsyncTasker) Stop() {
func (lat *AsyncTaskService) Stop() {
lat.lock.Lock()
if lat.isClose {
lat.lock.Unlock()
Expand All @@ -217,16 +217,14 @@ func (lat *AsyncTasker) Stop() {
lat.goroutine.Close(true)

util.SafeCloseChan(lat.ready)

util.Logger().Debugf("AsyncTasker is stopped")
}

func (lat *AsyncTasker) Ready() <-chan struct{} {
func (lat *AsyncTaskService) Ready() <-chan struct{} {
return lat.ready
}

func NewAsyncTasker() *AsyncTasker {
return &AsyncTasker{
func NewAsyncTaskService() *AsyncTaskService {
return &AsyncTaskService{
schedules: make(map[string]*scheduler, DEFAULT_MAX_SCHEDULE_COUNT),
removeTasks: make(map[string]struct{}, DEFAULT_MAX_SCHEDULE_COUNT),
goroutine: util.NewGo(make(chan struct{})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//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 store
package async

import (
"testing"
Expand Down Expand Up @@ -72,19 +72,19 @@ func (tt *testTask) Do(ctx context.Context) error {
}

func TestBaseAsyncTasker_AddTask(t *testing.T) {
at := NewAsyncTasker()
at := NewAsyncTaskService()
at.Run()
defer at.Stop()

ctx, cancel := context.WithCancel(context.Background())
err := at.AddTask(ctx, nil)
err := at.Add(ctx, nil)
if err == nil {
fail(t, "add nil task should be error")
}
cancel()

testCtx1, testC1 := context.WithCancel(context.Background())
err = at.AddTask(testCtx1, &testTask{
err = at.Add(testCtx1, &testTask{
done: testC1,
test: "test1",
})
Expand All @@ -97,7 +97,7 @@ func TestBaseAsyncTasker_AddTask(t *testing.T) {
}

testCtx2, testC2 := context.WithTimeout(context.Background(), 3*time.Second)
err = at.AddTask(testCtx2, &testTask{
err = at.Add(testCtx2, &testTask{
done: testC2,
test: "test2",
})
Expand All @@ -112,12 +112,12 @@ func TestBaseAsyncTasker_AddTask(t *testing.T) {
}

func TestBaseAsyncTasker_Stop(t *testing.T) {
at := NewAsyncTasker()
at := NewAsyncTaskService()
at.Stop()
at.Run()

_, cancel := context.WithCancel(context.Background())
err := at.AddTask(context.Background(), &testTask{
err := at.Add(context.Background(), &testTask{
done: cancel,
test: "test stop",
result: true,
Expand All @@ -126,7 +126,7 @@ func TestBaseAsyncTasker_Stop(t *testing.T) {
fail(t, "add task should be ok")
}
_, cancel = context.WithCancel(context.Background())
err = at.AddTask(context.Background(), &testTask{
err = at.Add(context.Background(), &testTask{
done: cancel,
test: "test stop",
wait: 3 * time.Second,
Expand All @@ -138,7 +138,7 @@ func TestBaseAsyncTasker_Stop(t *testing.T) {
<-time.After(time.Second)
at.Stop()

err = at.AddTask(context.Background(), &testTask{result: true})
err = at.Add(context.Background(), &testTask{result: true})
if err != nil {
fail(t, "add task should be ok when Tasker is stopped")
}
Expand All @@ -147,15 +147,15 @@ func TestBaseAsyncTasker_Stop(t *testing.T) {
}

func TestBaseAsyncTasker_RemoveTask(t *testing.T) {
at := NewAsyncTasker()
at := NewAsyncTaskService()
at.Run()

err := at.DeferRemoveTask("test")
err := at.DeferRemove("test")
if err != nil {
fail(t, "remove task should be ok")
}
_, cancel := context.WithCancel(context.Background())
err = at.AddTask(context.Background(), &testTask{
err = at.Add(context.Background(), &testTask{
done: cancel,
test: "test remove task",
result: true,
Expand All @@ -166,13 +166,13 @@ func TestBaseAsyncTasker_RemoveTask(t *testing.T) {
}
fmt.Println("OK")

err = at.DeferRemoveTask("test")
err = at.DeferRemove("test")
if err != nil {
fail(t, "remove task should be ok")
}
at.Stop()

err = at.DeferRemoveTask("test")
err = at.DeferRemove("test")
if err == nil {
fail(t, "remove task should be error when Tasker is stopped")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func SetReqCtx(r *http.Request, key string, val interface{}) {
}
}

func ParseTenantProject(ctx context.Context) string {
return StringJoin([]string{ParseTenant(ctx), ParseProject(ctx)}, "/")
func ParseDomainProject(ctx context.Context) string {
return ParseDomain(ctx) + "/" + ParseProject(ctx)
}

func ParseTenant(ctx context.Context) string {
v, ok := FromContext(ctx, "tenant").(string)
func ParseDomain(ctx context.Context) string {
v, ok := FromContext(ctx, "domain").(string)
if !ok {
return ""
}
Expand Down

0 comments on commit 3f75851

Please sign in to comment.