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

How do you make sure the concurrent issue? #25

Closed
dawei101 opened this issue Oct 4, 2021 · 2 comments
Closed

How do you make sure the concurrent issue? #25

dawei101 opened this issue Oct 4, 2021 · 2 comments

Comments

@dawei101
Copy link

dawei101 commented Oct 4, 2021

delete(subCategoryClients, disconnected.ClientUUID)

I see there is no lock when remove and add item from map, this will case crash.

@jcuga
Copy link
Owner

jcuga commented Oct 4, 2021

Hello,

I believe the map usage is always run in the same goroutine, which uses channels to receive actions from the http goroutintes. This is the subscription manager's run() function running in it's own goroutine.

See:

go subManager.run()

	go subManager.run()

Where the run function has:

func (sm *subscriptionManager) run() error {

		select {
		case newClient := <-sm.clientSubscriptions:
			sm.handleNewClient(newClient)
			sm.seeIfTimeToPurgeStaleCategories()
		case disconnected := <-sm.ClientTimeouts:
			sm.handleClientDisconnect(disconnected)
			sm.seeIfTimeToPurgeStaleCategories()
		case event := <-sm.Events:
			// Optional hook on publish
			if sm.AddOn != nil {
				sm.AddOn.OnPublish(event)
			}
			sm.handleNewEvent(event)
			sm.seeIfTimeToPurgeStaleCategories()

Then, when other goroutines handling HTTP need to tell the subscription manager about a new client, a new event, or a disconnect, they send data to the manager via channels which are concurrent-safe.

for example:
sending new subscription over channel to run():

subscriptionRequests <- subscription

client disconnect:

clientTimeouts <- &subscription.clientCategoryPair

event publish:

m.eventsIn <- &Event{timeToEpochMilliseconds(time.Now()), category, data, u}

Conversely, the manager's goroutine will send data back to clients via channels:

newClient.Events <- events

If you see any code that is accessing maps across multiple goroutines, or if you have encountered any errors, please point them out and I will try to fix them. But I think all map access is limited to the manager's run() goroutine and the functions called by run() which are still within the same goroutine. All cross-go-routine sharing of data is done via channels.

@dawei101
Copy link
Author

Thanks for so rich reply, get it 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants