-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start a relay v2 by default, add config options
- Loading branch information
1 parent
8a70f70
commit 71eb4bf
Showing
5 changed files
with
154 additions
and
16 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
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,94 @@ | ||
package relayv2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
v2relay "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay" | ||
|
||
"github.com/libp2p/go-libp2p-core/event" | ||
"github.com/libp2p/go-libp2p-core/host" | ||
"github.com/libp2p/go-libp2p-core/network" | ||
) | ||
|
||
type RelayManager struct { | ||
host host.Host | ||
|
||
mutex sync.Mutex | ||
relay *v2relay.Relay | ||
opts []v2relay.Option | ||
|
||
refCount sync.WaitGroup | ||
ctxCancel context.CancelFunc | ||
} | ||
|
||
func NewRelayManager(host host.Host, opts ...v2relay.Option) *RelayManager { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
m := &RelayManager{ | ||
host: host, | ||
opts: opts, | ||
ctxCancel: cancel, | ||
} | ||
m.refCount.Add(1) | ||
go m.background(ctx) | ||
return m | ||
} | ||
|
||
func (m *RelayManager) background(ctx context.Context) { | ||
defer m.refCount.Done() | ||
defer func() { | ||
m.mutex.Lock() | ||
if m.relay != nil { | ||
m.relay.Close() | ||
} | ||
m.mutex.Unlock() | ||
}() | ||
|
||
subReachability, _ := m.host.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged)) | ||
defer subReachability.Close() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case ev, ok := <-subReachability.Out(): | ||
if !ok { | ||
return | ||
} | ||
if err := m.reachabilityChanged(ev.(event.EvtLocalReachabilityChanged).Reachability); err != nil { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (m *RelayManager) reachabilityChanged(r network.Reachability) error { | ||
switch r { | ||
case network.ReachabilityPublic: | ||
fmt.Println("reachability changed to public") | ||
relay, err := v2relay.New(m.host, m.opts...) | ||
if err != nil { | ||
return err | ||
} | ||
m.mutex.Lock() | ||
defer m.mutex.Unlock() | ||
m.relay = relay | ||
case network.ReachabilityPrivate: | ||
fmt.Println("reachability changed to private") | ||
m.mutex.Lock() | ||
defer m.mutex.Unlock() | ||
if m.relay != nil { | ||
err := m.relay.Close() | ||
m.relay = nil | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (m *RelayManager) Close() error { | ||
m.ctxCancel() | ||
m.refCount.Wait() | ||
return nil | ||
} |