Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap in goroutine crashed #5

Open
hiqsociety opened this issue Nov 9, 2022 · 4 comments
Open

wrap in goroutine crashed #5

hiqsociety opened this issue Nov 9, 2022 · 4 comments

Comments

@hiqsociety
Copy link

crash

package main

import (
        "context"
        "log"
        "time"
        //"strings"

        flatbuffers "github.com/google/flatbuffers/go"

        "github.com/smfrpc/smf-go/example/demo"
        "github.com/smfrpc/smf-go/example/demo_gen"
        "github.com/smfrpc/smf-go/src/smf"
)

//var xreq = strings.Repeat("x", 11)
var xreq = string([]byte{0,3,23,41})

func buildRequest() []byte {
        builder := flatbuffers.NewBuilder(0)
        name := builder.CreateString(xreq)
        demo.RequestStart(builder)
        demo.RequestAddName(builder, name)
        resp := demo.RequestEnd(builder)
        builder.Finish(resp)
        return builder.FinishedBytes()
}

func main() {
        client, err := smf.Dial("tcp", "127.0.0.1:20766")
        if err != nil {
                log.Fatal(err)
        }

        store := demo_gen.NewSmfStorageClient(client)
        //resp, err := store.Get(context.TODO(), buildRequest())
        start := time.Now()
        for i:=0;i<100000;i++ {
                go func() {
                        resp, err := store.Get(context.TODO(), buildRequest())
                        if err != nil {
        //                      log.Fatal(err)
                        }
                        log.Printf("Response: [ name=%q ]", resp.Name())
                }()
        }
        client.Close()

        elapsed := time.Since(start)
        log.Printf("Binomial took %s", elapsed)
        time.Sleep(100*time.Second)

}

@crackcomm
Copy link
Contributor

Hi.

That happens because of session counter, I assume.

What you could do is try using sync.Pool.

@hiqsociety
Copy link
Author

care to provide a working example?

i've tried
https://github.com/panjf2000/ants

didnt work too.

i understand the syncpool part... can u provide the example? thx

@crackcomm
Copy link
Contributor

What I meant is creating a pool of *smf.Client objects.

package main

import (
	"context"
	"log"
	"sync"
	"time"

	flatbuffers "github.com/google/flatbuffers/go"

	"github.com/smfrpc/smf-go/example/demo"
	"github.com/smfrpc/smf-go/example/demo_gen"
	"github.com/smfrpc/smf-go/src/smf"
)

// var xreq = strings.Repeat("x", 11)
var xreq = string([]byte{0, 3, 23, 41})

func buildRequest() []byte {
	builder := flatbuffers.NewBuilder(0)
	name := builder.CreateString(xreq)
	demo.RequestStart(builder)
	demo.RequestAddName(builder, name)
	resp := demo.RequestEnd(builder)
	builder.Finish(resp)
	return builder.FinishedBytes()
}

func main() {
	pool := sync.Pool{
		New: func() any {
			client, err := smf.Dial("tcp", "127.0.0.1:20766")
			if err != nil {
				log.Fatal(err)
			}
			return client
		},
	}

	start := time.Now()
	for i := 0; i < 100000; i++ {
		go func() {
			client := pool.Get().(*smf.Client)
			defer pool.Put(client)
			store := demo_gen.NewSmfStorageClient(client)
			resp, err := store.Get(context.TODO(), buildRequest())
			if err != nil {
				log.Fatal(err)
			}
			log.Printf("Response: [ name=%q ]", resp.Name())
		}()
	}

	elapsed := time.Since(start)
	log.Printf("Binomial took %s", elapsed)
	time.Sleep(100 * time.Second)

}

@crackcomm
Copy link
Contributor

crackcomm commented Nov 9, 2022

Take in mind this benchmark will not result in an accurate result.

This code will start 100000 goroutines concurrently and not wait for them. It will create tens of thousands of connections. Connection time will be added on top of the RPC call time even if you do wait for the goroutines to exit.

What I would do instead is spin up one goroutine for each connection and measure only time between request and response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants