diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-01-03 19:04:00 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-02-05 12:59:42 +0100 |
commit | 89d2c5ed7a054bc05a21209d5a9c79ad7151f8f7 (patch) | |
tree | e19022fe717ddfb840599bf68e4c5a9268f8c278 /peer.go | |
parent | dff424baf8acc4d2a5858d3947761c5090d8e640 (diff) |
Extend structs rather than embed, when possible
Diffstat (limited to 'peer.go')
-rw-r--r-- | peer.go | 46 |
1 files changed, 23 insertions, 23 deletions
@@ -19,7 +19,7 @@ const ( type Peer struct { isRunning AtomicBool - mutex sync.RWMutex // Mostly protects endpoint, but is generally taken whenever we modify peer + sync.RWMutex // Mostly protects endpoint, but is generally taken whenever we modify peer keypairs Keypairs handshake Handshake device *Device @@ -57,10 +57,10 @@ type Peer struct { } routines struct { - mutex sync.Mutex // held when stopping / starting routines - starting sync.WaitGroup // routines pending start - stopping sync.WaitGroup // routines pending stop - stop chan struct{} // size 0, stop all go routines in peer + sync.Mutex // held when stopping / starting routines + starting sync.WaitGroup // routines pending start + stopping sync.WaitGroup // routines pending stop + stop chan struct{} // size 0, stop all go routines in peer } cookieGenerator CookieGenerator @@ -74,11 +74,11 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) { // lock resources - device.staticIdentity.mutex.RLock() - defer device.staticIdentity.mutex.RUnlock() + device.staticIdentity.RLock() + defer device.staticIdentity.RUnlock() - device.peers.mutex.Lock() - defer device.peers.mutex.Unlock() + device.peers.Lock() + defer device.peers.Unlock() // check if over limit @@ -89,8 +89,8 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) { // create peer peer := new(Peer) - peer.mutex.Lock() - defer peer.mutex.Unlock() + peer.Lock() + defer peer.Unlock() peer.cookieGenerator.Init(pk) peer.device = device @@ -126,15 +126,15 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) { } func (peer *Peer) SendBuffer(buffer []byte) error { - peer.device.net.mutex.RLock() - defer peer.device.net.mutex.RUnlock() + peer.device.net.RLock() + defer peer.device.net.RUnlock() if peer.device.net.bind == nil { return errors.New("no bind") } - peer.mutex.RLock() - defer peer.mutex.RUnlock() + peer.RLock() + defer peer.RUnlock() if peer.endpoint == nil { return errors.New("no known endpoint for peer") @@ -162,8 +162,8 @@ func (peer *Peer) Start() { // prevent simultaneous start/stop operations - peer.routines.mutex.Lock() - defer peer.routines.mutex.Unlock() + peer.routines.Lock() + defer peer.routines.Unlock() if peer.isRunning.Get() { return @@ -207,14 +207,14 @@ func (peer *Peer) ZeroAndFlushAll() { // clear key pairs keypairs := &peer.keypairs - keypairs.mutex.Lock() + keypairs.Lock() device.DeleteKeypair(keypairs.previous) device.DeleteKeypair(keypairs.current) device.DeleteKeypair(keypairs.next) keypairs.previous = nil keypairs.current = nil keypairs.next = nil - keypairs.mutex.Unlock() + keypairs.Unlock() // clear handshake state @@ -237,8 +237,8 @@ func (peer *Peer) Stop() { peer.routines.starting.Wait() - peer.routines.mutex.Lock() - defer peer.routines.mutex.Unlock() + peer.routines.Lock() + defer peer.routines.Unlock() peer.device.log.Debug.Println(peer, "- Stopping...") @@ -264,7 +264,7 @@ func (peer *Peer) SetEndpointFromPacket(endpoint Endpoint) { if roamingDisabled { return } - peer.mutex.Lock() + peer.Lock() peer.endpoint = endpoint - peer.mutex.Unlock() + peer.Unlock() } |