indigophox commented on code in PR #40284:
URL: https://github.com/apache/arrow/pull/40284#discussion_r1509608618


##########
go/arrow/flight/session/session.go:
##########
@@ -0,0 +1,233 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package session provides server middleware and reference implementations 
for Flight session management.
+//
+// The existing middleware implementation uses cookies, so any client would 
need middleware/support for storing and sending those cookies.
+// Both stateful and stateless session cookie implementations are provided.
+// The default stateful implementation persists sessions in-memory, but a 
custom SessionStore may be provided for more durable persistence.
+// Stateless session cookies may be used with no additional infrastructure, 
but they are not encrypted and should not contain sensitive information.
+package session
+
+import (
+       "context"
+       "errors"
+       "fmt"
+       "net/http"
+       "sync"
+
+       "github.com/apache/arrow/go/v16/arrow/flight"
+       "google.golang.org/grpc"
+       "google.golang.org/grpc/metadata"
+       "google.golang.org/protobuf/proto"
+)
+
+var ErrNoSession error = errors.New("flight: server session not present")
+
+type sessionMiddlewareKey struct{}
+
+// Return a copy of the provided context containing the provided ServerSession
+func NewSessionContext(ctx context.Context, session ServerSession) 
context.Context {
+       return context.WithValue(ctx, sessionMiddlewareKey{}, session)
+}
+
+// Retrieve the ServerSession from the provided context if it exists.
+// An error indicates that the session was not found in the context.
+func GetSessionFromContext(ctx context.Context) (ServerSession, error) {
+       session, ok := ctx.Value(sessionMiddlewareKey{}).(ServerSession)
+       if !ok {
+               return nil, ErrNoSession
+       }
+       return session, nil
+}
+
+// Container for named SessionOptionValues
+type ServerSession interface {
+       // An identifier for the session that the server can use to reconstruct
+       // the session state on future requests. It is the responsibility of
+       // each implementation to define the token's semantics.
+       Token() string
+       // Get session option value by name, or nil if it does not exist
+       GetSessionOption(name string) *flight.SessionOptionValue
+       // Get a copy of the session options
+       GetSessionOptions() map[string]*flight.SessionOptionValue
+       // Set session option by name to given value
+       SetSessionOption(name string, value *flight.SessionOptionValue)
+       // Idempotently remove name from this session
+       EraseSessionOption(name string)
+       // Close the session
+       Close() error
+       // Report whether the session has been closed
+       Closed() bool
+}
+
+// Handles session lifecycle management
+type ServerSessionManager interface {
+       // Create a new, empty ServerSession
+       CreateSession(ctx context.Context) (ServerSession, error)
+       // Get the current ServerSession, if one exists
+       GetSession(ctx context.Context) (ServerSession, error)
+       // Cleanup any resources associated with the current ServerSession
+       CloseSession(session ServerSession) error
+}
+
+// Implementation of common session behavior. Intended to be extended
+// by specific session implementations.
+type serverSession struct {
+       closed bool
+
+       options map[string]*flight.SessionOptionValue
+       mu      sync.RWMutex
+}
+
+func (session *serverSession) GetSessionOption(name string) 
*flight.SessionOptionValue {
+       session.mu.RLock()
+       defer session.mu.RUnlock()
+       value, found := session.options[name]
+       if !found {
+               return nil
+       }
+
+       return value
+}
+
+func (session *serverSession) GetSessionOptions() 
map[string]*flight.SessionOptionValue {
+       options := make(map[string]*flight.SessionOptionValue, 
len(session.options))
+
+       session.mu.RLock()
+       defer session.mu.RUnlock()
+       for k, v := range session.options {
+               options[k] = proto.Clone(v).(*flight.SessionOptionValue)
+       }
+
+       return options
+}
+
+func (session *serverSession) SetSessionOption(name string, value 
*flight.SessionOptionValue) {
+       if value.GetOptionValue() == nil {
+               session.EraseSessionOption(name)
+               return
+       }
+
+       session.mu.Lock()
+       defer session.mu.Unlock()
+       session.options[name] = value
+}
+
+func (session *serverSession) EraseSessionOption(name string) {
+       session.mu.Lock()
+       defer session.mu.Unlock()
+       delete(session.options, name)
+}
+
+func (session *serverSession) Close() error {
+       session.options = nil
+       session.closed = true
+       return nil
+}
+
+func (session *serverSession) Closed() bool {
+       return session.closed
+}
+
+// Create new instance of CustomServerMiddleware implementing server session 
persistence.
+//
+// The provided manager can be used to customize session 
implementation/behavior.
+// If no manager is provided, a stateful in-memory, goroutine-safe 
implementation is used.
+func NewServerSessionMiddleware(manager ServerSessionManager) 
*serverSessionMiddleware {
+       // Default manager

Review Comment:
   The shipped middleware classes for C++ and Java both provide their own 
option value store, presently without the option to inject a separate k:v store 
implementation—although that could be accomplished via a subclass of the 
provided middleware factory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to