Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve hub locking #1694

Merged
merged 28 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ea2bdbe
Upgrade go version and go mod tidy
MariemBaccari Oct 3, 2023
7442560
Add message ids thread safe structure
MariemBaccari Oct 3, 2023
66155dc
fix hub tests
MariemBaccari Oct 3, 2023
0e11844
Add peers struct
MariemBaccari Oct 4, 2023
3205c9e
fix queries data structure
MariemBaccari Oct 4, 2023
5c912bb
fix inbox tests
MariemBaccari Oct 10, 2023
f15fd57
debugging
MariemBaccari Oct 10, 2023
864ec8c
Add hub_state
MariemBaccari Oct 10, 2023
a45c2d9
remove additional config files
MariemBaccari Oct 10, 2023
25977d0
fix typo
MariemBaccari Oct 10, 2023
f853897
Add documentation and fix CI
MariemBaccari Oct 10, 2023
2c6af8e
fix hub log
MariemBaccari Oct 11, 2023
feecb98
Add thread safe map
MariemBaccari Oct 14, 2023
55bf030
Fix threadsafemap usage
MariemBaccari Oct 14, 2023
47a176c
Add usage of sets
MariemBaccari Oct 14, 2023
87e6a88
Address Pierluca's comments
MariemBaccari Oct 18, 2023
70eb68a
Add imports and foreach channels
MariemBaccari Oct 18, 2023
e0ae6a9
Change go version in workflows
MariemBaccari Oct 18, 2023
de9f7e4
change go version on karate workflow
MariemBaccari Oct 18, 2023
1314dbd
experimenting with CI and goimports
MariemBaccari Oct 18, 2023
962663f
experimenting with CI and goimports
MariemBaccari Oct 18, 2023
87934ac
try to add goimports
MariemBaccari Oct 18, 2023
05a8a8b
remove goimports
MariemBaccari Oct 18, 2023
54d4b75
fix documentation
MariemBaccari Oct 24, 2023
dd78bc2
cancel config changes
MariemBaccari Oct 24, 2023
2bc39db
adress pierluca's comments
MariemBaccari Oct 30, 2023
8b7482d
fix small typo
MariemBaccari Oct 30, 2023
94aba08
fix filenames
MariemBaccari Nov 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ jobs:
working-directory: ./be1-go

steps:
- name: Use go >= 1.19
- name: Use go >= 1.21
uses: actions/setup-go@v3
with:
go-version: ">=1.19"
go-version: ">=1.21"

- name: Setup repo
uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Use go >= 1.19
- name: Use go >= 1.21
uses: actions/setup-go@v3
with:
go-version: ">=1.19"
go-version: ">=1.21"

- name: build
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/karate_be1-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Use go >= 1.19
- name: Use go >= 1.21
uses: actions/setup-go@v3
with:
go-version: ">=1.19"
go-version: ">=1.21"

- name: Setup repo
uses: actions/checkout@v3
Expand Down
18 changes: 7 additions & 11 deletions be1-go/channel/lao/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"golang.org/x/exp/maps"
popstellar "popstellar"
"popstellar/channel"
"popstellar/channel/authentication"
Expand Down Expand Up @@ -703,16 +703,12 @@ func (c *Channel) createAndSendLAOGreet() error {
return xerrors.Errorf("failed to marshal the organizer key: %v", err)
}

peers := []messagedata.Peer{}
peersInfo := c.hub.GetPeersInfo()
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved

