Files
clio/tools/requests_gun/internal/ammo_provider/ammo_provider.go
Sergey Kuznetsov d11e7bc60e fix: Data race in new webserver (#1926)
There was a data race inside `CoroutineGroup` because internal timer was
used from multiple threads in the methods `asyncWait()` and
`onCoroutineComplete()`. Changing `registerForeign()` to spawn to the
same `yield_context` fixes the problem because now the timer is accessed
only from the same coroutine which has an internal strand.

During debugging I also added websocket support for `request_gun` tool.
2025-02-27 15:08:46 +00:00

38 lines
659 B
Go

package ammo_provider
import (
"bufio"
"io"
"strings"
"sync/atomic"
)
type AmmoProvider struct {
ammo []string
currentBullet atomic.Uint64
}
func (ap *AmmoProvider) getIndex() uint64 {
result := ap.currentBullet.Add(1)
return result % uint64(len(ap.ammo))
}
func (ap *AmmoProvider) GetBullet() string {
for {
res := ap.ammo[ap.getIndex()]
if !strings.HasPrefix(res, "#") {
return res
}
}
}
func New(reader io.Reader) *AmmoProvider {
scanner := bufio.NewScanner(reader)
var ammo []string
for scanner.Scan() {
ammo = append(ammo, scanner.Text())
}
return &AmmoProvider{ammo: ammo}
}