Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: x/sync/semaphore: expose the size #53940

Open
dsnet opened this issue Jul 18, 2022 · 0 comments
Open

proposal: x/sync/semaphore: expose the size #53940

dsnet opened this issue Jul 18, 2022 · 0 comments

Comments

@dsnet
Copy link
Member

dsnet commented Jul 18, 2022

In my usages of sempahore, I'm doing something like:

const maxWeight = ...
sema := semaphore.NewWeighted(maxWeight)
...
for ... {
    size := object.Size()
    if size > maxWeight {
        // Object is too large to fit in memory, switch to a streaming mode.
        object.StreamingMode(true)
        size = maxWeight // use the maximum weight to have exclusive lock over semaphore
    }
    sema.Acquire(size)
    go func() {
        defer sema.Release(size)
        ...
    }
}

You will notice that I'm passing around both sema and maxWeight. However, this is a somewhat unnecessary since the unexported sema.size field is exactly the information I need.

I propose adding a Capacity method:

// Capacity returns the maximum combined weight this semaphore is constructed with.
func (s *Weighted) Capacity() int64 {
    return s.size
}

Alternative names: Weight, MaxWeight, MaxSize. We should choose a name that would not prevent the possible future addition of a something like Length where we return the value of s.cur. The names of Length of Capacity would match the names of len(c) and cap(c) when a channel is used a semaphore.

\cc @jba

@dsnet dsnet added the Proposal label Jul 18, 2022
@gopherbot gopherbot added this to the Proposal milestone Jul 18, 2022
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

2 participants