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

Support register listener filters. #1014

Merged
merged 11 commits into from Mar 13, 2020
Merged

Conversation

wangfakang
Copy link
Contributor

Issues associated with this PR

Support register listener filters, such as original_dst listener filter.

Note Need to merge #1005 first .

Solutions

Execution phase

When a new connection is received, MOSN's OnAccept execution process is triggered. The listener filter will execute before the connection instance created. As follows

// ListenerEventListener
func (al *activeListener) OnAccept(rawc net.Conn, useOriginalDst bool, oriRemoteAddr net.Addr, ch chan types.Connection, buf []byte) {
	var rawf *os.File
    
	arc := newActiveRawConn(rawc, al) 

	arc.ContinueFilterChain(ctx, true)
}

func (arc *activeRawConn) ContinueFilterChain(ctx context.Context, success bool) {
        //execute listener filter handler
	for ; arc.acceptedFilterIndex < len(arc.acceptedFilters); arc.acceptedFilterIndex++ {
		filterStatus := arc.acceptedFilters[arc.acceptedFilterIndex].OnAccept(arc)   // running filter 
		if filterStatus == types.Stop {
			return
		}
	}
    // Create connection
    arc.activeListener.newConnection(ctx, arc.rawc)
}

Filter registration API

RegisterListener API is provided to RegisterListener filter instances. The listener filter instance only need to implement ListenerFilterChainFactory interface (such as realize OnAccept method) and ListenerFilterFactoryCreator type method for parse the filter configuration.

// ListenerFilterFactoryCreator creates a ListenerFilterChainFactory according to config
type ListenerFilterFactoryCreator func(config map[string]interface{}) (ListenerFilterChainFactory, error)

var creatorListenerFactory map[string]ListenerFilterFactoryCreator

func init() {
	creatorListenerFactory = make(map[string]ListenerFilterFactoryCreator)
}

// RegisterListener registers the filterType as ListenerFilterFactoryCreator
func RegisterListener(filterType string, creator ListenerFilterFactoryCreator) {
	creatorListenerFactory[filterType] = creator
}

// ListenerFilterChainFactory adds filter into ListenerFilterChainFactoryCallbacks
type ListenerFilterChainFactory interface {
	OnAccept(callbacks ListenerFilterChainFactoryCallbacks) FilterStatus
}

// CreateListenerFilterChainFactory creates a ListenerFilterChainFactory according to filterType
func CreateListenerFilterChainFactory(filterType string, config map[string]interface{}) (ListenerFilterChainFactory, error) {
	if cf, ok := creatorListenerFactory[filterType]; ok {
		lfcf, err := cf(config)
		if err != nil {
			return nil, fmt.Errorf("create listener filter chain factory failed: %v", err)
		}
		return lfcf, nil
	}
	return nil, fmt.Errorf("unsupported listener filter type: %v", filterType)
}

Configuration

Support for both static and XDS configuration, such as

"listener_filters": [
                {
                  "type": "orginal_dst",
                  "config": {
                   }
                }
           ]

UT result

Unit Test is needed if the code is changed, your unit test should cover boundary cases, corner cases, and some exceptional cases. And you need to show the UT result.

Benchmark

If your code involves the processing of every request, you should give the Benchmark Result.

Code Style

  • Make sure Goimports has run
  • Show Golint result

@wangfakang wangfakang requested review from nejisama and taoyuanyuan and removed request for nejisama March 3, 2020 06:38
@codecov-io
Copy link

codecov-io commented Mar 4, 2020

Codecov Report

Merging #1014 into master will increase coverage by 0.03%.
The diff coverage is 52.43%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1014      +/-   ##
==========================================
+ Coverage   56.59%   56.62%   +0.03%     
==========================================
  Files         238      238              
  Lines       22737    22786      +49     
==========================================
+ Hits        12868    12903      +35     
- Misses       8710     8722      +12     
- Partials     1159     1161       +2     
Impacted Files Coverage Δ
pkg/config/v2/filter.go 50.00% <ø> (ø)
pkg/config/v2/server.go 91.30% <ø> (ø)
pkg/config/v2/upstream.go 70.83% <ø> (ø)
pkg/configmanager/parser.go 44.52% <0.00%> (-3.14%) ⬇️
pkg/mosn/starter.go 33.12% <0.00%> (ø)
pkg/server/server.go 0.00% <0.00%> (ø)
pkg/types/network.go 0.00% <ø> (ø)
pkg/upstream/cluster/host.go 54.92% <20.00%> (-6.98%) ⬇️
pkg/server/handler.go 41.17% <52.94%> (+0.64%) ⬆️
pkg/server/adapter.go 64.70% <66.66%> (ø)
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02a352c...bfa5cc0. Read the comment docs.

pkg/server/handler.go Outdated Show resolved Hide resolved
pkg/server/handler.go Outdated Show resolved Hide resolved
cmd/mosn/main/mosn.go Outdated Show resolved Hide resolved
@nejisama nejisama merged commit f23e3a6 into mosn:master Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants