The act.WebWorker actor implements the low-level gen.ProcessBehavior interface and allows HTTP requests to be handled as asynchronous messages. It is designed to be used with a web server (refer to the Web section in meta-processes). To launch a process based on act.WebWorker, you need to embed it in your object and implement the required methods from the act.WebWorkerBehavior interface.
To work with your object, act.WebWorker uses the act.WebWorkerBehavior interface. This interface defines a set of callback methods:
typeWebWorkerBehaviorinterface {gen.ProcessBehavior// Init invoked on a spawn WebWorker for the initializing.Init(args ...any) error// HandleMessage invoked if WebWorker received a message sent with gen.Process.Send(...).// Non-nil value of the returning error will cause termination of this process.// To stop this process normally, return gen.TerminateReasonNormal// or any other for abnormal termination.HandleMessage(from gen.PID, message any) error// HandleCall invoked if WebWorker got a synchronous request made with gen.Process.Call(...).// Return nil as a result to handle this request asynchronously and// to provide the result later using the gen.Process.SendResponse(...) method.HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)// Terminate invoked on a termination processTerminate(reason error)// HandleEvent invoked on an event message if this process got subscribed on// this event using gen.Process.LinkEvent or gen.Process.MonitorEventHandleEvent(message gen.MessageEvent) error// HandleInspect invoked on the request made with gen.Process.Inspect(...)HandleInspect(from gen.PID, item ...string) map[string]string// HandleGet invoked on a GET requestHandleGet(from gen.PID, writer http.ResponseWriter, request *http.Request) error// HandlePOST invoked on a POST requestHandlePost(from gen.PID, writer http.ResponseWriter, request *http.Request) error// HandlePut invoked on a PUT requestHandlePut(from gen.PID, writer http.ResponseWriter, request *http.Request) error// HandlePatch invoked on a PATCH requestHandlePatch(from gen.PID, writer http.ResponseWriter, request *http.Request) error// HandleDelete invoked on a DELETE requestHandleDelete(from gen.PID, writer http.ResponseWriter, request *http.Request) error// HandleHead invoked on a HEAD requestHandleHead(from gen.PID, writer http.ResponseWriter, request *http.Request) error// HandleOptions invoked on an OPTIONS requestHandleOptions(from gen.PID, writer http.ResponseWriter, request *http.Request) error}
All methods in the act.WebWorkerBehavior interface are optional for implementation.
It is most efficient to use act.WebWorker in combination with act.Pool for load balancing when handling HTTP requests:
//// WebWorker//funcfactory_MyWebWorker() gen.ProcessBehavior {return&MyWebWorker{}}typeMyWebWorkerstruct {act.WebWorker}// Handle GET requests. func (w *MyWebWorker) HandleGet(from gen.PID, writer http.ResponseWriter, request *http.Request) error { w.Log().Info("got HTTP request %q", request.URL.Path) w.WriteHeader(http.StatusOK)returnnil}//// Pool of workers//typeMyPoolstruct {act.Pool}funcfactory_MyPool() gen.ProcessBehavior {return&MyPool{}}// Init invoked on a spawn Pool for the initializing.func (p *MyPool) Init(args ...any) (act.PoolOptions, error) { opts :=act.PoolOptions{ WorkerFactory: factory_MyWebWorker, } p.Log().Info("started process pool of MyWebWorker with %d workers", opts.PoolSize)return opts, nil}
An example implementation of a web server using act.WebWorker and act.Pool for load distribution when handling HTTP requests can be found in the repository at https://github.com/ergo-services/examples, the project demo.