Skip to content

Commit

Permalink
implement CloseSession
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Jan 14, 2024
1 parent 611d687 commit 93d74e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions internal/session/session.go
Expand Up @@ -33,6 +33,15 @@ func Update(qr *sqltypes.Result, session *psdbv1alpha1.Session) {
}
}

func Reset(session *psdbv1alpha1.Session) *psdbv1alpha1.Session {
id := UUID(session)
dbname := DBName(session)

session = New(dbname)
session.VitessSession.SessionUUID = id
return session
}

func New(dbname string) *psdbv1alpha1.Session {
// we're not doing anything with the signature, and it's opaque bytes
// to clients, so just generate a random 32 bytes
Expand Down
31 changes: 30 additions & 1 deletion main.go
Expand Up @@ -95,6 +95,17 @@ func getConn(ctx context.Context, uname, pass, dbname, session string) (*timedCo
return getConn(ctx, uname, pass, dbname, session)
}

func closeConn(uname, pass, dbname, session string) {
key := mysqlConnKey{uname, pass, dbname, session}

connMu.Lock()
if conn, ok := connPool[key]; ok {
conn.Close()
delete(connPool, key)
}
connMu.Unlock()
}

// dial connects to the underlying MySQL server, and switches to the underlying
// database automatically.
func dial(ctx context.Context, uname, pass, dbname string) (*mysql.Conn, error) {
Expand Down Expand Up @@ -355,7 +366,25 @@ func (server) CloseSession(
ctx context.Context,
req *connect.Request[psdbv1alpha1.CloseSessionRequest],
) (*connect.Response[psdbv1alpha1.CloseSessionResponse], error) {
return connect.NewResponse(&psdbv1alpha1.CloseSessionResponse{}), nil
ll := logger.With(
log.String("method", "CloseSession"),
log.String("content_type", req.Header().Get("Content-Type")),
)

creds, err := auth.ParseWithSecret(req.Header().Get("Authorization"))
if err != nil || creds.Type() != auth.BasicAuthType {
ll.Error("unauthenticated", log.Error(err))
return nil, connect.NewError(connect.CodeUnauthenticated, err)
}

sess := req.Msg.Session
if sess != nil {
closeConn(creds.Username(), string(creds.SecretBytes()), session.DBName(sess), session.UUID(sess))
}

return connect.NewResponse(&psdbv1alpha1.CloseSessionResponse{
Session: session.Reset(sess),
}), nil
}

func (server) Prepare(
Expand Down

0 comments on commit 93d74e2

Please sign in to comment.