You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Easy to use: Get up and running with the library in minutes
Intuitive: Get access to a powerful pattern within software development, without encountering common pitfalls or mistakes!
Actively developed: Ideas and contributions welcomed!
Getting Started
concur is a Go library that allows you to safely and easily implement a concurrency pattern in your codebase. Install the latest version, and get up and running in minutes.
This library is currently used by the Five9-GO repository, to provide a reliable, safe way to interact with the Five9 API Websocket service.
What is concurrency, and why is it useful?
Concurrency is the composition of independently executing computations.
Concurrency is a way to structure software, particularly as a way to write clean code that interacts well with the real world.
It is not parallelism.
- Rob Pike, 2012
Simply put, concurrency in software allows you to create fast, robust systems that can be relied upon to be consistent. Some examples of what concurrency can do, as provided by the Go team:
Common pitfalls of concurrency that this library prevents
Race conditions
Deadlocks
Unpredictable or "flaky" tests when testing concurrent data models (such as Websockets, which are inherently asynchronous messages being sent back and forth between systems)
Install
go get github.com/equalsgibson/concur@v0.0.3
Example of ASyncReader
Run the example ASyncReader with a mock asynchronous resource, along with a CPU and Memory profile.
varcpuprofile=flag.String("cpuprofile", "", "write cpu profile to `file`")
varmemprofile=flag.String("memprofile", "", "write memory profile to `file`")
funcmain() {
flag.Parse()
if*cpuprofile!="" {
f, err:=os.Create(*cpuprofile)
iferr!=nil {
log.Fatal("could not create CPU profile: ", err)
}
deferf.Close() // error handling omitted for example
iferr:=pprof.StartCPUProfile(f); err!=nil {
log.Fatal("could not start CPU profile: ", err)
}
deferpprof.StopCPUProfile()
}
// Create a context that we can pass into the async loop function
ctx:=context.Background()
iterations:=uint(0)
// Create a new ASyncReader that will print the current iteration every 5 seconds.
// This could also fetch data from an API or database at specific intervals, or
// set up an asynchronous connection to a datasource (i.e. a Websocket)
reader:=concur.NewAsyncReader(
func(ctx context.Context) (uint, error) {
timer:=time.NewTimer(time.Second*1)
defertimer.Stop()
for {
select {
case<-timer.C:
ifiterations>=300 {
return0, errors.New("end of the example - thanks for using the concur package")
}
iterations++
returniterations, nil
case<-ctx.Done():
return0, ctx.Err()
}
}
},
)
// Create a goroutine for the Loop function to run in, so that the main program is not
// stopped from continuing execution while we wait for an asynchronous update.
goreader.Loop(ctx)
// Defer reader.Close, so that the reader.Loop() goroutine can return
deferreader.Close()
// Listen for updates on the reader.Updates() channel and check the context has not been cancelled.
for {
select {
caseupdate:=<-reader.Updates():
ifupdate.Err!=nil {
log.Printf("Got an error response from Loop fetch function: %s", update.Err.Error())
if*memprofile!="" {
f, err:=os.Create(*memprofile)
iferr!=nil {
log.Fatal("could not create memory profile: ", err)
}
deferf.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
iferr:=pprof.WriteHeapProfile(f); err!=nil {
log.Fatal("could not write memory profile: ", err)
}
}
return
}
log.Printf("Current Iteration: %d - Current goroutines: %d", iterations, runtime.NumGoroutine())
case<-ctx.Done():
log.Printf("Context was cancelled or hit deadline: %s", ctx.Err())
if*memprofile!="" {
f, err:=os.Create(*memprofile)
iferr!=nil {
log.Fatal("could not create memory profile: ", err)
}
deferf.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
iferr:=pprof.WriteHeapProfile(f); err!=nil {
log.Fatal("could not write memory profile: ", err)
}
}
return
}
}
}
Expected Output:
cgibson@wsl-ubuntuNexus:~/concur$ go run examples/async_reader_timer.go/main.go -cpuprofile cpu.prof -memprofile mem.prof
2024/10/29 00:01:00 Current Iteration: 1 - Current goroutines: 4
2024/10/29 00:01:01 Current Iteration: 2 - Current goroutines: 4
...
2024/10/29 00:01:03 Current Iteration: 299 - Current goroutines: 4
2024/10/29 00:01:03 Current Iteration: 300 - Current goroutines: 4
2024/10/29 00:01:05 Got an error response from Loop fetch function: end of the example - thanks for using the concur package
You can then analyze the profiles created, using the pprof tool
Contributing
Contributions are what make the open source community such an amazing place to learn, get inspired, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
Fork the Project
Create your Feature Branch (git checkout -b feature/AmazingFeature)
Commit your Changes (git commit -m 'Add some AmazingFeature')
Push to the Branch (git push origin feature/AmazingFeature)
Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt for more information.