Reverting a few lines from PR #2406

As discussed with @mitchellh and @phinze we don’t need to randomize in
order to get infinite ID’s. When we hit the highest possible number and
add `1` it will just wrap back to `0`, which is just fine with regards
to how Terraform works and uses these ID’s.

Tested this by setting the initial value of `m.nextId` to `4294967293`
where the maximum is `4294967295 `. So with some additional logging it
clearly showed it wrapped and continued without any issues.
This commit is contained in:
Sander van Harmelen 2015-06-25 16:28:04 +02:00
parent 2423d104e6
commit a79c987538
1 changed files with 4 additions and 6 deletions

View File

@ -3,9 +3,9 @@ package rpc
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math/rand"
"net" "net"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/hashicorp/yamux" "github.com/hashicorp/yamux"
@ -17,6 +17,7 @@ import (
// or accept a connection from, and the broker handles the details of // or accept a connection from, and the broker handles the details of
// holding these channels open while they're being negotiated. // holding these channels open while they're being negotiated.
type muxBroker struct { type muxBroker struct {
nextId uint32
session *yamux.Session session *yamux.Session
streams map[uint32]*muxBrokerPending streams map[uint32]*muxBrokerPending
@ -94,12 +95,9 @@ func (m *muxBroker) Dial(id uint32) (net.Conn, error) {
return stream, nil return stream, nil
} }
// NextId returns a unique ID to use next. There is no need for seeding the // NextId returns a unique ID to use next.
// rand package as the returned ID's aren't stored or used anywhere outside
// the current runtime. So it's perfectly fine to get the same pseudo-random
// numbers each time terraform is running.
func (m *muxBroker) NextId() uint32 { func (m *muxBroker) NextId() uint32 {
return rand.Uint32() return atomic.AddUint32(&m.nextId, 1)
} }
// Run starts the brokering and should be executed in a goroutine, since it // Run starts the brokering and should be executed in a goroutine, since it