mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-31 09:57:17 +02:00
databroker: add support for putting multiple records (#3291)
* databroker: add support for putting multiple records * add OptimumPutRequestsFromRecords function * replace GetAll with SyncLatest * fix stream when there are no records
This commit is contained in:
parent
343fa43ed4
commit
f73c5c615f
28 changed files with 790 additions and 660 deletions
|
@ -43,8 +43,12 @@ func Get(ctx context.Context, client DataBrokerServiceClient, object recordObjec
|
|||
}
|
||||
|
||||
// Put puts a record into the databroker.
|
||||
func Put(ctx context.Context, client DataBrokerServiceClient, object recordObject) (*PutResponse, error) {
|
||||
return client.Put(ctx, &PutRequest{Record: NewRecord(object)})
|
||||
func Put(ctx context.Context, client DataBrokerServiceClient, objects ...recordObject) (*PutResponse, error) {
|
||||
records := make([]*Record, len(objects))
|
||||
for i, object := range objects {
|
||||
records[i] = NewRecord(object)
|
||||
}
|
||||
return client.Put(ctx, &PutRequest{Records: records})
|
||||
}
|
||||
|
||||
// ApplyOffsetAndLimit applies the offset and limit to the list of records.
|
||||
|
@ -95,3 +99,45 @@ loop:
|
|||
|
||||
return records, recordVersion, serverVersion, nil
|
||||
}
|
||||
|
||||
// GetRecord gets the first record, or nil if there are none.
|
||||
func (x *PutRequest) GetRecord() *Record {
|
||||
records := x.GetRecords()
|
||||
if len(records) == 0 {
|
||||
return nil
|
||||
}
|
||||
return records[0]
|
||||
}
|
||||
|
||||
// GetRecord gets the first record, or nil if there are none.
|
||||
func (x *PutResponse) GetRecord() *Record {
|
||||
records := x.GetRecords()
|
||||
if len(records) == 0 {
|
||||
return nil
|
||||
}
|
||||
return records[0]
|
||||
}
|
||||
|
||||
// default is 4MB, but we'll do 1MB
|
||||
const maxMessageSize = 1024 * 1024 * 1
|
||||
|
||||
// OptimumPutRequestsFromRecords creates one or more PutRequests from a slice of records.
|
||||
// If the size of the request exceeds the max message size it will be split in half
|
||||
// recursively until the requests are less than or equal to the max message size.
|
||||
func OptimumPutRequestsFromRecords(records []*Record) []*PutRequest {
|
||||
if len(records) <= 1 {
|
||||
return []*PutRequest{{Records: records}}
|
||||
}
|
||||
|
||||
req := &PutRequest{
|
||||
Records: records,
|
||||
}
|
||||
if proto.Size(req) <= maxMessageSize {
|
||||
return []*PutRequest{req}
|
||||
}
|
||||
|
||||
return append(
|
||||
OptimumPutRequestsFromRecords(records[:len(records)/2]),
|
||||
OptimumPutRequestsFromRecords(records[len(records)/2:])...,
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue