-
Notifications
You must be signed in to change notification settings - Fork 7
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
647b702
commit b994784
Showing
4 changed files
with
120 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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 { | ||
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( | ||
query, | ||
0, // lastSeenID | ||
db.PollingOptions{Interval: 5 * time.Second, Notifier: notifier, NumRows: 100}, | ||
) | ||
listener, err := subscription.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
worker := &PublishWorker{ | ||
subscription: *subscription, | ||
listener: listener, | ||
registrant: reg, | ||
store: store, | ||
} | ||
go worker.start() | ||
|
||
return worker, nil | ||
} | ||
|
||
func (p *PublishWorker) start() { | ||
for new_batch := range p.listener { | ||
for _, stagedEnv := range new_batch { | ||
originatedEnv, sid, err := p.registrant.SignStagedEnvelope(stagedEnv) | ||
Check failure on line 67 in pkg/api/publishWorker.go GitHub Actions / Lint
Check failure on line 67 in pkg/api/publishWorker.go GitHub Actions / Lint
|
||
if err != nil { | ||
panic("TODO(rich)") | ||
} | ||
originatedBytes, err := proto.Marshal(originatedEnv) | ||
Check failure on line 71 in pkg/api/publishWorker.go GitHub Actions / Lint
Check failure on line 71 in pkg/api/publishWorker.go GitHub Actions / Lint
|
||
if err != nil { | ||
panic("TODO(rich)") | ||
} | ||
q := queries.New(p.store) | ||
Check failure on line 75 in pkg/api/publishWorker.go GitHub Actions / Lint
Check failure on line 75 in pkg/api/publishWorker.go GitHub Actions / Lint
|
||
// TODO(rich) Verify context | ||
// q.InsertGatewayEnvelope(context.Background(), queries.InsertGatewayEnvelopeParams{ | ||
// OriginatorSid: sid, | ||
// Topic: originatedBytes, | ||
// OriginatorEnvelope: proto.Marshal(originatedEnv), | ||
// }) | ||
|
||
// Start transaction | ||
// Sign envelope | ||
// Insert into all envelopes | ||
// Delete envelope from staged_originator_envelopes | ||
} | ||
} | ||
} |
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