// this is common layout for all the services that require mutex protection and a guarantee that all running goroutines will be finished before stop finishes execution. This guarantee comes from waitGroup all one has to use CommonService.WaitGroup() in the goroutines that should finish by the end of stop function.
typeCommonServicestruct{
sync.RWMutex
cancelcontext.CancelFunc
ctxcontext.Context
wgsync.WaitGroup
startedbool
}
funcNewCommonService()*CommonService{
return&CommonService{
wg:sync.WaitGroup{},
RWMutex:sync.RWMutex{},
}
}
// mutex protected start function
// creates internal context over provided context and runs fn safely
// fn is excerpt to be executed to start the protocol