-
Notifications
You must be signed in to change notification settings - Fork 9
/
wire.go
61 lines (49 loc) · 1.61 KB
/
wire.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package deadlines
import (
"context"
"sync"
"time"
"google.golang.org/grpc"
nsgrpc "namespacelabs.dev/foundation/std/grpc"
)
var (
mu sync.RWMutex
registrations []*DeadlineRegistration
)
type interceptor struct{}
func (interceptor) unary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
service, method := nsgrpc.SplitMethodName(info.FullMethod)
var selected *Deadline_Configuration
if service != "" && method != "" {
mu.RLock()
outer:
for _, reg := range registrations {
for _, conf := range reg.conf.GetConfiguration() {
if (conf.ServiceName == "*" || conf.ServiceName == service) && (conf.MethodName == "*" || conf.MethodName == method) {
selected = conf
break outer
}
}
}
mu.RUnlock()
}
if selected != nil {
// Go will already make sure that we can't increase the incoming deadline.
newCtx, cancel := context.WithTimeout(ctx, time.Duration(selected.MaximumDeadline*1000000000))
defer cancel()
ctx = newCtx
}
return handler(ctx, req)
}
func (interceptor) streaming(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
// XXX streaming deadlines.
return handler(srv, stream)
}
func Prepare(ctx context.Context, deps ExtensionDeps) error {
var interceptor interceptor
deps.Interceptors.ForServer(interceptor.unary, interceptor.streaming)
return nil
}