Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(484)

Unified Diff: state/apiserver/resource.go

Issue 9746043: state/api: Split to multiple files (Closed)
Patch Set: Created 11 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « state/apiserver/perm_test.go ('k') | state/apiserver/root.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: state/apiserver/resource.go
=== added file 'state/apiserver/resource.go'
--- state/apiserver/resource.go 1970-01-01 00:00:00 +0000
+++ state/apiserver/resource.go 2013-05-24 17:53:15 +0000
@@ -0,0 +1,82 @@
+// Copyright 2013 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package apiserver
+
+import (
+ "launchpad.net/juju-core/log"
+ "strconv"
+ "sync"
+)
+
+// resource represents the interface provided by state watchers and pingers.
+type resource interface {
+ Stop() error
+}
+
+// resources holds all the resources for a connection.
+type resources struct {
+ mu sync.Mutex
+ maxId uint64
+ rs map[string]*srvResource
+}
+
+// srvResource holds the details of a resource. It also implements the
+// Stop RPC method for all resources.
+type srvResource struct {
+ rs *resources
+ resource resource
+ id string
+}
+
+// Stop stops the given resource. It causes any outstanding
+// Next calls to return a CodeStopped error.
+// Any subsequent Next calls will return a CodeNotFound
+// error because the resource will no longer exist.
+func (r *srvResource) Stop() error {
+ err := r.resource.Stop()
+ r.rs.mu.Lock()
+ defer r.rs.mu.Unlock()
+ delete(r.rs.rs, r.id)
+ return err
+}
+
+func newResources() *resources {
+ return &resources{
+ rs: make(map[string]*srvResource),
+ }
+}
+
+// get returns the srvResource registered with the given
+// id, or nil if there is no such resource.
+func (rs *resources) get(id string) *srvResource {
+ rs.mu.Lock()
+ defer rs.mu.Unlock()
+ return rs.rs[id]
+}
+
+// register records the given watcher and returns
+// a srvResource instance for it.
+func (rs *resources) register(r resource) *srvResource {
+ rs.mu.Lock()
+ defer rs.mu.Unlock()
+ rs.maxId++
+ sr := &srvResource{
+ rs: rs,
+ id: strconv.FormatUint(rs.maxId, 10),
+ resource: r,
+ }
+ rs.rs[sr.id] = sr
+ return sr
+}
+
+func (rs *resources) stopAll() {
+ rs.mu.Lock()
+ defer rs.mu.Unlock()
+ for _, r := range rs.rs {
+ if err := r.resource.Stop(); err != nil {
+ log.Errorf("state/api: error stopping %T resource: %v", r, err)
+ }
+ }
+ rs.rs = make(map[string]*srvResource)
+}
« no previous file with comments | « state/apiserver/perm_test.go ('k') | state/apiserver/root.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b