-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a100969
commit d27f00a
Showing
10 changed files
with
613 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"time" | ||
|
||
"github.com/xmtp/xmtpd/pkg/db" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/registrant" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
type PublishWorker struct { | ||
ctx context.Context | ||
listener <-chan []queries.StagedOriginatorEnvelope | ||
registrant *registrant.Registrant | ||
store *sql.DB | ||
subscription db.DBSubscription[queries.StagedOriginatorEnvelope] | ||
} | ||
|
||
func StartPublishWorker( | ||
ctx context.Context, | ||
reg *registrant.Registrant, | ||
store *sql.DB, | ||
notifier <-chan bool, | ||
) (*PublishWorker, error) { | ||
query := func(lastSeenID int64, numRows int32) ([]queries.StagedOriginatorEnvelope, int64, error) { | ||
results, err := queries.New(store).SelectStagedOriginatorEnvelopes( | ||
ctx, | ||
queries.SelectStagedOriginatorEnvelopesParams{ | ||
LastSeenID: lastSeenID, | ||
NumRows: numRows, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
if len(results) > 0 { | ||
lastSeenID = results[len(results)-1].ID | ||
} | ||
return results, lastSeenID, nil | ||
} | ||
subscription := db.NewDBSubscription( | ||
ctx, | ||
query, | ||
0, // lastSeenID | ||
db.PollingOptions{Interval: time.Second, Notifier: notifier, NumRows: 100}, | ||
) | ||
listener, err := subscription.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
worker := &PublishWorker{ | ||
ctx: ctx, | ||
subscription: *subscription, | ||
listener: listener, | ||
registrant: reg, | ||
store: store, | ||
} | ||
go worker.start() | ||
|
||
return worker, nil | ||
} | ||
|
||
func (p *PublishWorker) start() { | ||
for { | ||
select { | ||
case <-p.ctx.Done(): | ||
return | ||
case new_batch := <-p.listener: | ||
for _, stagedEnv := range new_batch { | ||
originatedEnv, err := p.registrant.SignStagedEnvelope(stagedEnv) | ||
if err != nil { | ||
panic("TODO(rich)") | ||
} | ||
originatedBytes, err := proto.Marshal(originatedEnv) | ||
if err != nil { | ||
panic("TODO(rich)") | ||
} | ||
|
||
q := queries.New(p.store) | ||
|
||
// On unique constraint conflicts, no error is thrown, but numRows is 0 | ||
_, err = q.InsertGatewayEnvelope( | ||
p.ctx, | ||
queries.InsertGatewayEnvelopeParams{ | ||
OriginatorID: int32(p.registrant.NodeID()), | ||
SequenceID: stagedEnv.ID, | ||
Topic: stagedEnv.Topic, | ||
OriginatorEnvelope: originatedBytes, | ||
}, | ||
) | ||
if err != nil { | ||
panic("TODO(rich)") | ||
} | ||
|
||
// Ensure the row is deleted, even if a unique constraint conflict occurred above | ||
_, err = q.DeleteStagedOriginatorEnvelope(context.Background(), stagedEnv.ID) | ||
if err != nil { | ||
panic("TODO(rich)") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.