-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #807 from traPtitech/impr/gitea-integration
Resync gitea on repository creation / speedup
- Loading branch information
Showing
17 changed files
with
597 additions
and
220 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
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
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.
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
75 changes: 75 additions & 0 deletions
75
pkg/infrastructure/grpc/controller_gitea_integration_service.go
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,75 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/samber/lo" | ||
log "github.com/sirupsen/logrus" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
|
||
"github.com/traPtitech/neoshowcase/pkg/domain" | ||
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb" | ||
) | ||
|
||
type giteaIntegrationConnection struct { | ||
reqSender chan<- *pb.GiteaIntegrationRequest | ||
} | ||
|
||
type ControllerGiteaIntegrationService struct { | ||
connections []*giteaIntegrationConnection | ||
lock sync.Mutex | ||
} | ||
|
||
func NewControllerGiteaIntegrationService() domain.ControllerGiteaIntegrationService { | ||
return &ControllerGiteaIntegrationService{} | ||
} | ||
|
||
func (s *ControllerGiteaIntegrationService) Connect(ctx context.Context, _ *connect.Request[emptypb.Empty], st *connect.ServerStream[pb.GiteaIntegrationRequest]) error { | ||
id := domain.NewID() | ||
log.WithField("id", id).Info("new gitea integration connection") | ||
defer log.WithField("id", id).Info("gitea integration connection closed") | ||
|
||
reqSender := make(chan *pb.GiteaIntegrationRequest) | ||
conn := &giteaIntegrationConnection{reqSender: reqSender} | ||
s.lock.Lock() | ||
s.connections = append(s.connections, conn) | ||
s.lock.Unlock() | ||
|
||
defer func() { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
s.connections = lo.Without(s.connections, conn) | ||
}() | ||
|
||
loop: | ||
for { | ||
select { | ||
case req, ok := <-reqSender: | ||
if !ok { | ||
break loop | ||
} | ||
err := st.Send(req) | ||
if err != nil { | ||
return err | ||
} | ||
case <-ctx.Done(): | ||
break loop | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *ControllerGiteaIntegrationService) Broadcast(req *pb.GiteaIntegrationRequest) { | ||
s.lock.Lock() | ||
defer s.lock.Unlock() | ||
|
||
for _, ssgen := range s.connections { | ||
select { | ||
case ssgen.reqSender <- req: | ||
default: | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
pkg/infrastructure/grpc/controller_gitea_integration_service_client.go
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,42 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
|
||
"github.com/traPtitech/neoshowcase/pkg/domain" | ||
"github.com/traPtitech/neoshowcase/pkg/domain/web" | ||
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb" | ||
"github.com/traPtitech/neoshowcase/pkg/infrastructure/grpc/pb/pbconnect" | ||
) | ||
|
||
type ControllerGiteaIntegrationServiceClient struct { | ||
client pbconnect.ControllerGiteaIntegrationServiceClient | ||
} | ||
|
||
func NewControllerGiteaIntegrationServiceClient( | ||
c ControllerServiceClientConfig, | ||
) domain.ControllerGiteaIntegrationServiceClient { | ||
return &ControllerGiteaIntegrationServiceClient{ | ||
client: pbconnect.NewControllerGiteaIntegrationServiceClient(web.NewH2CClient(), c.URL), | ||
} | ||
} | ||
|
||
func (c *ControllerGiteaIntegrationServiceClient) Connect(ctx context.Context, onRequest func(req *pb.GiteaIntegrationRequest)) error { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
st, err := c.client.Connect(ctx, connect.NewRequest(&emptypb.Empty{})) | ||
if err != nil { | ||
return err | ||
} | ||
defer st.Close() | ||
|
||
for st.Receive() { | ||
msg := st.Msg() | ||
onRequest(msg) | ||
} | ||
return st.Err() | ||
} |
Oops, something went wrong.