Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
standard map -> x.RWMap for thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Apr 14, 2024
1 parent dbeae0d commit 216edb2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions acator/grpcenclave/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
pb "github.com/findy-network/findy-common-go/grpc/authn/v1"
"github.com/findy-network/findy-common-go/jwt"
"github.com/findy-network/findy-common-go/rpc"
"github.com/findy-network/findy-common-go/x"
"github.com/golang/glog"
"github.com/lainio/err2"
"github.com/lainio/err2/assert"
Expand All @@ -19,7 +20,8 @@ import (
)

func RegisterAuthnServer(s *grpc.Server) error {
authnServ := &authnServer{authnCmd: make(map[int64]*authn.Cmd, 128)}
authnCmd := x.NewRWMap[CmdMap]()
authnServ := &authnServer{authnCmd: authnCmd}
pb.RegisterAuthnServiceServer(s, authnServ)
glog.V(1).Infoln("GRPC registration for authnServer")
return nil
Expand All @@ -34,13 +36,15 @@ func Serve(port int) {
})
}

type CmdMap = map[int64]*authn.Cmd

type authnServer struct {
pb.UnimplementedAuthnServiceServer
atomic.Int64

root string

authnCmd map[int64]*authn.Cmd
authnCmd *x.RWMap[CmdMap, int64, *authn.Cmd]
}

func (a *authnServer) AuthFuncOverride(
Expand Down Expand Up @@ -77,7 +81,13 @@ func (a *authnServer) Enter(
cmdID := a.Add(1)
glog.V(3).Infof("=== authn Enter cmd:%v, cmdID: %v", cmd.Type, cmdID)

a.authnCmd[cmdID] = &authn.Cmd{
secEnc := &grpcenclave.Enclave{
Cmd: cmd,
CmdID: cmdID,
OutChan: make(chan *pb.CmdStatus),
InChan: make(chan *pb.SecretMsg),
}
a.authnCmd.Set(cmdID, &authn.Cmd{
SubCmd: strings.ToLower(cmd.GetType().String()),
UserName: cmd.GetUserName(),
PublicDIDSeed: cmd.GetPublicDIDSeed(),
Expand All @@ -86,14 +96,8 @@ func (a *authnServer) Enter(
Counter: cmd.GetCounter(),
Token: cmd.GetJWT(),
Origin: cmd.GetOrigin(),
}
secEnc := &grpcenclave.Enclave{
Cmd: cmd,
CmdID: cmdID,
OutChan: make(chan *pb.CmdStatus),
InChan: make(chan *pb.SecretMsg),
}
a.authnCmd[cmdID].SecEnclave = secEnc
SecEnclave: secEnc,
})

go func() {
defer err2.Catch(err2.Err(func(err error) {
Expand All @@ -109,7 +113,7 @@ func (a *authnServer) Enter(
}
close(secEnc.OutChan)
}))
r := try.To1(a.authnCmd[cmdID].Exec(nil))
r := try.To1(a.authnCmd.Get(cmdID).Exec(nil))
secEnc.OutChan <- &pb.CmdStatus{
CmdID: cmdID,
Type: pb.CmdStatus_READY_OK,
Expand All @@ -128,7 +132,7 @@ func (a *authnServer) Enter(
try.To(server.Send(status))
}
glog.V(3).Infoln("end Enter, delete cmdID:", cmdID, "...")
delete(a.authnCmd, cmdID)
a.authnCmd.Del(cmdID)
glog.V(3).Infoln("... ", cmdID, " deleted from map")
return nil
}
Expand All @@ -149,7 +153,7 @@ func (a *authnServer) EnterSecret(
})

glog.V(3).Infof("secret type: %v, ID: %v", smsg.GetType(), smsg.GetCmdID())
secEnc, ok := a.authnCmd[smsg.GetCmdID()].SecEnclave.(*grpcenclave.Enclave)
secEnc, ok := a.authnCmd.Get(smsg.GetCmdID()).SecEnclave.(*grpcenclave.Enclave)
assert.That(ok)
assert.NotNil(secEnc)
assert.CNotNil(secEnc.InChan)
Expand Down

0 comments on commit 216edb2

Please sign in to comment.