GithubHelp home page GithubHelp logo

Comments (2)

jcuga avatar jcuga commented on September 25, 2024

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.

from golongpoll.

dawei101 avatar dawei101 commented on September 25, 2024

Thanks for so rich reply, get it 💯

from golongpoll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.