Fix concurrency

This commit is contained in:
ferreo 2024-07-29 23:17:11 +01:00
parent 7c93f8f5fa
commit d02c6ae6ce
2 changed files with 32 additions and 7 deletions

View File

@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/http"
) )
@ -20,10 +19,9 @@ const configsFolder = "pika-build-config"
func InitGit() error { func InitGit() error {
_, err := git.PlainClone(config.Configs.GitCache, false, &git.CloneOptions{ _, err := git.PlainClone(config.Configs.GitCache, false, &git.CloneOptions{
URL: config.Configs.Buildrepo, URL: config.Configs.Buildrepo,
SingleBranch: true, SingleBranch: true,
ReferenceName: plumbing.ReferenceName(config.Configs.Buildbranch), Depth: 1,
Depth: 1,
Auth: &http.BasicAuth{ Auth: &http.BasicAuth{
Password: config.Configs.GitToken, Password: config.Configs.GitToken,
Username: config.Configs.GitUser, Username: config.Configs.GitUser,
@ -52,8 +50,7 @@ func ResetAndPull() error {
Password: config.Configs.GitToken, Password: config.Configs.GitToken,
Username: config.Configs.GitUser, Username: config.Configs.GitUser,
}, },
RemoteName: "origin", RemoteName: "origin",
ReferenceName: plumbing.ReferenceName(config.Configs.Buildbranch),
}) })
return err return err
} }

View File

@ -3,6 +3,7 @@ package fastmap
import ( import (
"fmt" "fmt"
"strings" "strings"
"sync"
"slices" "slices"
@ -10,6 +11,7 @@ import (
) )
type Fastmap[K comparable, V any] struct { type Fastmap[K comparable, V any] struct {
mu sync.RWMutex
idx map[K]int idx map[K]int
store []fastmapValue[K, V] store []fastmapValue[K, V]
} }
@ -27,6 +29,8 @@ func New[K comparable, V any]() *Fastmap[K, V] {
} }
func (m *Fastmap[K, V]) Set(key K, value V) { func (m *Fastmap[K, V]) Set(key K, value V) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.idx[key]; ok { if _, ok := m.idx[key]; ok {
m.store[m.idx[key]].Value = value m.store[m.idx[key]].Value = value
return return
@ -36,6 +40,8 @@ func (m *Fastmap[K, V]) Set(key K, value V) {
} }
func (m *Fastmap[K, V]) Get(key K) (value V, ok bool) { func (m *Fastmap[K, V]) Get(key K) (value V, ok bool) {
m.mu.RLock()
defer m.mu.RUnlock()
idx, ok := m.idx[key] idx, ok := m.idx[key]
if !ok { if !ok {
return return
@ -44,6 +50,8 @@ func (m *Fastmap[K, V]) Get(key K) (value V, ok bool) {
} }
func (m *Fastmap[K, V]) Delete(key K) { func (m *Fastmap[K, V]) Delete(key K) {
m.mu.Lock()
defer m.mu.Unlock()
idx, ok := m.idx[key] idx, ok := m.idx[key]
if !ok { if !ok {
return return
@ -54,15 +62,21 @@ func (m *Fastmap[K, V]) Delete(key K) {
} }
func (m *Fastmap[K, V]) Has(key K) bool { func (m *Fastmap[K, V]) Has(key K) bool {
m.mu.RLock()
defer m.mu.RUnlock()
_, ok := m.idx[key] _, ok := m.idx[key]
return ok return ok
} }
func (m *Fastmap[K, V]) Len() int { func (m *Fastmap[K, V]) Len() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.idx) return len(m.idx)
} }
func (m *Fastmap[K, V]) GetPage(pageNum int, pageSize int) *Fastmap[K, V] { func (m *Fastmap[K, V]) GetPage(pageNum int, pageSize int) *Fastmap[K, V] {
m.mu.RLock()
defer m.mu.RUnlock()
start := pageSize * pageNum start := pageSize * pageNum
end := start + pageSize end := start + pageSize
if end > len(m.store) { if end > len(m.store) {
@ -77,11 +91,15 @@ func (m *Fastmap[K, V]) GetPage(pageNum int, pageSize int) *Fastmap[K, V] {
} }
func (m *Fastmap[K, V]) Clear() { func (m *Fastmap[K, V]) Clear() {
m.mu.Lock()
defer m.mu.Unlock()
m.idx = make(map[K]int) m.idx = make(map[K]int)
m.store = make([]fastmapValue[K, V], 0) m.store = make([]fastmapValue[K, V], 0)
} }
func (m *Fastmap[K, V]) Iter(fn func(key K, value V) bool) { func (m *Fastmap[K, V]) Iter(fn func(key K, value V) bool) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, v := range m.store { for _, v := range m.store {
if !fn(v.Key, v.Value) { if !fn(v.Key, v.Value) {
break break
@ -90,6 +108,8 @@ func (m *Fastmap[K, V]) Iter(fn func(key K, value V) bool) {
} }
func (m *Fastmap[K, V]) StableSortByKey() { func (m *Fastmap[K, V]) StableSortByKey() {
m.mu.Lock()
defer m.mu.Unlock()
slices.SortStableFunc(m.store, func(a, b fastmapValue[K, V]) int { slices.SortStableFunc(m.store, func(a, b fastmapValue[K, V]) int {
aKey := fmt.Sprint(a.Key) aKey := fmt.Sprint(a.Key)
bKey := fmt.Sprint(b.Key) bKey := fmt.Sprint(b.Key)
@ -103,6 +123,8 @@ func (m *Fastmap[K, V]) StableSortByKey() {
} }
func (m *Fastmap[K, V]) MarshalText() ([]byte, error) { func (m *Fastmap[K, V]) MarshalText() ([]byte, error) {
m.mu.RLock()
defer m.mu.RUnlock()
var builder strings.Builder var builder strings.Builder
for _, v := range m.store { for _, v := range m.store {
builder.WriteString(fmt.Sprintf("%v:%v\n", v.Key, v.Value)) builder.WriteString(fmt.Sprintf("%v:%v\n", v.Key, v.Value))
@ -111,6 +133,8 @@ func (m *Fastmap[K, V]) MarshalText() ([]byte, error) {
} }
func (m *Fastmap[K, V]) UnmarshalText(text []byte) error { func (m *Fastmap[K, V]) UnmarshalText(text []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
m.Clear() m.Clear()
lines := strings.Split(string(text), "\n") lines := strings.Split(string(text), "\n")
for _, line := range lines { for _, line := range lines {
@ -135,6 +159,8 @@ func (m *Fastmap[K, V]) UnmarshalText(text []byte) error {
} }
func (m *Fastmap[K, V]) MarshalJSON() ([]byte, error) { func (m *Fastmap[K, V]) MarshalJSON() ([]byte, error) {
m.mu.RLock()
defer m.mu.RUnlock()
temp := make(map[K]V) temp := make(map[K]V)
for _, v := range m.store { for _, v := range m.store {
temp[v.Key] = v.Value temp[v.Key] = v.Value
@ -143,6 +169,8 @@ func (m *Fastmap[K, V]) MarshalJSON() ([]byte, error) {
} }
func (m *Fastmap[K, V]) UnmarshalJSON(data []byte) error { func (m *Fastmap[K, V]) UnmarshalJSON(data []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
temp := make(map[K]V) temp := make(map[K]V)
if err := json.Unmarshal(data, &temp); err != nil { if err := json.Unmarshal(data, &temp); err != nil {
return err return err