Giter VIP home page Giter VIP logo

Comments (4)

halturin avatar halturin commented on May 19, 2024

If I do understand you correctly... You are trying to create new gen.Server instance to start it under the Supervisor with the separated object under the hood for every single process. As you noticed, using the same object for that makes it shared between all the actors (processes spawned with this object). But... every process has a separate state which can be used within the process callbacks for your needs. Here is example

type private struct {
   i int
}

type example struct {
	gen.Server
	shared int
}

func (e *example) Init(process *gen.ServerProcess, args ...etf.Term) error {
	// State has interface{} type
	process.State = &private{i: 123} // this value is only available for this process
	e.shared = 567 // all spawned processes with the same object &example{...} have access to this value
	return nil
}
func (e *example) HandleCast(process *gen.ServerProcess, message etf.Term) gen.ServerStatus {
	state := process.State.(*private)
	fmt.Printf("[%s] HandleCast: %#v\n", process.Name(), state)

	return gen.ServerStatusOK
}

so, the answer is - this design is intentional

you may also want to see how to create a custom behavior on top of the gen.Server https://github.com/ergo-services/ergo/tree/master/examples/gendemo

from ergo.

jiait avatar jiait commented on May 19, 2024

Thanks for the example! Ok, I also know that process.State can be private, but I personally prefer private objects, temporary processes that access object code is easy to write and as friendly as an Erlang Dictionary! In this example, shared sharing also needs to be lock or unlocked during operation, but I don't like unlocking because I like Mailbox

from ergo.

halturin avatar halturin commented on May 19, 2024

I was thinking about your idea to create a new object for the spawned process. Pro - this object could be used as an atomic state. Cons - making a clone of provided object could be pretty hard and expensive (nested pointers, maps, slices, etc.).

from ergo.

leonlee2013 avatar leonlee2013 commented on May 19, 2024

I tried to solve this problem by dynamically starting and stopping the child spec.#140

from ergo.

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.