Skip to content

Opni Plugins

Alexandre Lamarre edited this page Apr 18, 2023 · 1 revision

Opni Plugins

Gateway

TODO

System Plugin

TODO

Caching Provider

The caching provider helps cache RPCs between GRPC services. It is a Client-side caching mechanism.

Caching requirements
  • A cache provider is set for the plugin, see below.
  • The method you wish to cache must have a request that is cacheable, which requires one of :
    • Implementing pkg/caching/CacheKeyer interface
    • Registering the request type func RegisterProtoType(msg proto.Message, f func(msg protoreflect.ProtoMessage) string) at init time
Configuring the cache mechanism

System plugins provide a method UseCachingProvider(c caching.CachingProvider[proto.Message]) that lets you access the underlying cache, and set the cache provider.

func (p *Plugin) UseCachingProvider(c caching.CachingProvider[proto.Message]) {
	c.SetCache(caching.NewInMemoryGrpcTtlCache(50*1024*1024, time.Minute))
}
Opt in/out of caching responses

By default no responses are cached unless opted into.

  • wrapping a request context with WithGrpcClientCaching(ctx context.Context, d time.Duration) :
clusterList, err := mgmtClient.ListClusters(
		caching.WithGrpcClientCaching(ctx, 1*time.Minute),
		&managementv1.ListClustersRequest{},
	)
  • WithBypassCache forcefully invalidates the cache :
clusterList, err := mgmtClient.ListClusters(
		caching.WithBypassCache(ctx),
		&managementv1.ListClustersRequest{},
	)
  • Servers are also free to force clients to cache their responses if the server method uses ForceClientCaching(ctx context.Context, ttl time.Duration). Note that WithBypassCache does not respect this directive:
func (c *SimpleServer) GetValueWithForcedClientCaching(ctx context.Context, _ *emptypb.Empty) (*Value, error) {
	c.mu.Lock()
	defer c.mu.Unlock()
	caching.ForceClientCaching(ctx, c.cacheMaxAge)
	return &Value{
		Value: c.value,
	}, nil
}

Agent

TODO

Clone this wiki locally