Skip to content

opt:(thrift) support concurrently register and find Annotation #100

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions thrift/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"strings"
"sync"

"github.com/cloudwego/dynamicgo/http"
"github.com/cloudwego/dynamicgo/meta"
Expand Down Expand Up @@ -156,28 +157,33 @@ type KeyMapping interface {

//--------------------------------- Thrid-party Register ----------------------------------

var annotations = map[string]map[AnnoScope]Annotation{}
// var annotations = map[string]map[AnnoScope]Annotation{}
var annotations sync.Map

// RegisterAnnotation register an annotation on specific AnnoScope
func RegisterAnnotation(an Annotation, keys ...string) {
for _, key := range keys {
key = strings.ToLower(key)
m := annotations[key]
m, _ := annotations.Load(key)
if m == nil {
m = make(map[AnnoScope]Annotation)
annotations[key] = m
m = new(sync.Map)
annotations.Store(key, m)
}
m[an.ID().Scope()] = an
m.(*sync.Map).Store(an.ID().Scope(), an)
}
}

func FindAnnotation(key string, scope AnnoScope) Annotation {
key = strings.ToLower(key)
m := annotations[key]
m, _ := annotations.Load(key)
if m == nil {
return nil
}
return m[scope]
x, _ := m.(*sync.Map).Load(scope)
if x == nil {
return nil
}
return x.(Annotation)
}

// makeAnnotation search an annotation by given key+scope and make its handler
Expand Down Expand Up @@ -208,35 +214,38 @@ type AnnotationMapper interface {
Map(ctx context.Context, ann []parser.Annotation, desc interface{}, opt Options) (cur []parser.Annotation, next []parser.Annotation, err error)
}

var annotationMapper = map[string]map[AnnoScope]AnnotationMapper{}
// var annotationMapper = map[string]map[AnnoScope]AnnotationMapper{}
var annotationMapper sync.Map

// RegisterAnnotationMapper register a annotation mapper on specific scope
func RegisterAnnotationMapper(scope AnnoScope, mapper AnnotationMapper, keys ...string) {
for _, key := range keys {
m := annotationMapper[key]
m, _ := annotationMapper.Load(key)
if m == nil {
m = make(map[AnnoScope]AnnotationMapper)
annotationMapper[key] = m
m = new(sync.Map)
annotationMapper.Store(key, m)
}
m[scope] = mapper
m.(*sync.Map).Store(scope, mapper)
}
}

func FindAnnotationMapper(key string, scope AnnoScope) AnnotationMapper {
m := annotationMapper[key]
m, _ := annotationMapper.Load(key)
if m == nil {
return nil
}
return m[scope]
x, _ := m.(*sync.Map).Load(scope)
if x == nil {
return nil
}
return x.(AnnotationMapper)
}

func RemoveAnnotationMapper(scope AnnoScope, keys ...string) {
for _, key := range keys {
m := annotationMapper[key]
m, _ := annotationMapper.Load(key)
if m != nil {
if _, ok := m[scope]; ok {
delete(m, scope)
}
m.(*sync.Map).Delete(scope)
}
}
}
Expand Down
Loading