for _, info := range c.hub.GetPeersInfo() {
peer := messagedata.Peer{
Address: info.ClientAddress,
}
if slices.Contains(peers, peer) {
continue
}
peers = append(peers, peer)
peers := make(map[messagedata.Peer]struct{}, len(peersInfo))

for _, info := range peersInfo {
peers[messagedata.Peer{Address: info.ClientAddress}] = struct{}{}
}

msgData := messagedata.LaoGreet{
Expand All @@ -721,7 +717,7 @@ func (c *Channel) createAndSendLAOGreet() error {
LaoID: c.extractLaoID(),
Frontend: base64.URLEncoding.EncodeToString(orgPkBuf),
Address: c.hub.GetClientServerAddress(),
Peers: peers,
Peers: maps.Keys(peers),
}

// Marshalls the message data
Expand Down
2 changes: 1 addition & 1 deletion be1-go/configServer2.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"other-servers": [
"localhost:9001"
]
}
}
10 changes: 5 additions & 5 deletions be1-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/zitadel/oidc/v2 v2.1.2
go.dedis.ch/kyber/v3 v3.0.13
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/sync v0.1.0
golang.org/x/sync v0.4.0
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
gopkg.in/yaml.v2 v2.2.3
)
Expand All @@ -38,11 +38,11 @@ require (
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions be1-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand All @@ -105,15 +105,15 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos=
golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -123,14 +123,14 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
Expand Down
17 changes: 17 additions & 0 deletions be1-go/hub/standard_hub/hub_state/Channels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hub_state

import "popstellar/channel"

// Channels stores channel ids with their corresponding channels
type Channels struct {
ThreadSafeMap[string, channel.Channel]
}

// ForEach iterates over all channels and applies the given function
func (c *Channels) ForEach(f func(channel.Channel)) {
c.Lock()
defer c.Unlock()
for _, channel := range c.table {
f(channel)
}
}
32 changes: 32 additions & 0 deletions be1-go/hub/standard_hub/hub_state/MessageIds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hub_state

import (
"golang.org/x/exp/slices"
)

// MessageIds stores a channel id with its corresponding message ids
type MessageIds struct {
ThreadSafeMap[string, []string]
}

// Add adds a message id to the slice of message ids of the channel
func (i *MessageIds) Add(channel string, id string) {
i.Lock()
defer i.Unlock()
messageIds, channelStored := i.table[channel]
if !channelStored {
i.table[channel] = append(i.table[channel], id)
return
}
alreadyStoredId := slices.Contains(messageIds, id)
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
if !alreadyStoredId {
i.table[channel] = append(i.table[channel], id)
}
}

// AddAll adds a slice of message ids to the slice of message ids of the channel
func (i *MessageIds) AddAll(channel string, ids []string) {
i.Lock()
defer i.Unlock()
i.table[channel] = append(i.table[channel], ids...)
}
44 changes: 44 additions & 0 deletions be1-go/hub/standard_hub/hub_state/ThreadSafeMap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package hub_state

import "sync"

type ThreadSafeMap[K comparable, V any] struct {
sync.RWMutex
table map[K]V
}

func NewThreadSafeMap[K comparable, V any]() ThreadSafeMap[K, V] {
return ThreadSafeMap[K, V]{
table: make(map[K]V),
}
}

func (i *ThreadSafeMap[K, V]) Get(key K) (V, bool) {
i.RLock()
defer i.RUnlock()
val, ok := i.table[key]
return val, ok
}

func (i *ThreadSafeMap[K, V]) Set(key K, val V) {
i.Lock()
defer i.Unlock()
i.table[key] = val
}

// GetTable returns a shallow copy of the map
func (i *ThreadSafeMap[K, V]) GetTable() map[K]V {
i.Lock()
defer i.Unlock()
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
tableCopy := make(map[K]V, len(i.table))
for key, val := range i.table {
tableCopy[key] = val
}
return tableCopy
}

func (i *ThreadSafeMap[K, V]) IsEmpty() bool {
i.Lock()
defer i.Unlock()
MariemBaccari marked this conversation as resolved.
Show resolved Hide resolved
return len(i.table) == 0
}
44 changes: 0 additions & 44 deletions be1-go/hub/standard_hub/hub_state/channels_by_id.go

This file was deleted.

59 changes: 0 additions & 59 deletions be1-go/hub/standard_hub/hub_state/ids_by_channel.go

This file was deleted.

Loading
Loading