Documentation ¶
Index ¶
Constants ¶
const (
EnvExecutionID = "RSMAP_EXECUTION_ID"
)
Variables ¶
This section is empty.
Functions ¶
func LockResources ¶ added in v0.0.4
func LockResources(ctx context.Context, resources ...*ResourceLocker) (func() error, error)
LockResources acquires exclusive/shared locks for multiple resources. Returned function releases all locks acquired.
Types ¶
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is the controller for external resource usage.
func New ¶
New creates an instance of Map that enables us to reuse external resources with thread safety. Most common use-case is Go's parallelized testing of multiple packages (`go test -p=N ./...`.)
Map has server mode and client mode. If Map has initialized in server mode, it creates database `logs.db` and write server address to `addr` under `${rsmapDir}/${executionID}/`. Other Map reads address of the server, and requests the server to acquire locks. So, every Go packages(directories) must specify same location as an argument. Otherwise, we cannot provide correct control. It's user's responsibility.
`executionID` is the identifier of the execution of `go test`. In default, we use the value of [os.Getppid()]. If you want to specify the id explicitly, set the value to `RSMAP_EXECUTION_ID` environment variable.
In almost cases, following code can be helpful.
p, _ := exec.Command("go", "mod", "GOMOD").Output() // Get file path of "go.mod". m, _ := rsmap.New(filepath.Join(filepath.Dir(strings.TrimSpace(string(p))), ".rsmap"))
func (*Map) Resource ¶
func (m *Map) Resource(ctx context.Context, name string, opts ...*ResourceOption) (*Resource, error)
Resource creates Resource object that provides control for resource usage.
Resource has a setting for max parallelism, you can specify the value by WithMaxParallelism(default value is 5.) And you want to perform an initialization of the resource, use WithInit.
type NewOption ¶
func WithHTTPClient ¶
WithHTTPClient specifies http.Client used for communication with server process. If your process launches server, this client may not be used.
func WithRetryPolicy ¶
func WithRetryPolicy(p backoff.Policy) *NewOption
WithRetryPolicy specifies a retry policy of each operations(resource initializations, lock acquisitions). For example, interval, exponential-backoff and max retry. For detailed settings, see backoff.NewExponentialPolicy or backoff.NewConstantPolicy.
type Resource ¶
type Resource struct {
// contains filtered or unexported fields
}
Resource brings an ability of acquire/release lock for the dedicated resource.
func (*Resource) Exclusive ¶ added in v0.0.4
func (r *Resource) Exclusive() *ResourceLocker
EXclusive returns ResourceLocker for Resource.
func (*Resource) Lock ¶
Lock acquires exclusive lock of the Resource. The instance of Resource can acquire only one lock. Consecutive acquisition without unlock doesn't fail, but do nothing.
To release lock, use [UnlockAny].
func (*Resource) RLock ¶
RLock acquires shared lock of the Resource. The instance of Resource can acquire only one lock. Consecutive acquisition without unlock doesn't fail, but do nothing.
To release lock, use [UnlockAny].
func (*Resource) Shared ¶ added in v0.0.4
func (r *Resource) Shared() *ResourceLocker
Shared returns ResourceLocker for Resource.
type ResourceLocker ¶ added in v0.0.4
type ResourceLocker struct {
// contains filtered or unexported fields
}
type ResourceOption ¶
ResourceOption represents option for Resource
func WithInit ¶
func WithInit(init InitFunc) *ResourceOption
WithInit specifies InitFunc for resource initialization.
InitFunc will be called only once globally, at first declaration by Resource. Other process waits until the completion of this initialization. So, if Resource is called without this option, we cannot perform initializations with concurrency safety.
func WithMaxParallelism ¶
func WithMaxParallelism(n int64) *ResourceOption
WithMaxParallelism specifies max parallelism of the resource usage.