This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type slot struct { | |
| mark uint32 | |
| req request | |
| ch chan response | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type ring struct { | |
| _ [8]uint64 // padding to reduce false sharing | |
| write uint64 | |
| _ [7]uint64 | |
| read1 uint64 | |
| _ [7]uint64 | |
| read2 uint64 | |
| _ [7]uint64 | |
| slots [1024]slot | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type PipeliningQueue interface { | |
| EnqueueRequest(req request) response | |
| NextRequestToSend() (req request, ok bool) | |
| ReplyToNextRequest(resp response) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ▶ go test -bench=BenchmarkClient -benchmem -benchtime 2s . | |
| goos: darwin | |
| goarch: arm64 | |
| pkg: rueidis-benchmark | |
| BenchmarkClient | |
| BenchmarkClient/Rueidis-parallelism(64)-key(16)-value(64) | |
| BenchmarkClient/Rueidis-parallelism(64)-key(16)-value(64)-10 4513908 532.5 ns/op 84 B/op 2 allocs/op | |
| BenchmarkClient/Channel-parallelism(64)-key(16)-value(64) | |
| BenchmarkClient/Channel-parallelism(64)-key(16)-value(64)-10 2199741 943.6 ns/op 260 B/op 4 allocs/op | |
| PASS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func pipelining(outgoing *bufio.Writer, incoming *bufio.Reader) chan request { | |
| requests := make(chan request, 512) | |
| waiting := make(chan request, 512) | |
| go func() { writing(outgoing, requests, waiting) }() | |
| go func() { reading(incoming, waiting) }() | |
| return requests | |
| } | |
| func makeRequest(requests chan request, data []byte) response { | |
| respCh := make(chan response, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func reading(incoming *bufio.Reader, waiting <-chan request) error { | |
| for { | |
| resp, err := readResponse(incoming) | |
| if err != nil { | |
| return err | |
| } | |
| if req := <-waiting; req.caller != nil { | |
| req.caller <- resp | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func writing(outgoing *bufio.Writer, requests <-chan request, waiting chan request) (err error) { | |
| var req request | |
| var more = true | |
| for more && err == nil { | |
| select { | |
| case req, more = <-requests: | |
| waiting <- req // <- push req into another waiting channel | |
| _, err = outgoing.Write(req.body) | |
| continue | |
| default: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func reading(incoming *bufio.Reader) error { | |
| for { | |
| resp, err := readResponse(incoming) | |
| if err != nil { | |
| return err | |
| } | |
| // find the original caller and send the response to it | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "bufio" | |
| func writing(buf *bufio.Writer, input <-chan []byte) (err error) { | |
| var data []byte | |
| var more = true | |
| for more && err == nil { | |
| select { | |
| case data, more = <-input: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://pkg.go.dev/os/signal | |
| func main() { | |
| c := make(chan os.Signal, 1) | |
| signal.Notify(c, os.Interrupt) | |
| s := <-c | |
| fmt.Println("Got signal:", s) | |
| } |