-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
168 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2018 Anapaya Systems | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// 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 handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/scionproto/scion/go/lib/common" | ||
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt" | ||
"github.com/scionproto/scion/go/lib/infra" | ||
"github.com/scionproto/scion/go/lib/infra/dedupe" | ||
) | ||
|
||
type segReq struct { | ||
segReq *path_mgmt.SegReq | ||
server net.Addr | ||
id uint64 | ||
} | ||
|
||
func (req *segReq) DedupeKey() string { | ||
return fmt.Sprintf("%s %s", req.segReq, req.server) | ||
} | ||
|
||
func (req *segReq) BroadcastKey() string { | ||
return fmt.Sprintf("%s %s", req.segReq, req.server) | ||
} | ||
|
||
type psDeduper struct { | ||
msger infra.Messenger | ||
} | ||
|
||
func (pd *psDeduper) segsRequestFunc(ctx context.Context, | ||
request dedupe.Request) dedupe.Response { | ||
|
||
req := request.(*segReq) | ||
segs, err := pd.msger.GetSegs(ctx, req.segReq, req.server, req.id) | ||
if err != nil { | ||
return dedupe.Response{Error: err} | ||
} | ||
return dedupe.Response{Data: segs} | ||
} | ||
|
||
func NewDeduper(msger infra.Messenger) *dedupe.Deduper { | ||
psd := &psDeduper{msger: msger} | ||
return dedupe.New(psd.segsRequestFunc, 0, 0) | ||
} | ||
|
||
func (h *segReqHandler) getSegsFromNetwork(ctx context.Context, | ||
req *path_mgmt.SegReq, server net.Addr, id uint64) (*path_mgmt.SegReply, error) { | ||
|
||
responseC, cancelF := h.segsDeduper.Request(ctx, &segReq{ | ||
segReq: req, | ||
server: server, | ||
id: id, | ||
}) | ||
defer cancelF() | ||
select { | ||
case response := <-responseC: | ||
if response.Error != nil { | ||
return nil, response.Error | ||
} | ||
return response.Data.(*path_mgmt.SegReply), nil | ||
case <-ctx.Done(): | ||
return nil, common.NewBasicError("Context done while waiting for Segs", | ||
ctx.Err()) | ||
} | ||
} |
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,74 @@ | ||
// Copyright 2018 Anapaya Systems | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// 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 handlers | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt" | ||
"github.com/scionproto/scion/go/lib/infra" | ||
"github.com/scionproto/scion/go/lib/infra/mock_infra" | ||
"github.com/scionproto/scion/go/lib/log" | ||
"github.com/scionproto/scion/go/lib/xtest" | ||
) | ||
|
||
func TestDedupe(t *testing.T) { | ||
Convey("getSegsFromNetwork should dedupe", t, func() { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
ctx, cancelF := context.WithTimeout(context.Background(), time.Second) | ||
defer cancelF() | ||
msger := mock_infra.NewMockMessenger(ctrl) | ||
ireq := &infra.Request{ | ||
Logger: log.Root(), | ||
} | ||
h := &segReqHandler{ | ||
baseHandler: newBaseHandler(ireq, HandlerArgs{}), | ||
segsDeduper: NewDeduper(msger), | ||
} | ||
reply := &path_mgmt.SegReply{} | ||
msger.EXPECT().GetSegs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( | ||
func(_ context.Context, _ *path_mgmt.SegReq, _ net.Addr, | ||
_ uint64) (*path_mgmt.SegReply, error) { | ||
|
||
time.Sleep(20 * time.Millisecond) | ||
return reply, nil | ||
}, | ||
) | ||
req := &path_mgmt.SegReq{ | ||
RawSrcIA: xtest.MustParseIA("1-ff00:0:110").IAInt(), | ||
RawDstIA: xtest.MustParseIA("1-ff00:0:211").IAInt(), | ||
} | ||
req2 := &path_mgmt.SegReq{ | ||
RawSrcIA: req.RawSrcIA, | ||
RawDstIA: req.RawDstIA, | ||
} | ||
Convey("Parallel", xtest.Parallel(func(sc *xtest.SC) { | ||
r, err := h.getSegsFromNetwork(ctx, req, nil, 1) | ||
sc.SoMsg("Should be no error", err, ShouldBeNil) | ||
sc.SoMsg("Should return single reply", r, ShouldEqual, reply) | ||
}, func(sc *xtest.SC) { | ||
r, err := h.getSegsFromNetwork(ctx, req2, nil, 2) | ||
sc.SoMsg("Should be no error", err, ShouldBeNil) | ||
sc.SoMsg("Should return single reply", r, ShouldEqual, reply) | ||
})) | ||
}) | ||
} |
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 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