Skip to content

Commit

Permalink
server: remove unused context hook
Browse files Browse the repository at this point in the history
I can no longer remember what motivated me to add this in the first place, but
it is not used anywhere and was confusingly named, so it's gone now.
  • Loading branch information
creachadair committed Mar 14, 2024
1 parent 8400c81 commit 26319f3
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 36 deletions.
6 changes: 0 additions & 6 deletions server/tailsql/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ type Options struct {
// by the rule replaces the original string.
UIRewriteRules []UIRewriteRule `json:"-"`

// If non-nil, this function is called to annotate ctx before passing it in
// to a database query for the given source. If the callback is nil, or if
// it returns nil, ctx is used unmodified. Otherwise the returned value
// replaces ctx in the query.
QueryContext func(ctx context.Context, src, query string) context.Context `json:"-"`

// If non-nil, send logs to this logger. If nil, use log.Printf.
Logf logger.Logf `json:"-"`
}
Expand Down
14 changes: 1 addition & 13 deletions server/tailsql/tailsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ type Server struct {
rules []UIRewriteRule
authorize func(string, *apitype.WhoIsResponse) error
qtimeout time.Duration
qcontext func(ctx context.Context, src, query string) context.Context
logf logger.Logf

mu sync.Mutex
Expand Down Expand Up @@ -167,7 +166,6 @@ func NewServer(opts Options) (*Server, error) {
rules: opts.UIRewriteRules,
authorize: opts.authorize(),
qtimeout: opts.QueryTimeout.Duration(),
qcontext: opts.QueryContext,
logf: opts.logf(),
dbs: dbs,
}, nil
Expand Down Expand Up @@ -434,7 +432,7 @@ func (s *Server) queryContext(ctx context.Context, caller, src, query string) (*
defer cancel()
}

return runQueryInTx(s.getQueryContext(ctx, src, query), h,
return runQueryInTx(ctx, h,
func(fctx context.Context, tx *sql.Tx) (_ *dbResult, err error) {
start := time.Now()
var out dbResult
Expand Down Expand Up @@ -612,13 +610,3 @@ func (s *Server) getHandles() []*dbHandle {
// data are only ever appended to the end.
return s.dbs
}

// getQueryContext decorates ctx if necessary using the context hook for src and query.
func (s *Server) getQueryContext(ctx context.Context, src, query string) context.Context {
if s.qcontext != nil {
if qctx := s.qcontext(ctx, src, query); qctx != nil {
return qctx
}
}
return ctx
}
18 changes: 1 addition & 17 deletions server/tailsql/tailsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/tailscale/setec/client/setec"
"github.com/tailscale/setec/setectest"
"github.com/tailscale/tailsql/authorizer"
Expand Down Expand Up @@ -187,20 +186,14 @@ func TestServer(t *testing.T) {
},
},
}
var contextHookData [2]string
s, err := tailsql.NewServer(tailsql.Options{
LocalClient: fc,
UILinks: []tailsql.UILink{
{Anchor: testAnchor, URL: testURL},
},
UIRewriteRules: testUIRules,
Authorize: authorizer.ACLGrants(nil),
QueryContext: func(ctx context.Context, src, query string) context.Context {
contextHookData[0] = src
contextHookData[1] = query
return ctx
},
Logf: t.Logf,
Logf: t.Logf,
})
if err != nil {
t.Fatalf("NewServer: unexpected error: %v", err)
Expand All @@ -217,15 +210,6 @@ func TestServer(t *testing.T) {
defer htest.Close()
cli := htest.Client()

t.Run("ContextHook", func(t *testing.T) {
q := url.Values{"q": {"select count(*) from users"}}
url := htest.URL + "?" + q.Encode()
mustGet(t, cli, url)
if diff := cmp.Diff(contextHookData, [2]string{"main", "select count(*) from users"}); diff != "" {
t.Errorf("Context hook result (-got, +want):\n%s", diff)
}
})

t.Run("UI", func(t *testing.T) {
q := make(url.Values)
q.Set("q", "select location from users where name = 'alice'")
Expand Down

0 comments on commit 26319f3

Please sign in to comment.