Skip to content

Commit

Permalink
refactor filter done
Browse files Browse the repository at this point in the history
  • Loading branch information
justxuewei committed Jul 4, 2021
1 parent 1a19eec commit 49ff6b0
Show file tree
Hide file tree
Showing 41 changed files with 268 additions and 169 deletions.
14 changes: 7 additions & 7 deletions common/extension/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,36 @@
package extension

import (
"dubbo.apache.org/dubbo-go/v3/filter/auth"
"dubbo.apache.org/dubbo-go/v3/filter"
)

var (
authenticators = make(map[string]func() auth.Authenticator)
accessKeyStorages = make(map[string]func() auth.AccessKeyStorage)
authenticators = make(map[string]func() filter.Authenticator)
accessKeyStorages = make(map[string]func() filter.AccessKeyStorage)
)

// SetAuthenticator puts the @fcn into map with name
func SetAuthenticator(name string, fcn func() auth.Authenticator) {
func SetAuthenticator(name string, fcn func() filter.Authenticator) {
authenticators[name] = fcn
}

// GetAuthenticator finds the Authenticator with @name
// Panic if not found
func GetAuthenticator(name string) auth.Authenticator {
func GetAuthenticator(name string) filter.Authenticator {
if authenticators[name] == nil {
panic("authenticator for " + name + " is not existing, make sure you have import the package.")
}
return authenticators[name]()
}

// SetAccessKeyStorages will set the @fcn into map with this name
func SetAccessKeyStorages(name string, fcn func() auth.AccessKeyStorage) {
func SetAccessKeyStorages(name string, fcn func() filter.AccessKeyStorage) {
accessKeyStorages[name] = fcn
}

// GetAccesskeyStorages finds the storage with the @name.
// Panic if not found
func GetAccesskeyStorages(name string) auth.AccessKeyStorage {
func GetAccesskeyStorages(name string) filter.AccessKeyStorage {
if accessKeyStorages[name] == nil {
panic("accessKeyStorages for " + name + " is not existing, make sure you have import the package.")
}
Expand Down
7 changes: 3 additions & 4 deletions common/extension/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ package extension

import (
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/filter/handler"
)

var (
filters = make(map[string]func() filter.Filter)
rejectedExecutionHandler = make(map[string]func() handler.RejectedExecutionHandler)
rejectedExecutionHandler = make(map[string]func() filter.RejectedExecutionHandler)
)

// SetFilter sets the filter extension with @name
Expand All @@ -42,12 +41,12 @@ func GetFilter(name string) filter.Filter {
}

// SetRejectedExecutionHandler sets the RejectedExecutionHandler with @name
func SetRejectedExecutionHandler(name string, creator func() handler.RejectedExecutionHandler) {
func SetRejectedExecutionHandler(name string, creator func() filter.RejectedExecutionHandler) {
rejectedExecutionHandler[name] = creator
}

// GetRejectedExecutionHandler finds the RejectedExecutionHandler with @name
func GetRejectedExecutionHandler(name string) handler.RejectedExecutionHandler {
func GetRejectedExecutionHandler(name string) filter.RejectedExecutionHandler {
creator, ok := rejectedExecutionHandler[name]
if !ok {
panic("RejectedExecutionHandler for " + name + " is not existing, make sure you have import the package " +
Expand Down
15 changes: 7 additions & 8 deletions common/extension/tps_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@
package extension

import (
"dubbo.apache.org/dubbo-go/v3/filter/tps/limiter"
"dubbo.apache.org/dubbo-go/v3/filter/tps/strategy"
"dubbo.apache.org/dubbo-go/v3/filter"
)

var (
tpsLimitStrategy = make(map[string]strategy.TpsLimitStrategyCreator)
tpsLimiter = make(map[string]func() limiter.TpsLimiter)
tpsLimitStrategy = make(map[string]filter.TpsLimitStrategyCreator)
tpsLimiter = make(map[string]func() filter.TpsLimiter)
)

// SetTpsLimiter sets the TpsLimiter with @name
func SetTpsLimiter(name string, creator func() limiter.TpsLimiter) {
func SetTpsLimiter(name string, creator func() filter.TpsLimiter) {
tpsLimiter[name] = creator
}

// GetTpsLimiter finds the TpsLimiter with @name
func GetTpsLimiter(name string) limiter.TpsLimiter {
func GetTpsLimiter(name string) filter.TpsLimiter {
creator, ok := tpsLimiter[name]
if !ok {
panic("TpsLimiter for " + name + " is not existing, make sure you have import the package " +
Expand All @@ -43,12 +42,12 @@ func GetTpsLimiter(name string) limiter.TpsLimiter {
}

// SetTpsLimitStrategy sets the TpsLimitStrategyCreator with @name
func SetTpsLimitStrategy(name string, creator strategy.TpsLimitStrategyCreator) {
func SetTpsLimitStrategy(name string, creator filter.TpsLimitStrategyCreator) {
tpsLimitStrategy[name] = creator
}

// GetTpsLimitStrategyCreator finds the TpsLimitStrategyCreator with @name
func GetTpsLimitStrategyCreator(name string) strategy.TpsLimitStrategyCreator {
func GetTpsLimitStrategyCreator(name string) filter.TpsLimitStrategyCreator {
creator, ok := tpsLimitStrategy[name]
if !ok {
panic("TpsLimitStrategy for " + name + " is not existing, make sure you have import the package " +
Expand Down
2 changes: 1 addition & 1 deletion filter/auth/access_key.go → filter/access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package auth
package filter

import (
"dubbo.apache.org/dubbo-go/v3/common"
Expand Down
9 changes: 8 additions & 1 deletion filter/accesslog/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const (
// used in URL.

// nolint
FileDateFormat = "2006-01-02"
// nolint
Expand All @@ -51,6 +52,12 @@ const (
Arguments = "arguments"
)

func init() {
extension.SetFilter(constant.ACCESS_LOG_KEY, func() filter.Filter {
return NewFilter()
})
}

// Filter for Access Log
/**
* Although the access log filter is a default filter,
Expand Down
9 changes: 9 additions & 0 deletions filter/active/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
invocation2 "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
)

const (
dubboInvokeStartTime = "dubboInvokeStartTime"
Active = "active"
)

func init() {
extension.SetFilter(Active, func() filter.Filter {
return &Filter{}
})
}

// Filter tracks the requests status
type Filter struct{}

Expand Down
7 changes: 4 additions & 3 deletions filter/auth/accesskey_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
)

func init() {
extension.SetAccessKeyStorages(constant.ACCESS_KEY_STORAGE_KEY, func() AccessKeyStorage {
extension.SetAccessKeyStorages(constant.ACCESS_KEY_STORAGE_KEY, func() filter.AccessKeyStorage {
return &DefaultAccesskeyStorage{}
})
}
Expand All @@ -34,8 +35,8 @@ func init() {
type DefaultAccesskeyStorage struct{}

// GetAccessKeyPair retrieves AccessKeyPair from url by the key "accessKeyId" and "secretAccessKey"
func (storage *DefaultAccesskeyStorage) GetAccessKeyPair(invocation protocol.Invocation, url *common.URL) *AccessKeyPair {
return &AccessKeyPair{
func (storage *DefaultAccesskeyStorage) GetAccessKeyPair(invocation protocol.Invocation, url *common.URL) *filter.AccessKeyPair {
return &filter.AccessKeyPair{
AccessKey: url.GetParam(constant.ACCESS_KEY_ID_KEY, ""),
SecretKey: url.GetParam(constant.SECRET_ACCESS_KEY_KEY, ""),
}
Expand Down
14 changes: 13 additions & 1 deletion filter/auth/consumer_sign_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
)

func init() {
extension.SetFilter(constant.CONSUMER_SIGN_FILTER, func() filter.Filter {
return &ConsumerSignFilter{}
})
extension.SetFilter(constant.PROVIDER_AUTH_FILTER, func() filter.Filter {
return &ProviderAuthFilter{}
})
}

// ConsumerSignFilter signs the request on consumer side
type ConsumerSignFilter struct{}

Expand All @@ -35,7 +47,7 @@ func (csf *ConsumerSignFilter) Invoke(ctx context.Context, invoker protocol.Invo
logger.Infof("invoking ConsumerSign filter.")
url := invoker.GetURL()

err := doAuthWork(url, func(authenticator Authenticator) error {
err := doAuthWork(url, func(authenticator filter.Authenticator) error {
return authenticator.Sign(invocation, url)
})
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions filter/auth/default_authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package auth

import (
"dubbo.apache.org/dubbo-go/v3/filter"
"errors"
"fmt"
"strconv"
Expand All @@ -33,7 +34,7 @@ import (
)

func init() {
extension.SetAuthenticator(constant.DEFAULT_AUTHENTICATOR, func() Authenticator {
extension.SetAuthenticator(constant.DEFAULT_AUTHENTICATOR, func() filter.Authenticator {
return &DefaultAuthenticator{}
})
}
Expand Down Expand Up @@ -108,7 +109,7 @@ func (authenticator *DefaultAuthenticator) Authenticate(invocation protocol.Invo
return nil
}

func getAccessKeyPair(invocation protocol.Invocation, url *common.URL) (*AccessKeyPair, error) {
func getAccessKeyPair(invocation protocol.Invocation, url *common.URL) (*filter.AccessKeyPair, error) {
accesskeyStorage := extension.GetAccesskeyStorages(url.GetParam(constant.ACCESS_KEY_STORAGE_KEY, constant.DEFAULT_ACCESS_KEY_STORAGE))
accessKeyPair := accesskeyStorage.GetAccessKeyPair(invocation, url)
if accessKeyPair == nil || IsEmpty(accessKeyPair.AccessKey, false) || IsEmpty(accessKeyPair.SecretKey, true) {
Expand All @@ -118,7 +119,7 @@ func getAccessKeyPair(invocation protocol.Invocation, url *common.URL) (*AccessK
}
}

func doAuthWork(url *common.URL, do func(Authenticator) error) error {
func doAuthWork(url *common.URL, do func(filter.Authenticator) error) error {
shouldAuth := url.GetParamBool(constant.SERVICE_AUTH_KEY, false)
if shouldAuth {
authenticator := extension.GetAuthenticator(url.GetParam(constant.AUTHENTICATOR_KEY, constant.DEFAULT_AUTHENTICATOR))
Expand Down
3 changes: 2 additions & 1 deletion filter/auth/provider_auth_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package auth

import (
"context"
"dubbo.apache.org/dubbo-go/v3/filter"
)

import (
Expand All @@ -34,7 +35,7 @@ func (paf *ProviderAuthFilter) Invoke(ctx context.Context, invoker protocol.Invo
logger.Infof("invoking providerAuth filter.")
url := invoker.GetURL()

err := doAuthWork(url, func(authenticator Authenticator) error {
err := doAuthWork(url, func(authenticator filter.Authenticator) error {
return authenticator.Authenticate(invocation, url)
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion filter/auth/authenticator.go → filter/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package auth
package filter

import (
"dubbo.apache.org/dubbo-go/v3/common"
Expand Down
12 changes: 12 additions & 0 deletions filter/echo/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const (
Echo = "echo"
)

func init() {
extension.SetFilter(Echo, func() filter.Filter {
return &Filter{}
})
}

// Filter health check
// RPCService need a Echo method in consumer, if you want to use Filter
// eg:
Expand Down
11 changes: 11 additions & 0 deletions filter/execlmt/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
_ "dubbo.apache.org/dubbo-go/v3/filter/handler"
"dubbo.apache.org/dubbo-go/v3/protocol"
)

const (
ExecuteLimit = "execute"
)

func init() {
extension.SetFilter(ExecuteLimit, func() filter.Filter {
return NewFilter()
})
}

// Filter will limit the number of in-progress request and it's thread-safe.
/**
* example:
Expand Down
12 changes: 12 additions & 0 deletions filter/generic/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,22 @@ import (

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
invocation2 "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
)

const (
Generic = "generic"
)

func init() {
extension.SetFilter(Generic, func() filter.Filter {
return &Filter{}
})
}

// when do a generic invoke, struct need to be map

// nolint
Expand Down
9 changes: 9 additions & 0 deletions filter/generic/service_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/common/logger"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
invocation2 "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
)

const (
GenericService = "generic_service"
// nolint
GENERIC_SERIALIZATION_DEFAULT = "true"
)

func init() {
extension.SetFilter(GenericService, func() filter.Filter {
return &ServiceFilter{}
})
}

// nolint
type ServiceFilter struct{}

Expand Down
Loading

0 comments on commit 49ff6b0

Please sign in to comment.