Skip to content

Commit edce684

Browse files
authored
v23: add 'GetRequestID' to the v23 and runtime APIs. (#169)
This PR moves the API for adding/getting requestIDs to the v23 level for GetRequestID but hides the ability to set a requestID to within the runtime since it only makes sense to do so from within the runtime implementation.
1 parent 7361d5b commit edce684

File tree

7 files changed

+53
-30
lines changed

7 files changed

+53
-30
lines changed

v23/init_internal_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/google/uuid"
1213
"v.io/v23/context"
1314
"v.io/v23/discovery"
1415
"v.io/v23/flow"
@@ -43,6 +44,9 @@ func (*mockRuntime) GetListenSpec(ctx *context.T) rpc.ListenSpec { return rpc.Li
4344
func (*mockRuntime) GetPermissionsSpec(ctx *context.T) access.PermissionsSpec {
4445
return access.PermissionsSpec{}
4546
}
47+
func (*mockRuntime) GetRequestID(ctx *context.T) uuid.UUID {
48+
return uuid.UUID{}
49+
}
4650
func (*mockRuntime) WithBackgroundContext(ctx *context.T) *context.T { return nil }
4751
func (*mockRuntime) GetBackgroundContext(ctx *context.T) *context.T { return nil }
4852

v23/model.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28+
"github.com/google/uuid"
2829
"v.io/v23/context"
2930
"v.io/v23/discovery"
3031
"v.io/v23/flow"
@@ -134,6 +135,9 @@ type Runtime interface {
134135
// Dispatcher's Lookup method which will returns the object and
135136
// security.Authorizer used to serve the actual RPC call.
136137
WithNewDispatchingServer(ctx *context.T, name string, disp rpc.Dispatcher, opts ...rpc.ServerOpt) (*context.T, rpc.Server, error)
138+
139+
// GetRequestID returns the RequestID in 'ctx'.
140+
GetRequestID(ctx *context.T) uuid.UUID
137141
}
138142

139143
// WithPrincipal attaches 'principal' to the returned context.
@@ -207,6 +211,13 @@ func GetReservedNameDispatcher(ctx *context.T) rpc.Dispatcher {
207211
return initState.currentRuntime().GetReservedNameDispatcher(ctx)
208212
}
209213

214+
// GetRequestID returns the RequestID in 'ctx'. The underlying runtime should
215+
// set a new request ID for each new request that it receives and is prepared
216+
// to process.
217+
func GetRequestID(ctx *context.T) uuid.UUID {
218+
return initState.currentRuntime().GetRequestID(ctx)
219+
}
220+
210221
// NewFlowManager creates a new flow.Manager instance.
211222
// channelTimeout specifies the duration we are willing to wait before determining
212223
// that connections managed by this FlowManager are unhealthy and should be

v23/requestid/model.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

x/ref/runtime/factories/fake/runtime.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fake
66

77
import (
8+
"github.com/google/uuid"
89
v23 "v.io/v23"
910
"v.io/v23/context"
1011
"v.io/v23/discovery"
@@ -79,6 +80,10 @@ func (r *Runtime) GetPermissionsSpec(ctx *context.T) access.PermissionsSpec {
7980
return access.PermissionsSpec{}
8081
}
8182

83+
func (r *Runtime) GetRequestID(ctx *context.T) uuid.UUID {
84+
return uuid.UUID{}
85+
}
86+
8287
func (r *Runtime) NewDiscovery(ctx *context.T) (discovery.T, error) {
8388
// nologcall
8489
panic("unimplemented")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package rpc
2+
3+
import (
4+
"github.com/google/uuid"
5+
6+
"v.io/v23/context"
7+
)
8+
9+
type contextKey int
10+
11+
const requestIDKey = contextKey(iota)
12+
13+
func WithRequestID(ctx *context.T, requestID uuid.UUID) *context.T {
14+
return context.WithValue(ctx, requestIDKey, requestID)
15+
}
16+
17+
func RequestID(ctx *context.T) uuid.UUID {
18+
requestID, _ := ctx.Value(requestIDKey).(uuid.UUID)
19+
return requestID
20+
}

x/ref/runtime/internal/rpc/server.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
"sync"
1414
"time"
1515

16+
"github.com/google/uuid"
1617
"golang.org/x/net/trace"
1718
v23 "v.io/v23"
1819
"v.io/v23/context"
1920
"v.io/v23/flow"
2021
"v.io/v23/i18n"
2122
"v.io/v23/naming"
2223
"v.io/v23/options"
23-
"v.io/v23/requestid"
2424
"v.io/v23/rpc"
2525
"v.io/v23/security"
2626
"v.io/v23/security/access"
@@ -606,8 +606,12 @@ var (
606606
)
607607

608608
func newXFlowServer(flow flow.Flow, server *server) (*flowServer, error) {
609-
ctx := requestid.WithNewRequestID(server.ctx)
610-
ctx = context.WithLoggingPrefix(ctx, requestid.RequestID(ctx))
609+
requestID, err := uuid.NewUUID()
610+
if err != nil {
611+
return nil, fmt.Errorf("failed to allocate requestid: %v", err)
612+
}
613+
ctx := WithRequestID(server.ctx, requestID)
614+
ctx = context.WithLoggingPrefix(ctx, requestID)
611615
fs := &flowServer{
612616
ctx: ctx,
613617
server: server,

x/ref/runtime/internal/rt/runtime.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
"syscall"
1616
"time"
1717

18-
"v.io/x/lib/metadata"
19-
18+
"github.com/google/uuid"
2019
v23 "v.io/v23"
2120
"v.io/v23/context"
2221
"v.io/v23/discovery"
@@ -29,7 +28,7 @@ import (
2928
"v.io/v23/security/access"
3029
"v.io/v23/verror"
3130
"v.io/v23/vtrace"
32-
31+
"v.io/x/lib/metadata"
3332
"v.io/x/ref/internal/logger"
3433
idiscovery "v.io/x/ref/lib/discovery"
3534
"v.io/x/ref/lib/flags"
@@ -393,6 +392,10 @@ func (*Runtime) GetPermissionsSpec(ctx *context.T) access.PermissionsSpec {
393392
return ps
394393
}
395394

395+
func (*Runtime) GetRequestID(ctx *context.T) uuid.UUID {
396+
return irpc.RequestID(ctx)
397+
}
398+
396399
func (*Runtime) WithBackgroundContext(ctx *context.T) *context.T {
397400
// Note we add an extra context with a nil value here.
398401
// This prevents users from travelling back through the

0 commit comments

Comments
 (0)