Giter Site home page Giter Site logo

Robust Server about mwdns HOT 10 OPEN

vsupalov avatar vsupalov commented on July 21, 2024
Robust Server

from mwdns.

Comments (10)

vsupalov avatar vsupalov commented on July 21, 2024

Access to objects (such as games) from web(socket)handlers should be synchronized (think mutex) to prevent racing conditions.

from mwdns.

lucasb-eyer avatar lucasb-eyer commented on July 21, 2024

Only if the webserver itself is multithreaded, which is not necessarily the case if it's a select/poll/epoll/kqueue server! (TODO: figure this out)

from mwdns.

vsupalov avatar vsupalov commented on July 21, 2024

Good point, I was assuming it. There is concurrency in the handler execution, although there is said to be no parallelism (except when GOMAXPROCS is set to > 1). Testing code:

Golang:

package main

import (
   "fmt"
   "net/http"
   "time"
)

func serve(w http.ResponseWriter, r *http.Request) {
   fmt.Fprintln(w, "Hello there!")
   time.Sleep(5 * time.Second)
}

func main() {
   http.HandleFunc("/", serve)
   http.ListenAndServe(":8090", nil)
}

Bash:

 time for i in `seq 1 5`; do; curl localhost:8090&; done

All requests are finished at the same time and not 5 seconds after one another.

from mwdns.

lucasb-eyer avatar lucasb-eyer commented on July 21, 2024

So, should we just set GOMAXPROCS to 1 at the beginning of main? That might make our life much easier and it's not really the case that anything is computationally heavy and we'd need parallelism anyways, is it?

from mwdns.

vsupalov avatar vsupalov commented on July 21, 2024

it is usually set to 1 (by convention, not default) already, but this does not solve the concurrency issues. However, the worst effects of not-minding this that I currently see would be: joining an already full game (when 2 dudes try at the same time and the thread execution gods are benevolent), or scoring the same pair of cards in rush more by two different players. I have not looked hard though.

from mwdns.

lucasb-eyer avatar lucasb-eyer commented on July 21, 2024

Hmm? As I understood it, "concurrent but not parallel" will only let "other" code run when you call something which relinquishes control, such as Select or read/write a chan or Sleep. With this in mind, I don't see the scenarios you're talking about happening. Am I missing/misunderstanding something?

from mwdns.

vsupalov avatar vsupalov commented on July 21, 2024

I think my wording was off. By concurrent, I mean that the code may be executed interleaved. Assuming a handler does not just 'Sleep', but something like:

   fmt.Println("Hello there")
   time.Sleep((time.Duration)(rand.Int()%1000) * time.Nanosecond)
   fmt.Println("fine sir")
   time.Sleep((time.Duration)(rand.Int()%1000) * time.Nanosecond)
   fmt.Println("how are you")

then the output for two requests can be interleaving ("Hello...", "Hello...", "fine...", "fine...", "how...", "how..."). As the simplest case: if handler code contains some if condition, it can happen that two requests get through the check at once, instead of just one (scoring the same card by two players for example).

I think our concurrency issues can be solved easily, just by mutex-ing all requests that originate in handlers.

from mwdns.

lucasb-eyer avatar lucasb-eyer commented on July 21, 2024

Yes, I agree, but what I meant to say is that this "context switch" (quoted, it's not really one) will not happen arbitrarily, but only where "sleeping" functions are called, such as the ones I mentioned previously. Thus, if we don't have any call to such a function, no context will be switched.
Having said that, I agree that we can just add mutexes to be on the safe side.

from mwdns.

vsupalov avatar vsupalov commented on July 21, 2024

Oh, I understand now what you meant. Derp, sry. Are you sure that there are no arbitrary "context switches" (does logging count?), but only sensible ones?

from mwdns.

lucasb-eyer avatar lucasb-eyer commented on July 21, 2024

I'm confident, but I cannot be sure since I'm no go expert. At the end of the day, your "better be safe than sorry" way is... safer :)

from mwdns.

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.