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

Use retrier from the dmsg package netutil #1111

Merged
merged 7 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 46 additions & 40 deletions cmd/apps/skychat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ skychat app for skywire visor
package main

import (
"context"
"embed"
"encoding/json"
"flag"
Expand All @@ -19,9 +20,9 @@ import (
ipc "github.com/james-barrow/golang-ipc"
"github.com/skycoin/dmsg/buildinfo"
"github.com/skycoin/dmsg/cipher"
dmsgnetutil "github.com/skycoin/dmsg/netutil"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/routing"
Expand All @@ -35,7 +36,7 @@ const (

var log = logging.MustGetLogger("chat")
var addr = flag.String("addr", ":8001", "address to bind")
var r = netutil.NewRetrier(50*time.Millisecond, 5, 2, log)
var r = dmsgnetutil.NewRetrier(log, 50*time.Millisecond, dmsgnetutil.DefaultMaxBackoff, 5, 2)

var (
appC *app.Client
Expand Down Expand Up @@ -74,9 +75,11 @@ func main() {
}
go handleIPCSignal(ipcClient)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

http.Handle("/", http.FileServer(getFileSystem()))
http.HandleFunc("/message", messageHandler)
http.HandleFunc("/message", messageHandler(ctx))
http.HandleFunc("/sse", sseHandler)

fmt.Print("Serving HTTP on", *addr)
Expand Down Expand Up @@ -140,55 +143,58 @@ func handleConn(conn net.Conn) {
}
}

func messageHandler(w http.ResponseWriter, req *http.Request) {
data := map[string]string{}
if err := json.NewDecoder(req.Body).Decode(&data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

pk := cipher.PubKey{}
if err := pk.UnmarshalText([]byte(data["recipient"])); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
func messageHandler(ctx context.Context) func(w http.ResponseWriter, rreq *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {

addr := appnet.Addr{
Net: netType,
PubKey: pk,
Port: 1,
}
connsMu.Lock()
conn, ok := conns[pk]
connsMu.Unlock()
data := map[string]string{}
if err := json.NewDecoder(req.Body).Decode(&data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if !ok {
var err error
err = r.Do(func() error {
conn, err = appC.Dial(addr)
return err
})
if err != nil {
pk := cipher.PubKey{}
if err := pk.UnmarshalText([]byte(data["recipient"])); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

addr := appnet.Addr{
Net: netType,
PubKey: pk,
Port: 1,
}
connsMu.Lock()
conns[pk] = conn
conn, ok := conns[pk]
connsMu.Unlock()

go handleConn(conn)
}
if !ok {
var err error
err = r.Do(ctx, func() error {
conn, err = appC.Dial(addr)
return err
})
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

_, err := conn.Write([]byte(data["message"]))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
connsMu.Lock()
conns[pk] = conn
connsMu.Unlock()

connsMu.Lock()
delete(conns, pk)
connsMu.Unlock()
go handleConn(conn)
}

return
_, err := conn.Write([]byte(data["message"]))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)

connsMu.Lock()
delete(conns, pk)
connsMu.Unlock()

return
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions cmd/apps/skysocks-client/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ proxy client app for skywire visor
package main

import (
"context"
"flag"
"io"
"net"
Expand All @@ -13,8 +14,8 @@ import (
"github.com/sirupsen/logrus"
"github.com/skycoin/dmsg/buildinfo"
"github.com/skycoin/dmsg/cipher"
dmsgnetutil "github.com/skycoin/dmsg/netutil"

"github.com/skycoin/skywire/internal/netutil"
"github.com/skycoin/skywire/internal/skysocks"
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appnet"
Expand All @@ -29,11 +30,11 @@ const (

var log = logrus.New()

var r = netutil.NewRetrier(time.Second, 0, 1, log)
var r = dmsgnetutil.NewRetrier(log, time.Second, dmsgnetutil.DefaultMaxBackoff, 0, 1)

func dialServer(appCl *app.Client, pk cipher.PubKey) (net.Conn, error) {
func dialServer(ctx context.Context, appCl *app.Client, pk cipher.PubKey) (net.Conn, error) {
var conn net.Conn
err := r.Do(func() error {
err := r.Do(ctx, func() error {
var err error
conn, err = appCl.Dial(appnet.Addr{
Net: netType,
Expand All @@ -55,6 +56,9 @@ func main() {

skysocks.Log = log

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if _, err := buildinfo.Get().WriteTo(os.Stdout); err != nil {
log.Printf("Failed to output build info: %v", err)
}
Expand All @@ -74,7 +78,7 @@ func main() {
}

for {
conn, err := dialServer(appC, pk)
conn, err := dialServer(ctx, appC, pk)
if err != nil {
log.Fatalf("Failed to dial to a server: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/pkg/profile v1.5.0
github.com/shirou/gopsutil/v3 v3.21.4
github.com/sirupsen/logrus v1.8.1
github.com/skycoin/dmsg v0.0.0-20220125173526-46bb07d7fd64
github.com/skycoin/dmsg v0.0.0-20220302151611-e4368629eed2
github.com/skycoin/skycoin v0.27.1
github.com/skycoin/yamux v0.0.0-20200803175205-571ceb89da9f
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
Expand Down Expand Up @@ -63,7 +63,6 @@ require (
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect
github.com/getlantern/errors v1.0.1 // indirect
github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ github.com/getlantern/systray v1.1.0 h1:U0wCEqseLi2ok1fE6b88gJklzriavPJixZysZPkZ
github.com/getlantern/systray v1.1.0/go.mod h1:AecygODWIsBquJCJFop8MEQcJbWFfw/1yWbVabNgpCM=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f h1:6kLofhLkWj7lgCc+mvcVLnwhTzQYgL/yW/Y0e/JYwjg=
github.com/go-chi/chi/v5 v5.0.8-0.20220103230436-7dbe9a0bd10f/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
Expand Down Expand Up @@ -405,8 +403,8 @@ github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5k
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skycoin/dmsg v0.0.0-20220125173526-46bb07d7fd64 h1:XEg/CXSPx2JbZpxLxuxm2CeZ648yMXz+pymY+VIHbyk=
github.com/skycoin/dmsg v0.0.0-20220125173526-46bb07d7fd64/go.mod h1:EgRg8fy5RjF67OJlh9w+vhq3+Phyn6AXKSedkzhf1ww=
github.com/skycoin/dmsg v0.0.0-20220302151611-e4368629eed2 h1:K3iy7UEWuHE55HFTZIcg5fNYkyO/P5oxErioEQixFfA=
github.com/skycoin/dmsg v0.0.0-20220302151611-e4368629eed2/go.mod h1:9Q9sh2ph1GqdtTBOq4rg8VHsbE/WGBH20mntnN4ckfM=
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0duwyG+7WliWz5u9kgk1h5MnLuA=
github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6/go.mod h1:UXghlricA7J3aRD/k7p/zBObQfmBawwCxIVPVjz2Q3o=
github.com/skycoin/skycoin v0.26.0/go.mod h1:78nHjQzd8KG0jJJVL/j0xMmrihXi70ti63fh8vXScJw=
Expand Down Expand Up @@ -696,7 +694,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
106 changes: 0 additions & 106 deletions internal/netutil/retrier.go

This file was deleted.

Loading