testing locks
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 27s
Build and Publish / BuildAndDeploy (push) Successful in 2m21s

This commit is contained in:
matst80
2024-11-13 09:52:47 +01:00
parent e1470c02b6
commit d481bcdc77
2 changed files with 20 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"strconv" "strconv"
@@ -22,6 +23,7 @@ func NewPoolServer(pool GrainPool, pod_name string) *PoolServer {
func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request) error { func (s *PoolServer) HandleGet(w http.ResponseWriter, r *http.Request) error {
id := r.PathValue("id") id := r.PathValue("id")
log.Printf("Getting cart %s\n", id)
data, err := s.pool.Get(ToCartId(id)) data, err := s.pool.Get(ToCartId(id))
if err != nil { if err != nil {
return err return err

View File

@@ -319,11 +319,23 @@ func (p *SyncedPool) Negotiate() {
} }
func (p *SyncedPool) GetHealthyRemotes() []*RemoteHost {
p.mu.RLock()
defer p.mu.RUnlock()
remotes := make([]*RemoteHost, 0, len(p.remotes))
for _, r := range p.remotes {
if r.IsHealthy() {
remotes = append(remotes, r)
}
}
return remotes
}
func (p *SyncedPool) RequestOwnership(id CartId) error { func (p *SyncedPool) RequestOwnership(id CartId) error {
ok := 0 ok := 0
all := 0 all := 0
p.mu.RLock()
for _, r := range p.remotes { for _, r := range p.GetHealthyRemotes() {
if !r.IsHealthy() { if !r.IsHealthy() {
continue continue
} }
@@ -342,7 +354,7 @@ func (p *SyncedPool) RequestOwnership(id CartId) error {
} }
ok++ ok++
} }
p.mu.RUnlock()
if ok == 0 && all > 0 { if ok == 0 && all > 0 {
p.removeLocalGrain(id) p.removeLocalGrain(id)
return fmt.Errorf("no remotes confirmed change") return fmt.Errorf("no remotes confirmed change")
@@ -361,8 +373,7 @@ func (p *SyncedPool) removeLocalGrain(id CartId) {
} }
func (p *SyncedPool) AddRemote(host string) error { func (p *SyncedPool) AddRemote(host string) error {
p.mu.Lock()
defer p.mu.Unlock()
_, hasHost := p.remotes[host] _, hasHost := p.remotes[host]
if host == "" || p.IsKnown(host) || hasHost { if host == "" || p.IsKnown(host) || hasHost {
return nil return nil
@@ -378,7 +389,9 @@ func (p *SyncedPool) AddRemote(host string) error {
MissedPings: 0, MissedPings: 0,
Host: host, Host: host,
} }
p.mu.Lock()
p.remotes[host] = &remote p.remotes[host] = &remote
p.mu.Unlock()
go func() { go func() {
<-remote.PersistentConnection.Died <-remote.PersistentConnection.Died
log.Printf("Removing host, remote died %s", host) log.Printf("Removing host, remote died %s", host)