concept resource in category .net

This is an excerpt from Manning's book Concurrency in .NET.
To optimize the performance of a concurrent application with intense memory consumption, recycle the objects that otherwise are subject to be garbage collected by the system. In the parallel compress and encrypt stream example, you want to reuse the same byte buffers (byte arrays) generated instead of creating new ones. This is possible using
ObjectPool, a class designed to provide a cached pool of objects that recycles the items that aren’t being used. This reuse of objects avoids expensive acquisition and release of resources, minimizing the potential memory allocation. Specifically, in the highly concurrent example, you need a thread-safe and non-blocking (task-based) concurrent object pool (figure 13.2).
In general,
ReaderWriterAgentis a better fit for programs that concurrently access resources asynchronously using I/O operations.Figure 13.7
ReaderWriterAgentacts as a gate agent to asynchronously synchronize the access to share resources. In the top image, only one write operation at a time is executed, while the read operations are queued up to wait asynchronously for the write operation to complete before proceeding. In the bottom image, multiple read operations are processed asynchronously in parallel according to the degree of parallelism configured.![]()
Listing 14.1 Web API trading controller
[<RoutePrefix("api/trading")>] type TradingController() = inherit ApiController() let subject = new Subject<CommandWrapper>() #1 let publish connectionId cmd = #7 match cmd with | Result.Ok(cmd) -> #2 CommandWrapper.Create connectionId cmd #3 subject.OnNext #1 | Result.Error(e) -> subject.OnError(exn (e)) #2 cmd let toResponse (request : HttpRequestMessage) result = match result with | Ok(_) -> request.CreateResponse(HttpStatusCode.OK) | _ -> request.CreateResponse(HttpStatusCode.BadRequest) #4 [<Route("sell"); HttpPost>] member this.PostSell([<FromBody>] tr : TradingRequest) = async { let connectionId = tr.ConnectionID #5 return { Symbol = tr.Symbol.ToUpper() Quantity = tr.Quantity Price = tr.Price Trading = TradingType.Sell } |> tradingdValidation #6 |> publish connectionId #7 |> toResponse this.Request #4 } |> Async.StartAsTask #8 interface IObservable<CommandWrapper> with member this.Subscribe observer = subject.Subscribe observer #1 override this.Dispose disposing = if disposing then subject.Dispose() base.Dispose disposing #9 #1 The controller uses a Subject instance to behave as an observable to publish commands to the observer registered. #2 Validates the command using the Result type #3 Creates a wrapper around a given command to enrich the type with metadata #4 Uses a helper function for the controller actions to deliver an http response #5 Shows the current connection ID from SignalR context #6 Validates using a function composition #7 Publishes the command using Rx #8 Starts the actions as a Task to make the action run asynchronously #9 Disposes the Subject; important to free up the resources !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":4,\"ch\":19},{\"line\":4,\"ch\":26}],[{\"line\":43,\"ch\":26},{\"line\":43,\"ch\":33}],[{\"line\":51,\"ch\":17},{\"line\":51,\"ch\":24}],[{\"line\":10,\"ch\":1},{\"line\":10,\"ch\":15}],[{\"line\":6,\"ch\":5},{\"line\":6,\"ch\":12}],[{\"line\":30,\"ch\":4},{\"line\":30,\"ch\":11}],[{\"line\":43,\"ch\":73},{\"line\":43,\"ch\":80}],[{\"line\":34,\"ch\":6},{\"line\":34,\"ch\":23}],[{\"line\":36,\"ch\":14},{\"line\":36,\"ch\":25}],[{\"line\":37,\"ch\":13},{\"line\":37,\"ch\":22}]]"} !@%STYLE%@!