-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: router supports a more extensive set of rules. (#697)
# Description Refactor `Router` interface, the new interface is: ```go // Router routes data that is written by source/sfn according to parameters be passed. // Users should define their own rules that tells zipper how to route data and how to store the rules. type Router interface { // Add adds the route rule to the router. Add(connID string, observeDataTags []uint32, md metadata.M) error // Route gets the ID list of connections from the router. Route(dataTag uint32, md metadata.M) (connIDs []string) // Remove removes the route rule from the router. Remove(connID string) // Release release the router and removes all the route rules. Release() } ```
- Loading branch information
Showing
7 changed files
with
132 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,86 @@ | ||
// Package router defines the interface of router and route. | ||
// Package router defines the interface of router. | ||
package router | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/yomorun/yomo/core/frame" | ||
"github.com/yomorun/yomo/core/metadata" | ||
) | ||
|
||
// Router is the interface to manage the routes for applications. | ||
// Router routes data that is written by source/sfn according to parameters be passed. | ||
// Users should define their own rules that tells zipper how to route data and how to store the rules. | ||
type Router interface { | ||
// Route gets the route | ||
Route(metadata metadata.M) Route | ||
// Clean the routes. | ||
Clean() | ||
// Add adds the route rule to the router. | ||
Add(connID string, observeDataTags []uint32, md metadata.M) error | ||
// Route gets the ID list of connections from the router. | ||
Route(dataTag uint32, md metadata.M) (connIDs []string) | ||
// Remove removes the route rule from the router. | ||
Remove(connID string) | ||
// Release release the router and removes all the route rules. | ||
Release() | ||
} | ||
|
||
// Route manages data subscribers according to their observed data tags. | ||
type Route interface { | ||
// Add a route. | ||
Add(connID string, observeDataTags []frame.Tag) error | ||
// Remove a route. | ||
Remove(connID string) error | ||
// GetForwardRoutes returns all the subscribers by the given data tag. | ||
GetForwardRoutes(tag frame.Tag) (connIDs []string) | ||
type defaultRouter struct { | ||
// mu protects data. | ||
mu sync.RWMutex | ||
|
||
// data stores tag and connID connection. | ||
// The key is frame tag, The value is connID connection. | ||
data map[frame.Tag]map[string]struct{} | ||
} | ||
|
||
// DefaultRouter provides a default implementation of `router`, | ||
// It routes data according to observed tag or connID. | ||
func Default() *defaultRouter { | ||
return &defaultRouter{ | ||
data: make(map[frame.Tag]map[string]struct{}), | ||
} | ||
} | ||
|
||
func (r *defaultRouter) Add(connID string, ObserveDataTags []uint32, md metadata.M) error { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
for _, tag := range ObserveDataTags { | ||
conns := r.data[tag] | ||
if conns == nil { | ||
conns = map[string]struct{}{} | ||
r.data[tag] = conns | ||
} | ||
r.data[tag][connID] = struct{}{} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *defaultRouter) Route(dataTag uint32, md metadata.M) []string { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
|
||
var connID []string | ||
if conns, ok := r.data[dataTag]; ok { | ||
for k := range conns { | ||
connID = append(connID, k) | ||
} | ||
} | ||
return connID | ||
} | ||
|
||
func (r *defaultRouter) Remove(connID string) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
for _, conns := range r.data { | ||
delete(conns, connID) | ||
} | ||
} | ||
|
||
func (r *defaultRouter) Release() { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
for key := range r.data { | ||
delete(r.data, key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package router | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yomorun/yomo/core/metadata" | ||
) | ||
|
||
func TestRouter(t *testing.T) { | ||
router := Default() | ||
|
||
err := router.Add("conn-1", []uint32{1}, metadata.M{}) | ||
assert.NoError(t, err) | ||
|
||
err = router.Add("conn-2", []uint32{1}, metadata.M{}) | ||
assert.NoError(t, err) | ||
|
||
err = router.Add("conn-3", []uint32{1}, metadata.M{}) | ||
assert.NoError(t, err) | ||
|
||
ids := router.Route(1, nil) | ||
assert.ElementsMatch(t, []string{"conn-1", "conn-2", "conn-3"}, ids) | ||
|
||
router.Remove("conn-1") | ||
|
||
ids = router.Route(1, nil) | ||
assert.ElementsMatch(t, []string{"conn-2", "conn-3"}, ids) | ||
|
||
router.Release() | ||
|
||
ids = router.Route(1, nil) | ||
assert.Equal(t, []string(nil), ids) | ||
} |
Oops, something went wrong.
048dd08
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
yomo – ./
yomo-yomorun.vercel.app
yomo.run
www.yomo.run
yomo.vercel.app
yomo-git-master-yomorun.vercel.app