Giter VIP home page Giter VIP logo

Comments (4)

dashessmith avatar dashessmith commented on June 11, 2024 1

image

this bytes.buffer seems not thread safe

from go-micro.

DingYuan0118 avatar DingYuan0118 commented on June 11, 2024

Encounter the same problem. Use grpc server seems to avoid this

from go-micro.

DingYuan0118 avatar DingYuan0118 commented on June 11, 2024
image Seems all buf in channel use the same pointer which point to the underlying []byte since buf.Reset() does not change buf's address buf.Bytes() always return the same pointer.

use copy() to make a new []byte and then put it into channel can solve this.

func TestBuffer(t *testing.T) {
	wg := sync.WaitGroup{}
	defer wg.Wait()

	var ch = make(chan []byte, 128)
	wg.Add(1)
	go func() {
		defer wg.Done()

		buf := bytes.Buffer{}

		defer close(ch)

		for i := 1; i < 10; i++ {
			buf.Write([]byte(fmt.Sprintf("%v", i)))
			bs := buf.Bytes()
			buf.Reset()
			b := make([]byte, len(bs))
			copy(b, bs)
			ch <- b
		}

	}()

	wg.Add(1)

	go func() {
		defer wg.Done()
		for x := range ch {
			fmt.Printf("i:=%v\n", string(x))
		}
	}()
}


i:=1
i:=2
i:=3
i:=4
i:=5
i:=6
i:=7
i:=8
i:=9

from go-micro.

DingYuan0118 avatar DingYuan0118 commented on June 11, 2024
image

Change like this seems to make it work normal.

from go-micro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.