-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
志宇
committed
Feb 25, 2020
1 parent
04ade12
commit 68c8de2
Showing
4 changed files
with
80 additions
and
132 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 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,52 @@ | ||
package visor | ||
|
||
import ( | ||
"context" | ||
"net/rpc" | ||
|
||
"github.com/SkycoinProject/dmsg" | ||
"github.com/SkycoinProject/dmsg/netutil" | ||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/SkycoinProject/skywire-mainnet/pkg/snet" | ||
) | ||
|
||
func isDone(ctx context.Context) bool { | ||
select { | ||
case <-ctx.Done(): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func ServeRPCClient(ctx context.Context, log logrus.FieldLogger, n *snet.Network, rpcS *rpc.Server, rAddr dmsg.Addr, errCh chan<- error) { | ||
for { | ||
var conn *snet.Conn | ||
err := netutil.NewDefaultRetrier(log).Do(ctx, func() (rErr error) { | ||
conn, rErr = n.Dial(ctx, snet.DmsgType, rAddr.PK, rAddr.Port) | ||
return rErr | ||
}) | ||
if err != nil { | ||
if errCh != nil { | ||
errCh <- err | ||
} | ||
return | ||
} | ||
if conn == nil { | ||
log.WithField("conn == nil", conn == nil). | ||
Fatal("An unexpected occurrence happened.") | ||
} | ||
|
||
connCtx, cancel := context.WithCancel(ctx) | ||
go func() { | ||
rpcS.ServeConn(conn) | ||
cancel() | ||
}() | ||
<-connCtx.Done() | ||
|
||
log.WithError(conn.Close()). | ||
WithField("context_done", isDone(ctx)). | ||
Debug("ServeRPCClient: Closed conn.") | ||
} | ||
} |
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