Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
go graphql: allow overriding min rerun interval per query
Browse files Browse the repository at this point in the history
  • Loading branch information
changpingc committed Aug 30, 2018
1 parent 73fa22f commit 0fb7086
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions graphql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type JSONSocket interface {

type MakeCtxFunc func(context.Context) context.Context

type RerunIntervalFunc func(context.Context, *Query) time.Duration

type GraphqlLogger interface {
StartExecution(ctx context.Context, tags map[string]string, initial bool)
FinishExecution(ctx context.Context, tags map[string]string, delay time.Duration)
Expand Down Expand Up @@ -66,8 +68,8 @@ type conn struct {
mu sync.Mutex
subscriptions map[string]*reactive.Rerunner

minRerunInterval time.Duration
maxSubscriptions int
minRerunIntervalFunc RerunIntervalFunc
maxSubscriptions int
}

type inEnvelope struct {
Expand Down Expand Up @@ -280,7 +282,7 @@ func (c *conn) handleSubscribe(in *inEnvelope) error {
}

return nil, nil
}, c.minRerunInterval)
}, c.minRerunIntervalFunc(c.ctx, query))

return nil
}
Expand Down Expand Up @@ -375,7 +377,7 @@ func (c *conn) handleMutate(in *inEnvelope) error {
go c.rerunSubscriptionsImmediately()

return nil, errors.New("stop")
}, c.minRerunInterval)
}, c.minRerunIntervalFunc(c.ctx, query))

return nil
}
Expand Down Expand Up @@ -527,8 +529,8 @@ func CreateConnection(ctx context.Context, socket JSONSocket, schema *Schema, op
makeCtx: func(ctx context.Context) context.Context {
return ctx
},
maxSubscriptions: DefaultMaxSubscriptions,
minRerunInterval: DefaultMinRerunInterval,
maxSubscriptions: DefaultMaxSubscriptions,
minRerunIntervalFunc: func(context.Context, *Query) time.Duration { return DefaultMinRerunInterval },
}
for _, opt := range opts {
opt(c)
Expand All @@ -545,7 +547,7 @@ func WithExecutionLogger(logger GraphqlLogger) ConnectionOption {

func WithMinRerunInterval(d time.Duration) ConnectionOption {
return func(c *conn) {
c.minRerunInterval = d
c.minRerunIntervalFunc = func(context.Context, *Query) time.Duration { return d }
}
}

Expand Down Expand Up @@ -573,6 +575,13 @@ func WithSubscriptionLogger(logger SubscriptionLogger) ConnectionOption {
}
}

// WithMinRerunIntervalFunc is deprecated.
func WithMinRerunIntervalFunc(fn RerunIntervalFunc) ConnectionOption {
return func(c *conn) {
c.minRerunIntervalFunc = fn
}
}

func (c *conn) ServeJSONSocket() {
defer c.closeSubscriptions()

Expand Down

0 comments on commit 0fb7086

Please sign in to comment.