Skip to content

Commit

Permalink
transport memory: fix races
Browse files Browse the repository at this point in the history
* fix race with rand.Intn for non default source
* increase random interval to avoid issues when many services
  running on the host

Signed-off-by: Vasiliy Tolstov <[email protected]>
  • Loading branch information
vtolstov committed Jul 13, 2019
1 parent 2fecde1 commit 81d2259
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions transport/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
"github.com/micro/go-micro/transport"
)

var (
r = rand.New(rand.NewSource(time.Now().UnixNano()))
)

type memorySocket struct {
recv chan *transport.Message
send chan *transport.Message
Expand Down Expand Up @@ -178,7 +174,7 @@ func (m *memoryTransport) Listen(addr string, opts ...transport.ListenOption) (t

// if zero port then randomly assign one
if len(parts) > 1 && parts[len(parts)-1] == "0" {
i := r.Intn(10000)
i := rand.Intn(20000)
// set addr with port
addr = fmt.Sprintf("%s:%d", parts[:len(parts)-1], 10000+i)
}
Expand Down Expand Up @@ -215,6 +211,7 @@ func (m *memoryTransport) String() string {
}

func NewTransport(opts ...transport.Option) transport.Transport {
rand.Seed(time.Now().UnixNano())
var options transport.Options
for _, o := range opts {
o(&options)
Expand Down

0 comments on commit 81d2259

Please sign in to comment.