-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Make the Mirror Queue a queue #17326
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
Changes from all commits
6154804
293c806
0518c4b
a30c432
07a304a
aee49c3
6109ee0
54d8bff
128036d
3fb2d73
6d20582
78b00fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"fmt" | ||
"sync" | ||
|
||
"code.gitea.io/gitea/modules/json" | ||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
|
@@ -29,7 +30,7 @@ type ChannelUniqueQueueConfiguration ChannelQueueConfiguration | |
type ChannelUniqueQueue struct { | ||
*WorkerPool | ||
lock sync.Mutex | ||
table map[Data]bool | ||
table map[string]bool | ||
shutdownCtx context.Context | ||
shutdownCtxCancel context.CancelFunc | ||
terminateCtx context.Context | ||
|
@@ -54,7 +55,7 @@ func NewChannelUniqueQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue | |
shutdownCtx, shutdownCtxCancel := context.WithCancel(terminateCtx) | ||
|
||
queue := &ChannelUniqueQueue{ | ||
table: map[Data]bool{}, | ||
table: map[string]bool{}, | ||
shutdownCtx: shutdownCtx, | ||
shutdownCtxCancel: shutdownCtxCancel, | ||
terminateCtx: terminateCtx, | ||
|
@@ -65,9 +66,13 @@ func NewChannelUniqueQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue | |
} | ||
queue.WorkerPool = NewWorkerPool(func(data ...Data) { | ||
for _, datum := range data { | ||
// No error is possible here because PushFunc ensures that this can be marshalled | ||
bs, _ := json.Marshal(datum) | ||
|
||
queue.lock.Lock() | ||
delete(queue.table, datum) | ||
delete(queue.table, string(bs)) | ||
queue.lock.Unlock() | ||
|
||
handle(datum) | ||
} | ||
}, config.WorkerPoolConfiguration) | ||
|
@@ -94,23 +99,28 @@ func (q *ChannelUniqueQueue) PushFunc(data Data, fn func() error) error { | |
if !assignableTo(data, q.exemplar) { | ||
return fmt.Errorf("Unable to assign data: %v to same type as exemplar: %v in queue: %s", data, q.exemplar, q.name) | ||
} | ||
|
||
bs, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
q.lock.Lock() | ||
locked := true | ||
defer func() { | ||
if locked { | ||
q.lock.Unlock() | ||
} | ||
}() | ||
if _, ok := q.table[data]; ok { | ||
if _, ok := q.table[string(bs)]; ok { | ||
return ErrAlreadyInQueue | ||
} | ||
// FIXME: We probably need to implement some sort of limit here | ||
// If the downstream queue blocks this table will grow without limit | ||
q.table[data] = true | ||
q.table[string(bs)] = true | ||
if fn != nil { | ||
err := fn() | ||
if err != nil { | ||
delete(q.table, data) | ||
delete(q.table, string(bs)) | ||
return err | ||
} | ||
} | ||
|
@@ -122,9 +132,14 @@ func (q *ChannelUniqueQueue) PushFunc(data Data, fn func() error) error { | |
|
||
// Has checks if the data is in the queue | ||
func (q *ChannelUniqueQueue) Has(data Data) (bool, error) { | ||
bs, err := json.Marshal(data) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can introduce a ps: just a little concern, should we limit the generated key's length? eg: if a key is too long, we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Json marshalability is tightly expected by the queues so it's not really replaceable. Unless you were thinking of something like allowing pushed Data to express some interface(s): type UniqueKeyed interface {
UniqueKey() string
}
type Conversion interface {
ToQueue() ([]byte, error)
FromQueue([]byte) error
}
type defaultConverter struct {
Data
}
func (d defaultConverter) ToQueue() ([]byte, error) {
return json.Marshal(d.Data)
}
func (d defaultConverter) FromQueue(bs []byte) error {
return json.Unmarshal(bs, d.Data)
}
func AsConversion(data Data) Conversion {
if conversion, ok := data.(Conversion); ok {
return conversion
}
return defaultConverter{Data: data}
}
// with defaultUniqueKeyed structs and AsUniqueKeyed() similarly. Then wired in as appropriate throughout the queues. As this isn't exposed to users I think the size isn't too much of an issue - developers should think carefully about the size of things that they push in to any queue though and we as maintainers should be aware that these things shouldn't be too large. In some ways as there is no current requirement for this - deciding to do this now we would be choosing an interface and API that would bind our hands but if you really think its needed then the above would be how I would do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I mean isn't that complex. I was thinking about: func generateDataTaskKey(data interface{}) (string, err) {
bs, err := json.Marshal(data)
if err != nil {
return err
}
return string(bs)
}
...
dataTaskKey, err := generateDataTaskKey(data)
...
delete(queue.table, dataTaskKey))
...
if _, ok := q.table[dataTaskKey]; ok {
...
}
...
q.table[dataTaskKey] = true
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with part a, the common function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the only unique queue implementation - if we were to change the keying from json marshalling here in the ChannelUniqueQueue we'd have to change it everywhere. This PR is simply a bugfix ensuring that the keying of the ChannelUniqueQueue is the same as the other queues . If we want to make it possible to change the keying algorithm then we need to do something like above and wire it in correctly otherwise someone will simply change one queue type and think it's fine. Whilst DRY is a good thing - if you make a helper you're saying this implementation can be changed easily when it absolutely cannot be. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still would like this helper function, no matter whether that mechanism is easy to replace or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I honestly do not see where this would be helpful. Please feel free to send me a PR with this helper function because I cannot see where it would be useful. |
||
q.lock.Lock() | ||
defer q.lock.Unlock() | ||
_, has := q.table[data] | ||
_, has := q.table[string(bs)] | ||
return has, nil | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.