This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 147
/
chained_forwarder_server.go
50 lines (41 loc) · 1.7 KB
/
chained_forwarder_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Package vppagent provides sdk API for creating chaining vppagent forwarders
package vppagent
import (
"context"
"github.com/golang/protobuf/ptypes/empty"
"github.com/sirupsen/logrus"
"github.com/networkservicemesh/networkservicemesh/pkg/tools/spanhelper"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/crossconnect"
"github.com/networkservicemesh/networkservicemesh/forwarder/api/forwarder"
)
type chainedForwarderServer struct {
handlers []forwarder.ForwarderServer
}
func (c *chainedForwarderServer) Request(ctx context.Context, crossConnect *crossconnect.CrossConnect) (*crossconnect.CrossConnect, error) {
span := spanhelper.FromContext(ctx, "Forwarder.Request")
defer span.Finish()
span.LogObject("crossConnect", crossConnect)
if len(c.handlers) == 0 {
logrus.Info("chainedForwarderServer: has not handlers for next request")
return crossConnect, nil
}
next := &next{handlers: c.handlers, index: 0}
nextCtx := withNext(span.Context(), next)
return next.Request(nextCtx, crossConnect)
}
func (c *chainedForwarderServer) Close(ctx context.Context, crossConnect *crossconnect.CrossConnect) (*empty.Empty, error) {
span := spanhelper.FromContext(ctx, "Forwarder.Close")
span.LogObject("crossConnect", crossConnect)
defer span.Finish()
if len(c.handlers) == 0 {
logrus.Info("chainedForwarderServer: has not handlers for next close")
return new(empty.Empty), nil
}
next := &next{handlers: c.handlers, index: 0}
nextCtx := withNext(span.Context(), next)
return next.Close(nextCtx, crossConnect)
}
// ChainOf makes chain of forwarder server handlers
func ChainOf(handlers ...forwarder.ForwarderServer) forwarder.ForwarderServer {
return &chainedForwarderServer{handlers: handlers}
}